From 58089feaf3a80833c47a8c7c6e65270b487a16a7 Mon Sep 17 00:00:00 2001 From: jeb228 Date: Thu, 14 Oct 2010 14:59:32 +0000 Subject: [PATCH] NIHVIVO-736 Move checkLoginStatus() methods from BaseEditController to VitroHttpServlet, so they can be more widely used. --- .../vedit/controller/BaseEditController.java | 46 ------ .../webapp/controller/VitroHttpServlet.java | 141 +++++++++++++----- 2 files changed, 101 insertions(+), 86 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vedit/controller/BaseEditController.java b/webapp/src/edu/cornell/mannlib/vedit/controller/BaseEditController.java index 79a74cde5..be83a7eb5 100644 --- a/webapp/src/edu/cornell/mannlib/vedit/controller/BaseEditController.java +++ b/webapp/src/edu/cornell/mannlib/vedit/controller/BaseEditController.java @@ -2,7 +2,6 @@ package edu.cornell.mannlib.vedit.controller; -import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -15,7 +14,6 @@ import java.util.Random; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -23,7 +21,6 @@ import org.apache.commons.logging.LogFactory; import com.hp.hpl.jena.ontology.OntModel; import edu.cornell.mannlib.vedit.beans.EditProcessObject; -import edu.cornell.mannlib.vedit.beans.LoginStatusBean; import edu.cornell.mannlib.vedit.util.FormUtils; import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.controller.Controllers; @@ -44,7 +41,6 @@ public class BaseEditController extends VitroHttpServlet { private final String EPO_KEYLIST_ATTR = "epoKeylist"; private final int MAX_EPOS = 5; private final Calendar cal = Calendar.getInstance(); - private final Random rand = new Random(cal.getTimeInMillis()); /* EPO is reused if the controller is passed an epoKey, e.g. if a previous form submission failed validation, or the edit is a multistage process. */ @@ -110,48 +106,6 @@ public class BaseEditController extends VitroHttpServlet { return Long.toHexString(cal.getTimeInMillis()); } - /** - * If not logged in, send them to the login page. - */ - protected boolean checkLoginStatus(HttpServletRequest request, - HttpServletResponse response) { - if (LoginStatusBean.getBean(request).isLoggedIn()) { - return true; - } else { - redirectToLoginPage(request, response); - return false; - } - } - - /** - * If not logged in at the minimum level or higher, send them to the login page. - */ - protected boolean checkLoginStatus(HttpServletRequest request, - HttpServletResponse response, int minimumLevel) { - if (LoginStatusBean.getBean(request).isLoggedInAtLeast(minimumLevel)) { - return true; - } else { - redirectToLoginPage(request, response); - return false; - } - } - - /** - * Not adequately logged in. Send them to the login page, and then back to - * the page that invoked this. - */ - private void redirectToLoginPage(HttpServletRequest request, - HttpServletResponse response) { - request.getSession().setAttribute("postLoginRequest", - request.getRequestURI() + "?" + request.getQueryString()); - try { - String loginPage = request.getContextPath() + Controllers.LOGIN; - response.sendRedirect(loginPage); - } catch (IOException ioe) { - log.error("checkLoginStatus() could not redirect to login page"); - } - } - protected void setRequestAttributes(HttpServletRequest request, EditProcessObject epo){ Portal portal = (Portal)request.getAttribute("portalBean"); request.setAttribute("epoKey",epo.getKey()); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/VitroHttpServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/VitroHttpServlet.java index 82b3374f0..80d3f04a7 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/VitroHttpServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/VitroHttpServlet.java @@ -14,50 +14,111 @@ import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -public class VitroHttpServlet extends HttpServlet -{ - private static final long serialVersionUID = 1L; +import edu.cornell.mannlib.vedit.beans.LoginStatusBean; - protected static DateFormat publicDateFormat = new SimpleDateFormat("M/dd/yyyy"); +public class VitroHttpServlet extends HttpServlet { + private static final long serialVersionUID = 1L; - private static final Log log = LogFactory.getLog(VitroHttpServlet.class.getName()); + protected static DateFormat publicDateFormat = new SimpleDateFormat( + "M/dd/yyyy"); - public final static String XHTML_MIMETYPE ="application/xhtml+xml"; - public final static String HTML_MIMETYPE ="text/html"; - - public final static String RDFXML_MIMETYPE ="application/rdf+xml"; - public final static String N3_MIMETYPE ="text/n3"; //unofficial and unregistered - public final static String TTL_MIMETYPE = "text/turtle"; //unofficial and unregistered - - /** - * Setup the auth flag, portal flag and portal bean objects. - * Put them in the request attributes. - */ - @Override - protected void doGet( HttpServletRequest request, HttpServletResponse response ) - throws ServletException, IOException - { - setup(request); - } + private static final Log log = LogFactory.getLog(VitroHttpServlet.class + .getName()); - protected final void setup(HttpServletRequest request) { - - //check to see if VitroRequestPrep filter was run - if( request.getAttribute("appBean") == null || - request.getAttribute("webappDaoFactory") == null ){ - log.warn("request scope was not prepared by VitroRequestPrep"); - } - } - + public final static String XHTML_MIMETYPE = "application/xhtml+xml"; + public final static String HTML_MIMETYPE = "text/html"; - /** - * doPost does the same thing as the doGet method - */ - @Override - protected void doPost( HttpServletRequest request, HttpServletResponse response ) - throws ServletException, IOException - { - doGet( request,response ); - } + public final static String RDFXML_MIMETYPE = "application/rdf+xml"; + public final static String N3_MIMETYPE = "text/n3"; // unofficial and + // unregistered + public final static String TTL_MIMETYPE = "text/turtle"; // unofficial and + // unregistered + + /** + * Setup the auth flag, portal flag and portal bean objects. Put them in the + * request attributes. + */ + @Override + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + setup(request); + } + + protected final void setup(HttpServletRequest request) { + + // check to see if VitroRequestPrep filter was run + if (request.getAttribute("appBean") == null + || request.getAttribute("webappDaoFactory") == null) { + log.warn("request scope was not prepared by VitroRequestPrep"); + } + } + + /** + * doPost does the same thing as the doGet method + */ + @Override + protected void doPost(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + doGet(request, response); + } + + // ---------------------------------------------------------------------- + // static utility methods for all Vitro servlets + // ---------------------------------------------------------------------- + + /** + * If not logged in, send them to the login page. + */ + public static boolean checkLoginStatus(HttpServletRequest request, + HttpServletResponse response) { + if (LoginStatusBean.getBean(request).isLoggedIn()) { + return true; + } else { + try { + redirectToLoginPage(request, response); + } catch (IOException ioe) { + log.error("checkLoginStatus() could not redirect to login page"); + } + return false; + } + } + + /** + * If not logged in at the minimum level or higher, send them to the login + * page. + */ + public static boolean checkLoginStatus(HttpServletRequest request, + HttpServletResponse response, int minimumLevel) { + if (LoginStatusBean.getBean(request).isLoggedInAtLeast(minimumLevel)) { + return true; + } else { + try { + redirectToLoginPage(request, response); + } catch (IOException ioe) { + log.error("checkLoginStatus() could not redirect to login page"); + } + return false; + } + } + + /** + * Not adequately logged in. Send them to the login page, and then back to + * the page that invoked this. + */ + public static void redirectToLoginPage(HttpServletRequest request, + HttpServletResponse response) throws IOException { + String postLoginRequest; + + String queryString = request.getQueryString(); + if ((queryString == null) || queryString.isEmpty()) { + postLoginRequest = request.getRequestURI(); + } else { + postLoginRequest = request.getRequestURI() + "?" + queryString; + } + + request.getSession().setAttribute("postLoginRequest", postLoginRequest); + String loginPage = request.getContextPath() + Controllers.LOGIN; + response.sendRedirect(loginPage); + } }