Partial AJAX calls don't handle container session timeouts

Components, Ajax Framework, Utilities and More.
Post Reply
User avatar
Oleg
Expert Member
Posts: 3803
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

09 Aug 2011, 18:32

Hello Cagatay,

My answer was for nickw (author of this post) :-) I don't know if my posts will help PrimeFaces. But, sure, you can implement global exception handler in PrimeFaces too. The pronlem is only - you need a redirect to any defined login / logout page if session timed out.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

tandraschko
PrimeFaces Core Developer
Posts: 3152
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

10 Aug 2011, 09:07

IMO we should have an global handler for the ajax errors!

Maybe something like:

Code: Select all

<p:registerAjaxErrorHandler type="ViewExpiredException" redirect="http://google.com" execute="alert('session timeout'); return true;" /> 
redirect and type should be optional. If type is missing, we should handle all exeptions. If execute returns true, we should redirect if redirect is not empty or defined.
Error type and message should be paramaters in the method for execute.

Also a clientside API would be great:

Code: Select all

PrimeFaces.ajax.registerAjaxErrorHandler('javax.faces.application.ViewExpiredException', 'http://google.com', new function(type, message) { alert('session timeout'); return true; });
Always Bet On Prime (+ Extensions)!

Thomas Andraschko
PrimeFaces Developer | PrimeFaces Extensions Founder
Apache OpenWebBeans PMC | Apache DeltaSpike PMC | Apache MyFaces PMC

Personal Blog: http://tandraschko.blogspot.de/

User avatar
Oleg
Expert Member
Posts: 3803
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

10 Aug 2011, 14:43

Hi Thomas,

p:registerAjaxErrorHandler sounds good, but it's registered on page level. Right? Should I write p:registerAjaxErrorHandler on every page if I want to catch exceptions for all pages? Well, template mechanism can help here, but I meant more or less a programmatic approach for gloabl exception handler and not declarativ one. Your idea is good anyway.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

12 Aug 2011, 14:41

A custom exception handler on the server side can handle exceptions in both Ajax and non-Ajax requests so I think that will generally be preferred over relying on the client side JavaScript to interpret an error response tag (handling the error tag would be a nice fall-back mechanism though). The typical response to an exception is to do a redirect so it is extremely important that the client handle the redirect response tag correctly. I have observed that the redirect tag is correctly interpreted in most cases, but in one case in particular it doesn't work: a dataTable row edit request made on an expired exception for example will not honor a redirect sent back in the response.

rodakr
Posts: 36
Joined: 17 Mar 2011, 00:50
Location: switzerland

12 Aug 2011, 16:06

Same Opinion as bumble.bee

If Browser sends request with expired Session ID, I should define on Server Side if I want answer with redirect to Login Side for traditional http POST/GET/DELETE/INSERT ) and for Ajack Request I might return Prime Face Dialog Box with Message "Session Expired" and Link to configurable URL ... ( URL could be mabe just web context /myappl or login/relogin URL /myappl/relogin )

... So maby some primefaces context parameters in web.xml to predefine the bahavior on ViewExpired?

Something Llike this:

<context-param>
<param-name>primefaces.redirect.javax.faces.application.ViewExpiredException</param-name>
<param-value>/login</param-value>
</context-param>
OSX 10.6.x , Fedora 14, XBuntu 10.x , majora 2.x, primefaces 3.0-snapshot, Tomcat 7.x , weblogic server 11.x, jboss 7.x , glassfish 3.x

rodakr
Posts: 36
Joined: 17 Mar 2011, 00:50
Location: switzerland

12 Aug 2011, 16:28

Until Optimuse provides nice solution with primefaces

Here What I use for now:

Code: Select all

import javax.faces.FacesException;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LifeCycleListener implements PhaseListener {

	   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	 private static final Log LOG = LogFactory.getLog("wlmp.ui.util");

	public PhaseId getPhaseId() {
	        return PhaseId.ANY_PHASE;
	}

	public void beforePhase(PhaseEvent event) {
		if(LOG.isTraceEnabled())  LOG.debug("START PHASE " + event.getPhaseId());
        FacesContext facesCtx = event.getFacesContext();
        ExternalContext extCtx = facesCtx .getExternalContext();
        HttpSession session = (HttpSession)extCtx .getSession(false); 
        boolean newSession = (session == null) || (session.isNew()); 
        boolean postback = !extCtx.getRequestParameterMap().isEmpty();
        boolean timedout = postback && newSession; 
        if(timedout) { 
        	if(LOG.isTraceEnabled())  LOG.debug(" Session Time out do redirect to /error.xhtml ");
        	Application app = facesCtx.getApplication(); 
        	ViewHandler viewHandler = app.getViewHandler();
        	facesCtx.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,"Session Time Out !", ""));
        	UIViewRoot view = viewHandler.createView( facesCtx, "/error.xhtml"); 
        	facesCtx.setViewRoot(view); 
        	facesCtx.renderResponse(); 
        	try { 
        		viewHandler.renderView(facesCtx, view); 
        		facesCtx.responseComplete(); 
        	} catch(Throwable t) { 
        		throw new FacesException( "Session timed out", t); 
        	} 
        	
        }
    }

    public void afterPhase(PhaseEvent event) {
      if(LOG.isTraceEnabled())  LOG.debug("END PHASE " + event.getPhaseId());
    }


}
in faces-config.xml

Code: Select all

 

..
..
<lifecycle>
  <phase-listener>LifeCycleListener</phase-listener>
 </lifecycle>
</faces-config>
OSX 10.6.x , Fedora 14, XBuntu 10.x , majora 2.x, primefaces 3.0-snapshot, Tomcat 7.x , weblogic server 11.x, jboss 7.x , glassfish 3.x

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

12 Aug 2011, 17:38

rodakr wrote:Same Opinion as bumble.bee
I'm suggesting a JSF 2 custom exception handler (javax.faces.context.ExceptionHandlerWrapper) dishing out redirects is generally a good way to handle errors, but you're suggesting using a Phase Listener and displaying a dialog on ajax error (most likely implemented by handling the error tag in the response).

I think your approach is fine, but it doesn't sound the same as mine.

tandraschko
PrimeFaces Core Developer
Posts: 3152
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

12 Aug 2011, 18:54

IMO a clientside handler is the better solution because you are flexible. A simple redirect can be handled with a exceptionhandler, too.
If the session expires, you can open modal dialogs with information etc.
Always Bet On Prime (+ Extensions)!

Thomas Andraschko
PrimeFaces Developer | PrimeFaces Extensions Founder
Apache OpenWebBeans PMC | Apache DeltaSpike PMC | Apache MyFaces PMC

Personal Blog: http://tandraschko.blogspot.de/

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

12 Aug 2011, 19:57

I agree that handling the error tag allows more flexibility. I just wish the simpler approach of handling the redirect tag worked in all PrimeFaces components!

Matrium
Posts: 112
Joined: 16 May 2011, 08:27

12 Aug 2011, 22:48

optimus.prime wrote:This is an important one, I'll take this into M3.
great to hear, thanks!

atm i am using idlemonitor to redirect to my timeout page and invalidate the session myself before a timeout occurs, but it doesn't feel like the best solution :-)
never tried to use redirect for this so far thought, will give it a shot (thanks oleg for the nice links)

it looks like most of the problems i run into while developing my first primefaces application are either already solved or will be soon.. i can't even begin to say how happy i am with primefaces! Thanks guys for all the great work!
PrimeFaces (Elite) 4.0.13, Majorra 2.1.28, Tomcat 7.0.53
Testing with Firefox, Chrome and IE9+IE10
<3 Primefaces!!!

Post Reply
  • Information
  • Who is online

    Users browsing this forum: Baidu [Spider], Google [Bot], Yahoo [Bot] and 2 guests