sling servlet filter | Community
Skip to main content
New Participant
October 16, 2015
Solved

sling servlet filter

  • October 16, 2015
  • 9 replies
  • 4805 views

Hi,

 

I would like to write a filter that will intercept a page properties post request. My filter will check to see what parameters are being posted and if it find the ones I am interested in. I would like to stop further processing of this post and do a redirect to another page. So far I have had no luck. Can you please provide an example.

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Sham_HC

One sample filter for a reference at  http://aemfaq.blogspot.com/2013/05/blocking-anonymous-access-to-crx-in-non.html

9 replies

Sham_HC
Sham_HCAccepted solution
New Participant
October 16, 2015
smacdonald2008
New Participant
October 16, 2015

Did you install the package and see the redirect in action - i just installed it and it worked for me. Here is the code for the Filter: See the attachment -- you need to get similar code into your project.

chrisu168Author
New Participant
October 16, 2015

hi,

 

thanks for that. I was looking at the code and this looks like a java servlet filter is it possible to use a sling servlet filter and is there any differences?

Thanks

chrisu168Author
New Participant
October 16, 2015

hi,

when you say install the package you mean upload it into package manager is that correct ?

I will do that now for a fresh publisher instance.

chrisu168Author
New Participant
October 16, 2015

Hi,

I have installed it on a fresh instance and tried to change the redirect locations and it does not point to the new locations

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { if ( req instanceof HttpServletRequest && res instanceof HttpServletResponse ) { final HttpServletRequest request = (HttpServletRequest)req; final HttpServletResponse response = (HttpServletResponse)res; String pathInfo = request.getPathInfo() ; boolean crxdeAuthenticated = false; boolean crxAuthenticated = false; if(pathInfo != null){ Cookie[] cookies = request.getCookies(); if(cookies!=null){ for (int i = 0; i < cookies.length; i++) { String name = cookies[i].getName(); String value = cookies[i].getValue(); if(name!=null && name.equals("login-workspace") && value!=null){ crxAuthenticated = true; } if(name!=null && name.equals("login-token") && value!=null){ crxdeAuthenticated = true; } } } if(pathInfo.startsWith("/crx/explorer/crx_main_files/admin.css")){ //Do nothing }else if ( !pathInfo.startsWith("/crx/explorer/login.jsp") && pathInfo.startsWith("/crx/explorer") &&  !crxAuthenticated ){ response.sendRedirect("/content/geometrixx-outdoors/en.html"); return; }else if( ( pathInfo.startsWith("/crxde") || pathInfo.startsWith("/crx/de") ) &&  !crxdeAuthenticated ){ RequestDispatcher rd = request.getRequestDispatcher("/content/geometrixx-outdoors/en.html"); rd.forward(request, response); return; } } } chain.doFilter(req, res); }
smacdonald2008
New Participant
October 16, 2015

A sling servlet is a Java Servlet - the difference is the classes that the servlet inherits from. For example:

http://sling.apache.org/apidocs/sling5/org/apache/sling/api/servlets/SlingAllMethodsServlet.html

Did you successfully deploy your servlet and can you post data to it?

chrisu168Author
New Participant
October 16, 2015

Hi,

 

I have managed to post to the servlet filter, the problems is when I try to do a redirect from the servlet filter to another page. I get an alert box on the browser and the browser refuses to go the the redirect page. I have looked inside the browser console for firefox and I can see that it is doing a post for the current page and a get to the redirected page and then I see an alert on the browser. I cannot see any errors of any sort.

Thanks

chrisu168Author
New Participant
October 16, 2015

I have done followed Sham's suggestion and this is the result I get.

smacdonald2008
New Participant
October 16, 2015

I recommend trying the solution that Sham suggested.