Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

sling servlet filter

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Level 10
9 Replies

Avatar

Correct answer by
Level 10

Avatar

Level 10

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.

Avatar

Level 4

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

Avatar

Level 4

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.

Avatar

Level 4

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); }

Avatar

Level 10

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?

Avatar

Level 4

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

Avatar

Level 4

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

Avatar

Level 10

I recommend trying the solution that Sham suggested.