Showing posts with label view expired exception. Show all posts
Showing posts with label view expired exception. Show all posts

How to Handle ViewExpiredException in JSF

Here's the solution on how to handle the ViewExpiredException in JSF. This occurs when your session timed out or you tried to invalidate the session when you logged out. I've used this code to my 2 previous projects and it really worked fine.

First, you just need to add Spring 2.5 to your project libraries. Download Spring 2.5 here and just copy the code below.

import javax.faces.application.ViewExpiredException;
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 org.springframework.util.StringUtils;

public class SessionTimeoutFilter implements Filter {

private String homePage = "index.jsp";

@Override
public void init(FilterConfig filter) throws ServletException {
//Filter initialized
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException {
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
if (isSessionCheckingNeeded(httpServletRequest)) {
if ((isInvalidSession(httpServletRequest))) {
String timeoutUrl = httpServletRequest.getContextPath() + "/faces/" + getHomePage();
httpServletResponse.sendRedirect(timeoutUrl);
return;
}
}
}

try {
chain.doFilter(request, response);
} catch (ViewExpiredException ve) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;

String timeoutUrl = httpServletRequest.getContextPath() + "/faces/" + getHomePage();
httpServletResponse.sendRedirect(timeoutUrl);

} catch (Exception e) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;

String timeoutUrl = httpServletRequest.getContextPath() + "/faces/" + getHomePage();

if (StringUtils.countOccurrencesOf(e.getMessage(), "could not be restored.") > 0) {
httpServletResponse.sendRedirect(timeoutUrl);
return;
}

httpServletResponse.sendRedirect(timeoutUrl);
return;
}
}

private boolean isInvalidSession(HttpServletRequest httpServletRequest) {

boolean sessionInValid = (httpServletRequest.getRequestedSessionId() != null)
&& !httpServletRequest.isRequestedSessionIdValid();
return sessionInValid;
}

private boolean isSessionCheckingNeeded(HttpServletRequest request) {

int counter = StringUtils.countOccurrencesOf(request.getRequestURL().toString(), getHomePage())
+ StringUtils.countOccurrencesOf(request.getRequestURL().toString(), getHomePage());

if (counter > 0) {
return false;
}
return true;
}

@Override
public void destroy() {
//Session destroyed
}

public String getHomePage() {
return homePage;
}

public void setHomePage(String homePage) {
this.homePage = homePage;
}
}