Reputation: 6270
I am implementing a Authentication Filter and I am also using it for exception handling.
As there is no web.xml in CQ5 so we can't put a mapping like <exception-type>
.
All my controllers(Servlets) throw Exception back to caller. And I have a filter that is called before each and every one of my Servlets. So I am explicitly throwing
new SerlvetException()
in one of my servlets. This is ofcourse thrown to the caller i.e my main filter. Exception occurs correctly at chain.doFilter()
and I am handling the exception here(in filter). but it just doesn't get handled why?
Also,what is this Scope property SlingFilter I dont understand this REQUEST,ERROR, COMPONENT,INCLUDE property of SlingFilter SCOPE
Here is my code and stack trace.
public class AddNewUserController extends SlingAllMethodsServlet{
/**
* @author Oliver Kaunds
*/
private static final long serialVersionUID = 1L;
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Reference
protected AddNewUserService addNewUserService;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
log.info("VideoJet :: Do GET Called ");
String path =request.getRequestURI();
log.info("VIDEOJET PATH :: "+ path);
try{
if(path.equals("/services/videojet/v1/AddNewUserController/view"))
{
List<HRRepresentative> list =addNewUserService.getHRRepresentative();
request.setAttribute("hrList",list );
HttpSession session = request.getSession();
String userOperation =(String)session.getAttribute("userOp");
request.setAttribute("userOp", userOperation);
session.removeAttribute("userOp");
throw new Exception("Error Page Test"); // Manuly throw exception
}
And my main filter
package com.videojet.hiresite.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingFilter(order=1)
@Properties({
@Property(name="service.pid", value="com.videojet.hiresite.filters.HireSiteFilter",propertyPrivate=false),
@Property(name="service.description",value="Main Authentication Filter", propertyPrivate=false),
@Property(name="service.vendor",value="Zensar Tech", propertyPrivate=false),
@Property(name="pattern",value="/services/videojet/v1/.*", propertyPrivate=false)
})
public class HireSiteFilter implements Filter {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Authentication Filter for the whole application
log.info("HireSiteFilter Invoked");
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
String path =httpServletRequest.getRequestURI();
log.info("Request URI on AJAX ::"+path);
try{
if(path.contains("LoginController"))
{
log.info("HireSiteFilter :: Request to LoginController");
chain.doFilter(request, response);
}
else
{
HttpSession session = httpServletRequest.getSession(false);
if(session ==null || session.getAttribute("userId")==null)
{
log.info("HireSiteFilter :: Not Logged in");
HttpServletResponse httpResponse = (HttpServletResponse)response;
httpResponse.sendRedirect("/content/videojet/en/loginPage.html");
}
else
{
log.info("HireSiteFilter :: Logged in");
chain.doFilter(request, response); // This is within Try block---!!!!!!!!!!
}
}
}
catch(Exception exception)
{
// Internal Server Error Manul dispatch to Error Page
// /content/videojet/en/errorPage.html
log.info("********************* TEST ***********************************");
HttpServletResponse httpServletResponse =(HttpServletResponse)response;
httpServletResponse.sendRedirect("/content/videojet/en/errorPage.html");
}
}
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
}
}
the Exception occurs correct but is not caught by the catch block. the stack trace is below.
04.07.2014 11:46:26.395 *INFO* [0:0:0:0:0:0:0:1 [1404454586395] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter HireSiteFilter Invoked
04.07.2014 11:46:26.395 *INFO* [0:0:0:0:0:0:0:1 [1404454586395] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter Request URI on AJAX ::/services/videojet/v1/LoginController/validateUser
04.07.2014 11:46:26.395 *INFO* [0:0:0:0:0:0:0:1 [1404454586395] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter HireSiteFilter :: Request to LoginController
04.07.2014 11:46:26.399 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController Do Post **************
04.07.2014 11:46:26.399 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController The Username :::::foster
04.07.2014 11:46:26.399 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController The Password ::::::test
04.07.2014 11:46:26.399 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.daos.EmployeeDaoImp JCR QUERY 1::SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('/content/videojetdata/HR_EMPLOYEE') AND NAME(s) = 'foster'
04.07.2014 11:46:26.864 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.daos.EmployeeDaoImp Query Check :: Presenttest
04.07.2014 11:46:26.866 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.services.login.LoginServiceImp The Passwrod from the daatabase+++++++test**********
04.07.2014 11:46:26.866 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController Username::foster
04.07.2014 11:46:26.866 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController Password::test
04.07.2014 11:46:26.869 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.daos.EmployeeDaoImp JCR Query::SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('/content/videojetdata/HR_EMPLOYEE') AND NAME(s) = 'foster'
04.07.2014 11:46:26.943 *INFO* [0:0:0:0:0:0:0:1 [1404454586943] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter HireSiteFilter Invoked
04.07.2014 11:46:26.943 *INFO* [0:0:0:0:0:0:0:1 [1404454586943] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter Request URI on AJAX ::/services/videojet/v1/AddNewUserController/view
04.07.2014 11:46:26.943 *INFO* [0:0:0:0:0:0:0:1 [1404454586943] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter HireSiteFilter :: Logged in
04.07.2014 11:46:26.945 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.controllers.AddNewUserController VideoJet :: Do GET Called
04.07.2014 11:46:26.945 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.controllers.AddNewUserController VIDEOJET PATH :: /services/videojet/v1/AddNewUserController/view
04.07.2014 11:46:26.945 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.daos.HRRepresentativeDaoImp THE JCR QUERY ::SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('/content/videojetdata/HR_REP')
04.07.2014 11:46:27.740 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] org.apache.jackrabbit.core.query.lucene.DocNumberCache size=715/1024, #accesses=1001, #hits=256, #misses=745, cacheRatio=26%
04.07.2014 11:46:29.642 *INFO* [pool-6-thread-24-com/day/cq/replication/job/publish(com/day/cq/replication/job/publish)] com.day.cq.replication.impl.AgentManagerImpl Processing job for agent publish
04.07.2014 11:46:29.643 *INFO* [pool-6-thread-24-com/day/cq/replication/job/publish(com/day/cq/replication/job/publish)] com.day.cq.replication.Agent.publish Sending POST request to http://localhost:4503/bin/receive?sling:authRequestLogin=1
04.07.2014 11:46:29.893 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] org.apache.jackrabbit.core.query.lucene.DocNumberCache size=121/1024, #accesses=1001, #hits=520, #misses=481, cacheRatio=52%
04.07.2014 11:46:30.604 *ERROR* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.controllers.AddNewUserController VIDEJET EXCEPTION:: java.lang.Exception: Error Page Test
at com.videojet.hiresite.controllers.AddNewUserController.doGet(com.videojet.hiresite.controllers.AddNewUserController.java:75)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:268)
at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:139)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:500)
at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:147)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:308)
at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:141)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:257)
at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
at com.day.cq.wcm.core.impl.warp.TimeWarpFilter.doFilter(TimeWarpFilter.java:106)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter.doFilter(RedirectFilter.java:296)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter.doFilter(RequestProgressTrackerLogFilter.java:59)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet.doFilter(FormsHandlingServlet.java:220)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.theme.impl.ThemeResolverFilter.doFilter(ThemeResolverFilter.java:76)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:117)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.wcm.core.impl.WCMRequestFilter.doFilter(WCMRequestFilter.java:89)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.rewriter.impl.RewriterFilter.doFilter(RewriterFilter.java:83)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.bgservlets.impl.BackgroundServletStarterFilter.doFilter(BackgroundServletStarterFilter.java:135)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processRequest(SlingRequestProcessorImpl.java:153)
at org.apache.sling.engine.impl.SlingMainServlet.service(SlingMainServlet.java:206)
at org.apache.felix.http.base.internal.handler.ServletHandler.doHandle(ServletHandler.java:96)
at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:79)
at org.apache.felix.http.base.internal.dispatch.ServletPipeline.handle(ServletPipeline.java:42)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:49)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:117)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.security.impl.ReferrerFilter.doFilter(ReferrerFilter.java:238)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at com.adobe.granite.license.impl.LicenseCheckFilter.doFilter(LicenseCheckFilter.java:179)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at com.videojet.hiresite.filters.HireSiteFilter.doFilter(com.videojet.hiresite.filters.HireSiteFilter.java:65)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:78)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.engine.impl.log.RequestLoggerFilter.doFilter(RequestLoggerFilter.java:75)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.felix.http.base.internal.dispatch.FilterPipeline.dispatch(FilterPipeline.java:48)
at org.apache.felix.http.base.internal.dispatch.Dispatcher.dispatch(Dispatcher.java:39)
at org.apache.felix.http.base.internal.DispatcherServlet.service(DispatcherServlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at com.day.j2ee.servletengine.ServletRuntimeEnvironment.service(ServletRuntimeEnvironment.java:250)
at com.day.j2ee.servletengine.RequestDispatcherImpl.doFilter(RequestDispatcherImpl.java:315)
at com.day.j2ee.servletengine.RequestDispatcherImpl.service(RequestDispatcherImpl.java:334)
at com.day.j2ee.servletengine.RequestDispatcherImpl.service(RequestDispatcherImpl.java:377)
at com.day.j2ee.servletengine.ServletHandlerImpl.process(ServletHandlerImpl.java:351)
at com.day.j2ee.servletengine.HttpListener$Worker.run(HttpListener.java:625)
at java.lang.Thread.run(Unknown Source
Upvotes: 1
Views: 924
Reputation: 47
Try using scope as @SlingFilter(scope = SlingFilterScope.COMPONENT, order = -10000, metatype = false) I think in your case it should be SlingFilterScope.ERROR
sling.filter.scope – Indicates the filter chain the filter should be part of. Required! Sling won’t pick up your filter if it isn’t set.
Also, Check Filterconditions at first place like shown below
public void doFilter(ServletRequest pRequest, ServletResponse pResponse,
FilterChain pChain) throws IOException, ServletException {
if (!(pRequest instanceof SlingHttpServletRequest)) {
throw new ServletException("Request is not a Sling HTTP request.");
}
if (isFilterEnabled(slingRequest)) {
// Implement Filter-Logic
} else {
// continue with filter chaining
pChain.doFilter(pRequest, pResponse);
}
}
Upvotes: 1
Reputation: 47
What servlet Filter can be used for ? 1. Authentication blocking request based on user identity 2. Logging and auditing - Tracking uses of a web application 3. Image conversion - Scaling maps and so on 4. Data Compression - Making downloads smaller 5. Localization - Targeting a request and response in particular locale 6. XSL/T Transformation of XML content
I am not sure but i think it doesn't support exception handling. Do check once.
Upvotes: 0
Reputation: 6100
Sling filter scopes are explained at http://sling.apache.org/documentation/the-sling-engine/filters.html
Upvotes: 1