Skapa servlet
Nästa steg är att skapa en servlet som anropar lämpliga metoder för vår anpassade OSGi-tjänst. Servern har åtkomst till adaptiva formulärdata, information om bifogade filer. Servern returnerar ett unikt program-ID som kan användas för att hämta det delvis ifyllda adaptiva formuläret.
Den här servern anropas när användaren klickar på knappen Spara och avsluta i det adaptiva formuläret
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