Skip to content

Instantly share code, notes, and snippets.

@arockwell
Created August 26, 2009 14:18
Show Gist options
  • Select an option

  • Save arockwell/175525 to your computer and use it in GitHub Desktop.

Select an option

Save arockwell/175525 to your computer and use it in GitHub Desktop.
package edu.ufl.housing.checkin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.portals.bridges.struts.PortletTilesRequestProcessor;
/**
* This class depends on the SessionTimeoutPlugin, which must have the
* initialAction and validSessionTokenName properties configured.
*
* This class guard against timeout errors by ensuring the user passes through
* the initialAction.
*
* The initialAction must set the validSessionTokenName attribute in the
* session. For requests other than initialAction this class forces the user
* back to initialAction if the validSessionTokenName attribute is not present.
*/
public class SessionTimeoutRequestProcessor extends PortletTilesRequestProcessor {
private static Logger logger = Logger.getLogger(SessionTimeoutRequestProcessor.class);
/**
* If the request is not for the initialAction, verify that the attribute
* validSessionTokenName is in the session.
*
* @param request Current request
* @param response Current response
*/
@Override
public void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
SessionTimeoutConfigPlugin sessionTimeoutConfigPlugin =
(SessionTimeoutConfigPlugin) getServletContext().getAttribute(
SessionTimeoutConfigPlugin.PLUGIN_NAME_KEY);
String initialAction =
sessionTimeoutConfigPlugin.getInitialAction();
String validSessionTokenName =
sessionTimeoutConfigPlugin.getValidSessionTokenName();
if (isTimeout(request, initialAction, validSessionTokenName)) {
logger.info("Session timeout redirecting to: " + initialAction);
request.getRequestDispatcher(initialAction).forward(request, response);
} else {
super.process(request, response);
}
} catch (Exception ex) {
String msg = "Failed to set redirect. Message: " + ex.getMessage();
request.setAttribute("error", msg);
request.getRequestDispatcher("/error.shtml").forward(request, response);
}
}
// If the request is not the initialAction, check to see that the
// validSessionTokenName exists in the session
private boolean isTimeout(HttpServletRequest request, String initialAction,
String validSessionTokenName) {
boolean isTimeout = false;
String requestedAction = request.getRequestURI();
if (!requestedAction.contains(initialAction) &&
request.getSession().getAttribute(validSessionTokenName) == null) {
isTimeout = true;
}
return isTimeout;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment