NIHVIVO-2211 Clean up the logic in ServletPolicyList and the classes that call it.

This commit is contained in:
jeb228 2011-03-09 21:49:38 +00:00
parent b162de36d2
commit 1c9b125800
9 changed files with 113 additions and 113 deletions

View file

@ -33,7 +33,6 @@ log4j.appender.AllAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}
log4j.rootLogger=INFO, AllAppender log4j.rootLogger=INFO, AllAppender
log4j.logger.edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList=WARN
log4j.logger.edu.cornell.mannlib.vitro.webapp.controller.freemarker.BrowseController=WARN log4j.logger.edu.cornell.mannlib.vitro.webapp.controller.freemarker.BrowseController=WARN
log4j.logger.edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener=WARN log4j.logger.edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener=WARN
log4j.logger.edu.cornell.mannlib.vitro.webapp.dao.jena.RDBGraphGenerator=WARN log4j.logger.edu.cornell.mannlib.vitro.webapp.dao.jena.RDBGraphGenerator=WARN

View file

@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletResponse;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.IdentifierBundle; import edu.cornell.mannlib.vitro.webapp.auth.identifier.IdentifierBundle;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.RequestIdentifiers; import edu.cornell.mannlib.vitro.webapp.auth.identifier.RequestIdentifiers;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyList;
import edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList; import edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList;
import edu.cornell.mannlib.vitro.webapp.auth.policy.ifaces.PolicyDecision; import edu.cornell.mannlib.vitro.webapp.auth.policy.ifaces.PolicyDecision;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAction; import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAction;
@ -59,11 +60,11 @@ public class AuthTestController extends VitroHttpServlet {
private void checkAuths(ServletOutputStream out, IdentifierBundle ids, ServletContext servletContext) private void checkAuths(ServletOutputStream out, IdentifierBundle ids, ServletContext servletContext)
throws IOException{ throws IOException{
ServletPolicyList policy = ServletPolicyList.getPolicies(servletContext); PolicyList policy = ServletPolicyList.getPolicies(servletContext);
out.println("<h1>Authorization tests:</h1>"); out.println("<h1>Authorization tests:</h1>");
if( policy == null ) { out.println("No Policy objects found in ServletContext. "); if (policy.isEmpty()) {
out.println("No Policy objects found in ServletContext. ");
} }
out.println("<table>"); out.println("<table>");
for(RequestedAction action: actions){ for(RequestedAction action: actions){

View file

@ -51,11 +51,6 @@ public class AuthorizationHelper {
PolicyIface policy = RequestPolicyList.getPolicies(vreq); PolicyIface policy = RequestPolicyList.getPolicies(vreq);
if (isEmptyPolicy(policy)) { if (isEmptyPolicy(policy)) {
policy = ServletPolicyList.getPolicies(servletContext); policy = ServletPolicyList.getPolicies(servletContext);
if (isEmptyPolicy(policy)) {
log.error("No policy found in request at "
+ RequestPolicyList.POLICY_LIST);
policy = new PolicyList();
}
} }
return policy; return policy;

View file

@ -375,9 +375,7 @@ public class JenaNetidPolicy extends DefaultInconclusivePolicy implements Visiti
log.error("could not get jenaOntModel from JenaBaseDao, JenaNetidPolicy will not work"); log.error("could not get jenaOntModel from JenaBaseDao, JenaNetidPolicy will not work");
} }
JenaNetidPolicy jnip = new JenaNetidPolicy(model); ServletPolicyList.addPolicy(sce.getServletContext(), new JenaNetidPolicy(model));
ServletPolicyList spl = ServletPolicyList.getPolicies(sce.getServletContext());
spl.add(jnip);
ActiveIdentifierBundleFactories.addFactory(sce, new SelfEditingIdentifierFactory()); ActiveIdentifierBundleFactories.addFactory(sce, new SelfEditingIdentifierFactory());
}catch(Exception e){ }catch(Exception e){

View file

@ -3,6 +3,7 @@
package edu.cornell.mannlib.vitro.webapp.auth.policy; package edu.cornell.mannlib.vitro.webapp.auth.policy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -22,9 +23,7 @@ import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAct
* or null decisions will be ignored and the next policy on the list will * or null decisions will be ignored and the next policy on the list will
* be queried. * be queried.
* *
*
* @author bdc34 * @author bdc34
*
*/ */
public class PolicyList extends ArrayList<PolicyIface> implements PolicyIface{ public class PolicyList extends ArrayList<PolicyIface> implements PolicyIface{
private static final Log log = LogFactory.getLog(PolicyList.class.getName()); private static final Log log = LogFactory.getLog(PolicyList.class.getName());
@ -33,6 +32,11 @@ public class PolicyList extends ArrayList<PolicyIface> implements PolicyIface{
super(); super();
} }
public PolicyList(Collection<PolicyIface> policies) {
super(policies);
}
@Override
public PolicyDecision isAuthorized(IdentifierBundle whoToAuth, RequestedAction whatToAuth) { public PolicyDecision isAuthorized(IdentifierBundle whoToAuth, RequestedAction whatToAuth) {
PolicyDecision pd = null; PolicyDecision pd = null;
for(PolicyIface policy : this){ for(PolicyIface policy : this){
@ -43,12 +47,11 @@ public class PolicyList extends ArrayList<PolicyIface> implements PolicyIface{
break; break;
if( pd.getAuthorized() == Authorization.UNAUTHORIZED ) if( pd.getAuthorized() == Authorization.UNAUTHORIZED )
break; break;
// if( pd.getAuthorized() == Authorization.INCONCLUSIVE ) // if( pd.getAuthorized() == Authorization.INCONCLUSIVE )
// continue; // continue;
} else{ } else{
log.debug("policy " + policy.toString() + " returned a null PolicyDecision"); log.debug("policy " + policy.toString() + " returned a null PolicyDecision");
} }
}catch(Throwable th){ }catch(Throwable th){
log.error("ignoring exception in policy " + policy.toString(), th ); log.error("ignoring exception in policy " + policy.toString(), th );
} }

View file

@ -11,94 +11,105 @@ import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.auth.policy.ifaces.PolicyIface; import edu.cornell.mannlib.vitro.webapp.auth.policy.ifaces.PolicyIface;
/** /**
* This is a PolicyList that can do isAuthorized and stashes a singleton * This maintains a PolicyList in the ServletContext. As a rule, however, this
* in the ServletContext. * is only used as the basis for the RequestPolicyList. Client code that wants
* * to access the current list of policies should look there.
* The intent of this class is to allow a single point for policies
* in a ServletContext. example:
* <code>
* Authorization canIDoIt = ServletPolicyList.getPolicies( getServletContext() ).isAuthorized( IdBundle, action );
* </code>
*
* @author bdc34
*
*/ */
public class ServletPolicyList extends PolicyList { public class ServletPolicyList {
protected static String POLICY_LIST = "policy_list"; private static final String ATTRIBUTE_POLICY_LIST = ServletPolicyList.class.getName();
private static final Log log = LogFactory.getLog(ServletPolicyList.class.getName()); private static final Log log = LogFactory.getLog(ServletPolicyList.class);
/** /**
* This is for general public use to get a list of policies for the ServletContext. * Get a copy of the current list of policies. This method may return an
* @param sc * empty list, but it never returns null.
* @return
*/ */
@SuppressWarnings("unchecked") public static PolicyList getPolicies(ServletContext sc) {
public static ServletPolicyList getPolicies(ServletContext sc){ return new PolicyList(getPolicyList(sc));
ServletPolicyList list = null;
try{
list = (ServletPolicyList)sc.getAttribute(POLICY_LIST);
}catch(ClassCastException cce){
log.error(POLICY_LIST +" server context attribute was not of type List<PolicyIface>");
}
if( list == null ){
list = new ServletPolicyList();
sc.setAttribute(POLICY_LIST, list);
}
return list;
}
public static void addPolicy(ServletContext sc, PolicyIface policy){
ServletPolicyList policies = getPolicies(sc);
if( !policies.contains(policy) ){
policies.add(policy);
log.info("Added policy: " + policy.toString());
}else{
log.info("Ignored attempt to add redundent policy.");
}
} }
/** /**
* This adds the policy to the front of the list but it may be moved further down * Add the policy to the end of the list.
* the list by other policies that are later added using this method.
*/ */
public static void addPolicyAtFront(ServletContext sc, PolicyIface policy){ public static void addPolicy(ServletContext sc, PolicyIface policy) {
ServletPolicyList policies = getPolicies(sc); if (policy == null) {
if( !policies.contains(policy) ){
policies.add(0,policy);
log.info("Added policy at front of ServletPolicyList: " + policy.toString());
}else{
log.info("Ignored attempt to add redundent policy.");
}
}
/** import edu.cornell.mannlib.vitro.webapp.auth.policy.ifaces.VisitingPolicyIface;
* Replace first instance of policy found in policy list. If no instance
* is found in list add at end of the list.
*
* @param sc
* @param policy
*/
public static void replacePolicy(ServletContext sc, PolicyIface policy){
if( sc == null )
throw new IllegalArgumentException( "replacePolicy() needs a non-null ServletContext");
if( policy == null )
return; return;
Class clzz = policy.getClass(); }
ServletPolicyList spl = ServletPolicyList.getPolicies(sc); PolicyList policies = getPolicyList(sc);
ListIterator<PolicyIface> it = spl.listIterator(); if (!policies.contains(policy)) {
boolean replaced = false; policies.add(policy);
while(it.hasNext()){ log.info("Added policy: " + policy.getClass().getSimpleName());
PolicyIface p = (PolicyIface)it.next(); log.debug("Added policy: " + policy.toString());
if( clzz.isAssignableFrom(p.getClass()) ){ } else {
it.set( policy ); log.warn("Ignored attempt to add redundant policy.");
replaced = true;
} }
} }
if( ! replaced ){
ServletPolicyList.addPolicy(sc, policy); /**
* Add the policy to the front of the list. It may be moved further down the
* list by other policies that are later added using this method.
*/
public static void addPolicyAtFront(ServletContext sc, PolicyIface policy) {
if (policy == null) {
return;
}
PolicyList policies = getPolicyList(sc);
if (!policies.contains(policy)) {
policies.add(0, policy);
log.info("Added policy at front: " + policy.getClass().getSimpleName());
log.debug("Added policy at front: " + policy.toString());
} else {
log.warn("Ignored attempt to add redundant policy.");
} }
} }
/**
* Replace the first instance of this class of policy in the list. If no
* instance is found, add the policy to the end of the list.
*/
public static void replacePolicy(ServletContext sc, PolicyIface policy) {
if (policy == null) {
return;
}
Class<?> clzz = policy.getClass();
PolicyList policies = getPolicyList(sc);
ListIterator<PolicyIface> it = policies.listIterator();
while (it.hasNext()) {
if (clzz.isAssignableFrom(it.next().getClass())) {
it.set(policy);
return;
}
}
addPolicy(sc, policy);
}
/**
* Get the current PolicyList from the context, or create one if there is
* none. This method may return an empty list, but it never returns null.
*/
private static PolicyList getPolicyList(ServletContext ctx) {
if (ctx == null) {
throw new NullPointerException("ctx may not be null.");
}
Object obj = ctx.getAttribute(ATTRIBUTE_POLICY_LIST);
if (obj == null) {
obj = new PolicyList();
ctx.setAttribute(ATTRIBUTE_POLICY_LIST, obj);
}
if (!(obj instanceof PolicyList)) {
throw new IllegalStateException("Expected to find an instance of "
+ PolicyList.class.getName()
+ " in the context, but found an instance of "
+ obj.getClass().getName() + " instead.");
}
return (PolicyList) obj;
}
} }

View file

@ -93,10 +93,6 @@ public class PropertyEditLinks extends TagSupport{
PolicyIface policy = RequestPolicyList.getPolicies(pageContext.getRequest()); PolicyIface policy = RequestPolicyList.getPolicies(pageContext.getRequest());
if( policy == null || ( policy instanceof PolicyList && ((PolicyList)policy).size() == 0 )){ if( policy == null || ( policy instanceof PolicyList && ((PolicyList)policy).size() == 0 )){
policy = ServletPolicyList.getPolicies( pageContext.getServletContext() ); policy = ServletPolicyList.getPolicies( pageContext.getServletContext() );
if( policy == null || ( policy instanceof PolicyList && ((PolicyList)policy).size() == 0 )){
log.error("No policy found in request at " + RequestPolicyList.POLICY_LIST);
return SKIP_BODY;
}
} }
IdentifierBundle ids = RequestIdentifiers.getIdBundleForRequest(pageContext.getRequest()); IdentifierBundle ids = RequestIdentifiers.getIdBundleForRequest(pageContext.getRequest());

View file

@ -38,9 +38,6 @@ public class EditingPolicyHelper {
policy = RequestPolicyList.getPolicies(vreq); policy = RequestPolicyList.getPolicies(vreq);
if( policy == null || ( policy instanceof PolicyList && ((PolicyList)policy).size() == 0 )){ if( policy == null || ( policy instanceof PolicyList && ((PolicyList)policy).size() == 0 )){
policy = ServletPolicyList.getPolicies( servletContext ); policy = ServletPolicyList.getPolicies( servletContext );
if( policy == null || ( policy instanceof PolicyList && ((PolicyList)policy).size() == 0 )){
log.error("No policy found in request at " + RequestPolicyList.POLICY_LIST);
}
} }
} }

View file

@ -28,7 +28,7 @@
<h3>Is there a self editing policy in the context?</h3> <h3>Is there a self editing policy in the context?</h3>
<% <%
ServletPolicyList spl = ServletPolicyList.getPolicies(application); PolicyList spl = ServletPolicyList.getPolicies(application);
SelfEditingPolicy sePolicy = null; SelfEditingPolicy sePolicy = null;
ListIterator it = spl.listIterator(); ListIterator it = spl.listIterator();
String found = "Could not find a SelfEditingPolicy"; String found = "Could not find a SelfEditingPolicy";