NIHVIVO-736 Move checkLoginStatus() methods from BaseEditController to VitroHttpServlet, so they can be more widely used.
This commit is contained in:
parent
6426cd9267
commit
58089feaf3
2 changed files with 101 additions and 86 deletions
|
@ -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());
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue