How to produce visualized output from Sling POST to AEM?

It seems that Sling expects every form of POST to change JCR. So the expected / standard behavior will be POST-redirect-GET, which is great for most things. However, I need to be able to POST for AEM, and then use the data in this POST to create a visualized result. Our use of AEM is void and therefore I do not want to transfer POST'd data to the session in order to use it in a subsequent GET.

Some of them recommend putting POST data in the browser’s SessionStorage, but this is not broadly supported.

As far as I can tell, for Sling in AEM there is no way to take POST and produce a visualized result.

Here is a screenshot of what POST creates in the page / resourceType component and in any included jsp Sling that is involved in rendering.

enter image description here

I tried things like using the "nop" operation .

<input type="hidden" name=":operation" value="nop" />

But in any case, all servlets believe that POST occurs and is not displayed properly.

There is the possibility of creating a custom servlet to handle POST, but how do you process the template output and modify the request so that all components consider that they are serving GET?

UPDATED: Here is a screenshot of the result of "nop" POST.jsp. enter image description here

+4
source share
3 answers

What you can do is create a file POST.jspin the corresponding resourceType.

POST /content/yourapp/something, resourceType: your/app/example. /apps/your/app/example/POST.jsp . script POST.jsp, , , GET.

- , POST, SlingRequestProcessor. Sling. SlingRequestWrapper, getMethod(), "GET". , GET.

+2

, IIUC P ?

POST , - slingRequest.getRequestDispatcher(resource).forward(request, response) , request.getMethod() GET. P .

SlingHttpServletRequestWrapper .

+2

. - 302.

Another solution that comes to my mind is a special filter that will do the same. However, since AEM expects to get 200 instead of 302, it would be nice to say by attribute or parameter that this POST needs to be redirected. Otherwise, some AEM UI functions might be slow. This is a quick example of an idea. You will probably need to write something more complex.

@Component(immediate = true)
@Service
@Properties({
    @Property(name = Constants.SERVICE_DESCRIPTION, value = "Desc"),
    @Property(name = Constants.SERVICE_VENDOR, value = "Company name"),
    @Property(name = Constants.SERVICE_RANKING, intValue = RedirectFilter.RANKING),
    @Property(name = "filter.scope", value = "request") })
public class RedirectFilter implements Filter {

public static final int RANKING = -1000; // low ranking

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
}

@Override
public void destroy() {
}

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {
    if (request.getParameter("redirect").equals("true")) {
        ((SlingHttpServletResponse) response).sendRedirect(((SlingHttpServletRequest)request).getRequestURI());
    }
}

}

+1
source

Source: https://habr.com/ru/post/1537529/


All Articles