VIVO-769 Merge branch 'feature/orcid' into develop

Conflicts:
	webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/PrimitiveRdfEdit.java
	webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java
This commit is contained in:
Jim Blake 2014-05-07 17:31:44 -04:00
commit 2cd3f36db0
7 changed files with 89 additions and 28 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -4,7 +4,6 @@ package edu.cornell.mannlib.vitro.webapp.auth.permissions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,15 +19,19 @@ import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel;
import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
/** /**
* An immutable collection of Permission objects, keyed by URI. Resides in the * A collection of Permission objects, keyed by URI. Resides in the
* ServletContext. * ServletContext.
* *
* This is not thread-safe, so all Permissions should be added during context * This is not thread-safe, so Permissions should be added only during context
* initialization. * initialization.
*/ */
public class PermissionRegistry { public class PermissionRegistry {
private static final Log log = LogFactory.getLog(PermissionRegistry.class); private static final Log log = LogFactory.getLog(PermissionRegistry.class);
// ----------------------------------------------------------------------
// The factory
// ----------------------------------------------------------------------
private static final String ATTRIBUTE_NAME = PermissionRegistry.class private static final String ATTRIBUTE_NAME = PermissionRegistry.class
.getName(); .getName();
@ -55,7 +58,8 @@ public class PermissionRegistry {
"PermissionRegistry has already been set."); "PermissionRegistry has already been set.");
} }
PermissionRegistry registry = new PermissionRegistry(permissions); PermissionRegistry registry = new PermissionRegistry();
registry.addPermissions(permissions);
ctx.setAttribute(ATTRIBUTE_NAME, registry); ctx.setAttribute(ATTRIBUTE_NAME, registry);
} }
@ -80,26 +84,40 @@ public class PermissionRegistry {
return (PermissionRegistry) o; return (PermissionRegistry) o;
} }
private final Map<String, Permission> permissionsMap; // ----------------------------------------------------------------------
// The instance
// ----------------------------------------------------------------------
public PermissionRegistry(Collection<? extends Permission> permissions) { private final Map<String, Permission> map = new HashMap<>();
Map<String, Permission> map = new HashMap<String, Permission>();
/**
* This class is not thread-safe, so permissions should be added only during
* context initialization.
*/
public void addPermissions(Collection<? extends Permission> permissions) {
for (Permission p : permissions) { for (Permission p : permissions) {
String uri = p.getUri(); addPermission(p);
if (map.containsKey(uri)) {
throw new IllegalStateException("A Permission is already "
+ "registered with this URI: '" + uri + "'.");
}
map.put(uri, p);
} }
this.permissionsMap = Collections.unmodifiableMap(map); }
/**
* This class is not thread-safe, so permissions should be added only during
* context initialization.
*/
public void addPermission(Permission p) {
String uri = p.getUri();
if (map.containsKey(uri)) {
throw new IllegalStateException("A Permission is already "
+ "registered with this URI: '" + uri + "'.");
}
map.put(uri, p);
} }
/** /**
* Is there a Permission registered with this URI? * Is there a Permission registered with this URI?
*/ */
public boolean isPermission(String uri) { public boolean isPermission(String uri) {
return permissionsMap.containsKey(uri); return map.containsKey(uri);
} }
/** /**
@ -110,7 +128,7 @@ public class PermissionRegistry {
* this URI, call isPermission() instead. * this URI, call isPermission() instead.
*/ */
public Permission getPermission(String uri) { public Permission getPermission(String uri) {
Permission p = permissionsMap.get(uri); Permission p = map.get(uri);
if (p == null) { if (p == null) {
log.warn("No Permission is registered for '" + uri + "'"); log.warn("No Permission is registered for '" + uri + "'");
return new BrokenPermission(uri); return new BrokenPermission(uri);

View file

@ -37,6 +37,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessing
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ExceptionResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ExceptionResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ForwardResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ForwardResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.NotAuthorizedResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.RdfResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.RdfResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.RedirectResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.RedirectResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
@ -229,17 +230,19 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
response.setStatus(statusCode); response.setStatus(statusCode);
} }
if (values instanceof ExceptionResponseValues) { if (values instanceof NotAuthorizedResponseValues) {
doException(vreq, response, values); doNotAuthorized(vreq, response, (NotAuthorizedResponseValues)values);
} else if (values instanceof TemplateResponseValues) { } else if (values instanceof ExceptionResponseValues) {
doTemplate(vreq, response, values); doException(vreq, response, values);
} else if (values instanceof RedirectResponseValues) { } else if (values instanceof TemplateResponseValues) {
doRedirect(vreq, response, values); doTemplate(vreq, response, values);
} else if (values instanceof ForwardResponseValues) { } else if (values instanceof RedirectResponseValues) {
doForward(vreq, response, values); doRedirect(vreq, response, values);
} else if (values instanceof RdfResponseValues) { } else if (values instanceof ForwardResponseValues) {
doRdf(vreq, response, values); doForward(vreq, response, values);
} } else if (values instanceof RdfResponseValues) {
doRdf(vreq, response, values);
}
} catch (ServletException e) { } catch (ServletException e) {
log.error("ServletException in doResponse()", e); log.error("ServletException in doResponse()", e);
} catch (IOException e) { } catch (IOException e) {
@ -247,7 +250,15 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
} }
} }
protected void doTemplate(VitroRequest vreq, HttpServletResponse response, private void doNotAuthorized(VitroRequest vreq,
HttpServletResponse response, NotAuthorizedResponseValues values) {
// This method does a redirect if the required authorizations are
// not met (and they won't be), so just return.
isAuthorizedToDisplayPage(vreq, response, values.getUnauthorizedAction());
return;
}
protected void doTemplate(VitroRequest vreq, HttpServletResponse response,
ResponseValues values) throws TemplateProcessingException { ResponseValues values) throws TemplateProcessingException {
Map<String, Object> templateDataModel = new HashMap<String, Object>(); Map<String, Object> templateDataModel = new HashMap<String, Object>();
@ -548,4 +559,5 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
// to set up the data model. // to set up the data model.
new FreemarkerComponentGenerator(request); new FreemarkerComponentGenerator(request);
} }
} }

View file

@ -0,0 +1,31 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.RequestedAction;
/**
* This allows processRequest() in sub-classes of FreemarkerHttpServlet to
* decide that the request is not authorized, and properly handle the
* redirection.
*/
public class NotAuthorizedResponseValues extends BaseResponseValues {
/**
* If logging is turned on, this will be written to the log as a reason for
* rejecting the servlet.
*/
private final String logMessage;
public NotAuthorizedResponseValues(String logMessage) {
this.logMessage = logMessage;
}
public RequestedAction getUnauthorizedAction() {
return new RequestedAction() {
@Override
public String toString() {
return "Servlet not authorized: " + logMessage;
}
};
}
}