primefaces exception handler not working

UI Components for JSF
Post Reply
senthinil
Posts: 54
Joined: 10 Jul 2013, 14:53

30 Jul 2015, 08:39

hi

I am implementing one view to be active for the logged-in user, if at any point in time other person should not be able to access the website with logged in same user credentials. I am using PF5.2 showcase demo, For the user first logged in, it works fine, if another user accesses the page using same credentials as the first,it rather redirecting to index_2.jsp, it throws java.lang.IllegalStateException Here is the code snippet :
in template.xhtml I added following code

Code: Select all

 <p:ajaxExceptionHandler type="com.test.exception.UnauthorizedAccessException"
                            update="unauthorisedAccess"
                            onexception="PF('unauthorisedAccessDialog').show();" />
               <p:remoteCommand name="onBrowserStuff"  id="remoteCommand"   
                                         
                                          action="#{chartView.onClicked}" update="idle" 
                process="@this"  />
                                        
      <p:dialog id="unauthorisedAccess"
         header="Unauthorised access"
         widgetVar="unauthorisedAccessDialog" 
         showEffect="fade" hideEffect="fade"
         modal="true" 
         width="400" 
         height="110" 
         closable="false"
         draggable="false" 
         resizable="false" 
         
         appendTo="@(body)"
         >
          <p:panel>
               Unauthorized access - only one user can access this token 
            </p:panel>
         </p:dialog>
         
In your ChartView.java I added onClicked() method as follows:

Code: Select all

public void onClicked() throws Exception
{
    
    if(map.containsKey(session.getId()) && dummyData.isLoggedIn())
    {
    	throw new UnauthorizedAccessException("User already logged in ...."); 
    }
    else if(dummyData.isLoggedIn()&&!map.containsKey(session.getId()))
    {
    	
    	 	throw new UnauthorizedAccessException("User already logged in");
    }
//rest of the code goes here
}
in web.xml

Code: Select all

 <context-param>
        <param-name>com.sun.faces.numberOfViewsInSession</param-name>
        <param-value>1</param-value>
    </context-param>
<!--    <context-param>
   <param-name>com.sun.faces.enableRestoreView22Compatibility</param-name>
   <param-value>true</param-value>
</context-param>-->
     <context-param>
        <param-name>com.sun.faces.numberOfLogicalViews</param-name>
        <param-value>1</param-value>
    </context-param>

 <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/index_2.jsp</location>
    </error-page>
    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
       <!-- <location>/error/viewExpired.xhtml</location>-->
       <location>/index.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/index_1.jsp</location>
    </error-page>
    
For the user first logged in, it works fine, if another user accesses the page it rather redirecting to index_2.jsp, it throws java.lang.IllegalStateException

Code: Select all


Jul 30, 2015 11:46:02 AM org.primefaces.application.exceptionhandler.PrimeExceptionHandler handle
SEVERE: Could not handle exception!
java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
	at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:483)
	at com.sun.faces.context.ExternalContextImpl.redirect(ExternalContextImpl.java:677)
	at org.primefaces.application.exceptionhandler.PrimeExceptionHandler.handleRedirect(PrimeExceptionHandler.java:339)
	at org.primefaces.application.exceptionhandler.PrimeExceptionHandler.handle(PrimeExceptionHandler.java:99)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
	at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:100)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.primefaces.showcase.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:32)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)


and in showcase.js I added :

Code: Select all

$(document).ready(function() {
 
    onBrowserStuff( [ { name: 'href', value: window.location.href } ] 
);
}
);
Please lemme know where I doing wrong
regards
sentil

senthinil
Posts: 54
Joined: 10 Jul 2013, 14:53

02 Aug 2015, 02:23

hi


I was doing wrong in the code in displaying chart,which was responsive.xhtml, I removed the repeated the line

<p:remoteCommand name="onBrowserStuff" id="remoteCommand"

action="#{chartView.onClicked}" update="idle"
process="@this" />

and it works. Hats off to PF5.2.


regards
sentil

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Bing [Bot] and 24 guests