April 2, 2020
Estimated Post Reading Time ~

How to handle error handling for post-operation

Note: Found some issues & working on fixing it. Till then please do not use the package.

The standard error handler for handling http errors described at [1]. This error handling mechanism [1] doesn't apply to POST operations (Ex:- [2]). The article demonstrates the feature PostResponseCreator [3] which helps achieve error handling for post-operation. Based on my personal experience When using any product I recommend always

Never neglect to implement custom error pages because most security holes happen from here.
To give consistent look and feel like logo, headlines, message etc..The steps are simple

Implement PostResponseCreator
Sample Example below (Download and install Sample package of the above example.)package com.sample.errorhandler;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.servlets.post.HtmlResponse;
import org.apache.sling.servlets.post.PostResponse;
import org.apache.sling.servlets.post.AbstractPostResponse;
import org.apache.sling.servlets.post.PostResponseCreator;


@Component
@Service

public class PostResponseErrorHandler implements PostResponseCreator{

public static final String RP_SEND_ERROR =":sendError";

//responses to be sent back to the client and logic is written in createPostResponse method

public PostResponse createPostResponse(SlingHttpServletRequest req) {
if (req.getParameter(RP_SEND_ERROR) == null) {
return new AbstractPostResponse() {
public void onChange(String type, String... arguments) {
// NO-OP
}
@Override
protected void doSend(HttpServletResponse response) throws IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("Thanks for reading article !");
response.getWriter().write("Place Holder for your layout !");
response.getWriter().flush();
}

};
} else {
return null;
}
}
}


Output, after installing the package and following [2], is shown below

[1] http://docs.adobe.com/docs/en/aem/6-0/develop/platform/customizing-errorhandler-pages.html



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.