NIHVIVO-736 Move checkLoginStatus() methods from BaseEditController to VitroHttpServlet, so they can be more widely used.

This commit is contained in:
jeb228 2010-10-14 14:59:32 +00:00
parent 6426cd9267
commit 58089feaf3
2 changed files with 101 additions and 86 deletions

View file

@ -2,7 +2,6 @@
package edu.cornell.mannlib.vedit.controller; package edu.cornell.mannlib.vedit.controller;
import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
@ -15,7 +14,6 @@ import java.util.Random;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -23,7 +21,6 @@ import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModel;
import edu.cornell.mannlib.vedit.beans.EditProcessObject; 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.vedit.util.FormUtils;
import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers; 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 String EPO_KEYLIST_ATTR = "epoKeylist";
private final int MAX_EPOS = 5; private final int MAX_EPOS = 5;
private final Calendar cal = Calendar.getInstance(); 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. /* 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. */ 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()); 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){ protected void setRequestAttributes(HttpServletRequest request, EditProcessObject epo){
Portal portal = (Portal)request.getAttribute("portalBean"); Portal portal = (Portal)request.getAttribute("portalBean");
request.setAttribute("epoKey",epo.getKey()); request.setAttribute("epoKey",epo.getKey());

View file

@ -14,50 +14,111 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
public class VitroHttpServlet extends HttpServlet import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
{
private static final long serialVersionUID = 1L;
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"; private static final Log log = LogFactory.getLog(VitroHttpServlet.class
public final static String HTML_MIMETYPE ="text/html"; .getName());
public final static String RDFXML_MIMETYPE ="application/rdf+xml"; public final static String XHTML_MIMETYPE = "application/xhtml+xml";
public final static String N3_MIMETYPE ="text/n3"; //unofficial and unregistered public final static String HTML_MIMETYPE = "text/html";
public final static String TTL_MIMETYPE = "text/turtle"; //unofficial and unregistered
/** public final static String RDFXML_MIMETYPE = "application/rdf+xml";
* Setup the auth flag, portal flag and portal bean objects. public final static String N3_MIMETYPE = "text/n3"; // unofficial and
* Put them in the request attributes. // unregistered
*/ public final static String TTL_MIMETYPE = "text/turtle"; // unofficial and
@Override // unregistered
protected void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
setup(request);
}
protected final void setup(HttpServletRequest request) { /**
* 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);
}
//check to see if VitroRequestPrep filter was run protected final void setup(HttpServletRequest request) {
if( request.getAttribute("appBean") == null ||
request.getAttribute("webappDaoFactory") == null ){
log.warn("request scope was not prepared by VitroRequestPrep");
}
}
// 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 * doPost does the same thing as the doGet method
*/ */
@Override @Override
protected void doPost( HttpServletRequest request, HttpServletResponse response ) protected void doPost(HttpServletRequest request,
throws ServletException, IOException HttpServletResponse response) throws ServletException, IOException {
{ doGet(request, response);
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);
}
} }