Reputation: 11
I am working with AEM version 6.2 and I need to create a servlet that connects to a backend system and call a restful service. This has to done using the doPost method and not the doGet for security reasons. I have been able to register the Sling based servlet within AEM however, I have not been able to call POST a request to the servlet. I have registered the using the @SlingServlet annotation using resourceTypes and Paths but still the doPost is not being called only doGet. I know the default call is to doGet from the server but I have specified the method as POST.
package com.dash.dev.servlets.core.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.jcr.api.SlingRepository;
@SlingServlet(name="A Servlet2",
resourceTypes = "/apps/DashServletApp/components/structure/page",
paths = "/bin/case/enrol2",
methods = "POST",
metatype=true)
public class AServletByPath extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 7175649210039853979L;
@Reference
private SlingRepository repository;
public void bindRepository(SlingRepository repository){
this.repository = repository;
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
HttpSession session = request.getSession();
String sessionID = session.getId();
System.out.println(sessionID);
PrintWriter out = response.getWriter();
out.println(sessionID + "POST Method called");
out.flush();
out.close();
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
HttpSession session = request.getSession();
String sessionID = session.getId();
System.out.println(sessionID);
PrintWriter out = response.getWriter();
out.println(sessionID + "POST Method called");
out.flush();
out.close();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally
{
//Important: Close the connect
httpClient.getConnectionManager().shutdown();
}
}
}
I have read the Sling documnetation and took particular note of:
The request methods supported by the servlet. The property value must either be a single String, an array of Strings or a Vector of Strings. This property is only considered for the registration with sling.servlet.resourceTypes. If this property is missing, the value defaults to GET and HEAD, regardless of which methods are actually implemented/handled by the servlet.
thank you!
Upvotes: 1
Views: 1770
Reputation: 9281
As the naeme suggests the SlingSafeMethodsServlet is supposed to be used only for "Safe" requests, which are GET
, HEAD
, OPTIONS
etc. By default this servlet doesn't support POST
, PUT
and DELETE
.
Quoting the docs
This base class is intended for applications where data is only read. As such, this servlet by itself does not support the POST, PUT and DELETE methods. Extensions of this class should either overwrite any of the doXXX methods of this class or add support for other read-only methods only.
Applications wishing to support data modification should rather use or extend the SlingAllMethodsServlet which also contains support for the POST, PUT and DELETE methods. This latter class should also be overwritten to add support for HTTP methods modifying data.
You can extend the SlingAllMethodsServlet to cater to your POST
requests.
Upvotes: 4