diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/filters/AuthSetupForRequest.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/filters/AuthSetupForRequest.java index e07f42a04..45b85c4e9 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/filters/AuthSetupForRequest.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/filters/AuthSetupForRequest.java @@ -11,24 +11,15 @@ import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import edu.cornell.mannlib.vitro.webapp.auth.identifier.IdentifierBundle; -import edu.cornell.mannlib.vitro.webapp.auth.identifier.IdentifierBundleFactory; -import edu.cornell.mannlib.vitro.webapp.auth.identifier.ServletIdentifierBundleFactory; import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyList; import edu.cornell.mannlib.vitro.webapp.auth.policy.RequestPolicyList; import edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList; /** - * Setup an IdentifierBundle and PolicyList for the request and put it in the request scope. + * Setup a PolicyList for the request and put it in the request scope. * - * It expects to get the IdentifierBundleFactory from ServletIdentifierBundleFactory and - * PolicyList from ServletPolicyList; + * It expects to get the PolicyList from ServletPolicyList; * * @author bdc34 * @@ -36,30 +27,15 @@ import edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList; public class AuthSetupForRequest implements Filter { ServletContext context; - public void init(FilterConfig filterConfig) throws ServletException { + @Override + public void init(FilterConfig filterConfig) throws ServletException { context = filterConfig.getServletContext(); } - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { - - //get a factory that will convert Requests into Identifiers - IdentifierBundleFactory idbf = ServletIdentifierBundleFactory.getIdentifierBundleFactory(context); - - //try to get the session - HttpSession session = null; - if( servletRequest instanceof HttpServletRequest) - session = ((HttpServletRequest)servletRequest).getSession(false); - - //get Identifiers and stick in Request scope - try{ - if( idbf != null ){ - IdentifierBundle ib = idbf.getIdentifierBundle(servletRequest, session, context); - servletRequest.setAttribute(IDENTIFIER_BUNDLE, ib); - } - }catch(RuntimeException rx){ - log.warn("could not get Identifier Bundle",rx); - } - + @Override + public void doFilter(ServletRequest servletRequest, + ServletResponse servletResponse, FilterChain filterChain) + throws IOException, ServletException { //get the policies that are in effect for the context and add to Request Scope PolicyList plist = ServletPolicyList.getPolicies(context); servletRequest.setAttribute(RequestPolicyList.POLICY_LIST , plist); @@ -67,29 +43,6 @@ public class AuthSetupForRequest implements Filter { filterChain.doFilter(servletRequest, servletResponse); } - public void destroy() { } - - private static final Log log = LogFactory.getLog(AuthSetupForRequest.class); - private static final String IDENTIFIER_BUNDLE = "IdentifierBundle"; - - /* ************ static utility methods ********************* */ - public static IdentifierBundle getIdentifierBundle(HttpServletRequest req){ - if( req != null ) - return (IdentifierBundle)req.getAttribute(IDENTIFIER_BUNDLE); - else - return null; - } - - public static PolicyList getPolicyList( HttpServletRequest req){ - if( req != null ){ - HttpSession sess = req.getSession(false); - if( sess != null ){ - return (PolicyList)sess.getAttribute(RequestPolicyList.POLICY_LIST); - }else{ - return null; - } - }else{ - return null; - } - } + @Override + public void destroy() { /* Nothing to destroy */ } } \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/ActiveIdentifierBundleFactories.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/ActiveIdentifierBundleFactories.java new file mode 100644 index 000000000..1201e5970 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/ActiveIdentifierBundleFactories.java @@ -0,0 +1,122 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.auth.identifier; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +/** + * Keep a list of the active IdentifierBundleFactories in the context. + */ +public class ActiveIdentifierBundleFactories { + private static final String ATTRIBUTE_ACTIVE_FACTORIES = ActiveIdentifierBundleFactories.class + .getName(); + + // ---------------------------------------------------------------------- + // static methods + // ---------------------------------------------------------------------- + + /** + * Add a new IdentifierBundleFactory to the list. + */ + public static void addFactory(ServletContextEvent sce, + IdentifierBundleFactory factory) { + if (sce == null) { + throw new NullPointerException("sce may not be null."); + } + if (factory == null) { + throw new NullPointerException("factory may not be null."); + } + + addFactory(sce.getServletContext(), factory); + } + + /** + * Add a new IdentifierBundleFactory to the list. + */ + public static void addFactory(ServletContext ctx, + IdentifierBundleFactory factory) { + if (ctx == null) { + throw new NullPointerException("ctx may not be null."); + } + if (factory == null) { + throw new NullPointerException("factory may not be null."); + } + + getActiveFactories(ctx).addFactory(factory); + } + + /** + * Get the Identifiers from the list of factories. This might return an + * empty bundle, but it never returns null. + * + * This is package access, and should only be called by RequestIdentifiers. + * Everyone else should ask RequestIdentifiers to fetch them from the + * request. + */ + static IdentifierBundle getIdentifierBundle(HttpServletRequest request) { + HttpSession session = request.getSession(); + ServletContext ctx = session.getServletContext(); + return getActiveFactories(ctx).getIdentifierBundle(request, session, + ctx); + } + + /** + * Get the singleton instance from the servlet context. If there isn't one, + * create one. This never returns null. + */ + private static ActiveIdentifierBundleFactories getActiveFactories( + ServletContext ctx) { + if (ctx == null) { + throw new NullPointerException("ctx may not be null."); + } + + Object obj = ctx.getAttribute(ATTRIBUTE_ACTIVE_FACTORIES); + if (obj == null) { + obj = new ActiveIdentifierBundleFactories(); + ctx.setAttribute(ATTRIBUTE_ACTIVE_FACTORIES, obj); + } + + if (!(obj instanceof ActiveIdentifierBundleFactories)) { + throw new IllegalStateException("Expected to find an instance of " + + ActiveIdentifierBundleFactories.class.getName() + + " in the context, but found an instance of " + + obj.getClass().getName() + " instead."); + } + + return (ActiveIdentifierBundleFactories) obj; + } + + // ---------------------------------------------------------------------- + // the instance + // ---------------------------------------------------------------------- + + private final List factories = new ArrayList(); + + private void addFactory(IdentifierBundleFactory factory) { + factories.add(factory); + } + + /** + * Run through the active factories and get all Identifiers for this + * request. + */ + private IdentifierBundle getIdentifierBundle(HttpServletRequest request, + HttpSession session, ServletContext ctx) { + IdentifierBundle ib = new ArrayIdentifierBundle(); + for (IdentifierBundleFactory ibf : factories) { + IdentifierBundle obj = ibf.getIdentifierBundle(request, session, + ctx); + if (obj != null) { + ib.addAll(obj); + } + } + return ib; + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/RequestIdentifiers.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/RequestIdentifiers.java new file mode 100644 index 000000000..9eff6e6f2 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/RequestIdentifiers.java @@ -0,0 +1,49 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.auth.identifier; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; + +/** + * Build a list of Identifiers that apply to the current request and cache them + * in the request. + */ +public class RequestIdentifiers { + private static final String ATTRIBUTE_ID_BUNDLE = RequestIdentifiers.class + .getName(); + + // ---------------------------------------------------------------------- + // static methods + // ---------------------------------------------------------------------- + + /** + * If the currently applicable Identifiers have been cached in the request, + * get them. If not, assemble them from the active factories, and cache them + * in the request. + * + * This method might return an empty bundle, but it never returns null. + */ + public static IdentifierBundle getIdBundleForRequest(ServletRequest request) { + if (!(request instanceof HttpServletRequest)) { + return new ArrayIdentifierBundle(); + } + HttpServletRequest hreq = (HttpServletRequest) request; + + Object obj = hreq.getAttribute(ATTRIBUTE_ID_BUNDLE); + if (obj == null) { + obj = ActiveIdentifierBundleFactories.getIdentifierBundle(hreq); + hreq.setAttribute(ATTRIBUTE_ID_BUNDLE, obj); + } + + if (!(obj instanceof IdentifierBundle)) { + throw new IllegalStateException("Expected to find an instance of " + + IdentifierBundle.class.getName() + + " in the request, but found an instance of " + + obj.getClass().getName() + " instead."); + } + + return (IdentifierBundle) obj; + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/ServletIdentifierBundleFactory.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/ServletIdentifierBundleFactory.java deleted file mode 100644 index a46659bd9..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/auth/identifier/ServletIdentifierBundleFactory.java +++ /dev/null @@ -1,87 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ - -package edu.cornell.mannlib.vitro.webapp.auth.identifier; - -import java.util.ArrayList; -import java.util.Collections; - -import javax.servlet.ServletContext; -import javax.servlet.ServletRequest; -import javax.servlet.http.HttpSession; - -/** - * This class is intended to provide access to a IdentifierBundleFactory in the - * servlet context. - * - * @author bdc34 - * - */ -public class ServletIdentifierBundleFactory extends ArrayList implements IdentifierBundleFactory { - public static String IDENTIFIER_BUNDLE_FACTORY = "IdentifierBundleFactory"; - public static String IDENTIFIER_BUNDLE = "IdentifierBundle"; - - /* ****************** static utility methods *************************/ - - /** - * Use this method to get an IdentifierBundleFactory for the servlet. - * @param sc - * @return - */ - public static ServletIdentifierBundleFactory getIdentifierBundleFactory(ServletContext sc){ - if( sc != null ){ - Object obj = sc.getAttribute(IDENTIFIER_BUNDLE_FACTORY); - if( obj != null && obj instanceof ServletIdentifierBundleFactory ){ - return (ServletIdentifierBundleFactory)obj; - }else{ - ServletIdentifierBundleFactory sibf = new ServletIdentifierBundleFactory(); - sc.setAttribute(IDENTIFIER_BUNDLE_FACTORY, sibf); - return sibf; - } - }else{ - return null; - } - } - - /** - * Gets IdentifierBundle for a request. - * Session may be null. - */ - public static IdentifierBundle getIdBundleForRequest(ServletRequest request, HttpSession session, ServletContext sc){ - if( request == null ) return null; - IdentifierBundle ib = (IdentifierBundle)request.getAttribute(IDENTIFIER_BUNDLE); - if( ib != null ) return ib; - - IdentifierBundleFactory ibf = getIdentifierBundleFactory(sc); - ib = ibf.getIdentifierBundle(request,session,sc); - request.setAttribute(IDENTIFIER_BUNDLE, ib); - return ib; - } - - public static IdentifierBundle getExistingIdBundle(ServletRequest request){ - if( request == null ) return null; - IdentifierBundle ib = (IdentifierBundle)request.getAttribute(IDENTIFIER_BUNDLE); - if( ib != null ) return ib; - else - return null; - } - - public static void addIdentifierBundleFactory(ServletContext sc, IdentifierBundleFactory ibfToAdd){ - ServletIdentifierBundleFactory serverIbf = getIdentifierBundleFactory(sc); - serverIbf.add( ibfToAdd ); - } - - /** - * Consider using getIdBundleForRequest instead of this method. - */ - public IdentifierBundle getIdentifierBundle(ServletRequest request, HttpSession session, ServletContext context) { - IdentifierBundle ib = new ArrayIdentifierBundle(); - for(IdentifierBundleFactory ibf : this){ - if( ibf != null ){ - IdentifierBundle obj = ibf.getIdentifierBundle(request,session, context); - if( obj != null ) - ib.addAll( obj ); - } - } - return ib; - } -}