Gestion de l’envoi du formulaire
[AEM Forms 6.5]{class="badge informative"}
Dans cette partie, nous allons créer un servlet simple qui s’exécute sur l’instance de publication AEM pour gérer l’envoi de formulaire HTML5. Ce servlet, à son tour, effectue une requête HTTP POST à un servlet s’exécutant dans une instance de création AEM responsable de l’enregistrement des données envoyées en tant que nœud nt:file
dans le référentiel de l’instance de création AEM.
Vous trouverez ci-dessous le code du servlet qui gère l’envoi du formulaire HTML5. Dans ce servlet, nous effectuons un appel POST vers un servlet monté sur /bin/startworkflow dans une instance de création AEM. Ce servlet enregistre les données de formulaire dans le référentiel de l’instance de création AEM.
Servlet de publication AEM
Le code ci-après gère l’envoi du formulaire HTML5. Ce code s’exécute sur l’instance de publication.
package com.aemforms.mobileforms.core.servlets;
import com.aemforms.mobileforms.core.configuration.service.AemServerCredentialsConfigurationService;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Servlet;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
@Component(service={Servlet.class}, property={"sling.servlet.methods=post", "sling.servlet.paths=/bin/handleformsubmission"})
public class HandleFormSubmission extends SlingAllMethodsServlet implements Serializable {
private static final long serialVersionUID = 1L;
private final transient Logger logger = LoggerFactory.getLogger(this.getClass());
@Reference
AemServerCredentialsConfigurationService aemServerCredentialsConfigurationService;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
logger.debug("In do POST of bin/handleformsubmission");
ByteArrayOutputStream result = new ByteArrayOutputStream();
try {
ServletInputStream is = request.getInputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
logger.debug(result.toString(StandardCharsets.UTF_8.name()));
} catch (IOException e1) {
logger.error("An error occurred", e1);
}
String postURL = aemServerCredentialsConfigurationService.getWorkflowServer();
logger.debug("The url to invoke workflow is "+postURL);
HttpPost postReq = new HttpPost(postURL);
// This is the base64 encoding of the admin credentials. This call should be made over HTTPS in production scenarios to avoid leaking credentials.
String userName = aemServerCredentialsConfigurationService.getUserName();
String password = aemServerCredentialsConfigurationService.getPassword();
String credential = userName+":"+password;
String encodedString = Base64.getEncoder().encodeToString(credential.getBytes());
postReq.addHeader("Authorization", "Basic "+encodedString);
System.out.println("The encoded string is "+"Basic "+encodedString);
CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
logger.debug("added url parameters");
try {
urlParameters.add(new BasicNameValuePair("xmlData", result.toString(StandardCharsets.UTF_8.name())));
postReq.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse httpResponse = httpClient.execute(postReq);
logger.debug("Sent request to author instance");
String startWorkflowResponse = EntityUtils.toString(httpResponse.getEntity());
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.write(startWorkflowResponse);
} catch (IOException e) {
logger.error("An error occurred", e);
}
}
}