Criar servlet
A próxima etapa é criar um servlet que chame os métodos apropriados de nosso serviço OSGi personalizado. O servlet tem acesso aos dados do formulário adaptável, às informações de anexos de arquivo. O servlet retorna uma ID de aplicativo exclusiva que pode ser usada para recuperar o formulário adaptável parcialmente preenchido.
Esse servlet é chamado quando o usuário clica no botão Salvar e sair no formulário adaptável
package saveandresume.core.servlets;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.saveAndResume.core.SaveAndFetchDataFromDB;
@Component(service = {
Servlet.class
}, property = {
"sling.servlet.methods=post",
"sling.servlet.paths=/bin/storeafdatawithattachments"
})
public class StoreDataInDBWithAttachmentsInfo extends SlingAllMethodsServlet {
private Logger log = LoggerFactory.getLogger(StoreDataInDBWithAttachmentsInfo.class);
private static final long serialVersionUID = 1 L;
@Reference
SaveAndFetchDataFromDB saveAndFetchFromDB;
public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
final String afData = request.getParameter("data");
final String tel = request.getParameter("mobileNumber");
log.debug("$$$The telephone number is " + tel);
log.debug("The request parameter data is " + afData);
try {
JsonObject fileMap = JsonParser.parseString(request.getParameter("fileMap")).getAsJsonObject();
log.debug("The file map is: " + fileMap.toString());
String newFileMap = saveAndFetchFromDB.storeAFAttachments(fileMap, request);
String application_id = saveAndFetchFromDB.storeFormData(afData, newFileMap, tel);
log.debug("The application id: " + application_id);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("applicationID", application_id);
response.setContentType("application/json");
response.setHeader("Cache-Control", "nocache");
response.setCharacterEncoding("utf-8");
PrintWriter out = null;
out = response.getWriter();
out.println(jsonObject.toString());
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
}
8de24117-1378-413c-a581-01e660b7163e