Author: michiel
Date: 2009-09-17 16:06:44 +0200 (Thu, 17 Sep 2009)
New Revision: 38669

Modified:
   
didactor/trunk/components/core/src/main/java/nl/didactor/filter/ProviderFilter.java
Log:
clenaed up a bit

Modified: 
didactor/trunk/components/core/src/main/java/nl/didactor/filter/ProviderFilter.java
===================================================================
--- 
didactor/trunk/components/core/src/main/java/nl/didactor/filter/ProviderFilter.java
 2009-09-17 13:41:34 UTC (rev 38668)
+++ 
didactor/trunk/components/core/src/main/java/nl/didactor/filter/ProviderFilter.java
 2009-09-17 14:06:44 UTC (rev 38669)
@@ -38,7 +38,7 @@
  */
 public class ProviderFilter implements Filter, MMBaseStarter, 
NodeEventListener, RelationEventListener {
     private static final Logger log = 
Logging.getLoggerInstance(ProviderFilter.class);
-    private static MMBase mmbase;
+    private MMBase mmbase;
 
 
     public static String USER_KEY = "nl.didactor.user_attributes";
@@ -100,7 +100,7 @@
      * Utility method. In didactor the current provider/education are 
determined using related url
      * objects to provider and education objects. This method does the needed 
queries.
      */
-    protected NodeList selectByRelatedUrl(Cloud cloud, NodeList nodes, String 
url) {
+    protected static NodeList selectByRelatedUrl(Cloud cloud, NodeList nodes, 
String url) {
         log.debug("Select for " + url);
         NodeList result = cloud.getCloudContext().createNodeList();
         NodeIterator ni = nodes.nodeIterator();
@@ -121,7 +121,7 @@
     }
 
 
-    protected NodeList selectForUser(Cloud cloud, NodeList nodes) {
+    protected static NodeList selectForUser(Cloud cloud, NodeList nodes) {
         int userNode = 
cloud.getCloudContext().getAuthentication().getNode(cloud.getUser());
         log.debug("Fitering for user " + cloud.getUser());
         if (cloud.hasNode(userNode)) {
@@ -145,7 +145,7 @@
     /**
      * With education, provider a certain locale is defined. Determin it.
      */
-    protected Locale findLocale(Node provider, Node education) {
+    protected static Locale findLocale(Node provider, Node education) {
         Locale locale;
         {
             String ls = provider.getStringValue("locale");
@@ -171,11 +171,11 @@
 
     }
 
-    protected String getSessionName() {
+    public static String getSessionName() {
         return "cloud_mmbase";
     }
 
-    protected Cloud getCloud(HttpServletRequest req) {
+    protected static Cloud getCloud(HttpServletRequest req) {
         HttpSession session = req.getSession(false); // false: do not create a 
session, only use it
         if (session == null) {
             return ContextProvider.getDefaultCloudContext().getCloud("mmbase");
@@ -208,7 +208,7 @@
      * @param provider If provider already determined, it can be given.
      * @param
      */
-    protected Node getEducation(Cloud cloud, Node provider, String[] urls) {
+    protected static Node getEducation(Cloud cloud, Node provider, String[] 
urls) {
         if (log.isDebugEnabled()) {
             log.debug("Finding education for " + provider.getNumber() + " " + 
Arrays.asList(urls));
         }
@@ -274,63 +274,10 @@
         NODE_KEYS.add("education");
         NODE_KEYS.add("provider");
     }
-    /**
-     * Filters the request: tries to find a jumper and redirects to this url 
when found, otherwise the
-     * request will be handled somewhere else in the filterchain.
-     * @param servletRequest The ServletRequest.
-     * @param servletResponse The ServletResponse.
-     * @param filterChain The FilterChain.
-     */
-    public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain filterChain) throws java.io.IOException, ServletException {
-        HttpServletResponse res = (HttpServletResponse) response;
-        HttpServletRequest req = (HttpServletRequest) request;
 
-        String sp = req.getServletPath();
-        if (sp.endsWith(".css") ||
-            sp.endsWith(".png") ||
-            sp.endsWith(".gif") ||
-            sp.endsWith(".js"))
-            {
-            filterChain.doFilter(request, response);
-            return;
-        }
-        if (mmbase == null || ! mmbase.getState()) {
-            // if mmbase not yet.503.
-            res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "MMBase 
not yet, or not successfully initialized (check mmbase log)");
-            return;
-        }
 
-        if (sp.startsWith("/images/") ||
-            sp.startsWith("/attachments/")) {
-            // no jsps here, these are blobs.
-            log.debug("No need to filter for " + sp);
-            filterChain.doFilter(request, response);
-            return;
-        }
-
-        boolean useParameters = true;
-        if(sp.startsWith("/mmbase/")) {
-            // all kind of generic jsp's can be there. reuest parameters can 
be used for something  else.
-            useParameters = false;
-        }
-
-
-        String serverName = req.getServerName();
-        String contextPath = req.getContextPath();
+    protected static Map<String, Serializable> getUserAttributes(Cloud cloud, 
HttpServletRequest req) {
         HttpSession session = req.getSession(false);
-
-        String parameterEducation = useParameters ? 
req.getParameter("education") : null;
-        if (parameterEducation != null && parameterEducation.length() == 0) 
parameterEducation = null;
-        if (parameterEducation == null && session != null) {
-            parameterEducation = (String) session.getAttribute(EDUCATION_KEY);
-            log.debug("education found from session " + parameterEducation);
-        }
-
-        String parameterProvider  = useParameters ? 
req.getParameter("provider") : null;
-
-        log.debug("Provider found from request " + parameterProvider);
-
-        final Cloud cloud = getCloud(req);
         Map<String, Serializable> userAttributes;
         if (session == null) {
             log.debug("no session");
@@ -347,11 +294,38 @@
                 }
             }
             int userNumber = 
cloud.getCloudContext().getAuthentication().getNode(cloud.getUser());
-            if (userNumber < 0) userNumber = 0;
-
+            if (userNumber < 0) {
+                userNumber = 0;
+            }
             userAttributes.put("user", userNumber);
+        }
+        return userAttributes;
+    }
+    public static boolean decorateRequest(HttpServletRequest req, 
HttpServletResponse res) throws IOException {
 
-            if (parameterEducation != null && useParameters && userNumber > 0) 
{
+        String serverName = req.getServerName();
+        String contextPath = req.getContextPath();
+        HttpSession session = req.getSession(false);
+
+        String parameterEducation = req.getParameter("education");
+        if (parameterEducation != null && parameterEducation.length() == 0) {
+            parameterEducation = null;
+        }
+        if (parameterEducation == null && session != null) {
+            parameterEducation = (String) session.getAttribute(EDUCATION_KEY);
+            log.debug("education found from session " + parameterEducation);
+        }
+
+        String parameterProvider  = req.getParameter("provider");
+
+        log.debug("Provider found from request " + parameterProvider);
+
+        final Cloud cloud = getCloud(req);
+
+        final Map<String, Serializable> userAttributes = 
getUserAttributes(cloud, req);
+        final int userNumber = (Integer) userAttributes.get("user");
+        if (session != null) {
+            if (parameterEducation != null && userNumber > 0) {
                 // remember some explicit education parameter in the session.
                 session.setAttribute(EDUCATION_KEY, parameterEducation);
             }
@@ -465,13 +439,11 @@
             }
 
             if (provider == null) {
-                if (req.getServletPath().startsWith("/mmbase")) {
-                    filterChain.doFilter(request, response);
-                } else {
-                    request.setAttribute("org.mmbase.servlet.error.message", 
"No provider found for '" + key + "'");
+                req.setAttribute("org.mmbase.servlet.error.message", "No 
provider found for '" + key + "'");
+                if (res != null) {
                     res.sendError(HttpServletResponse.SC_NOT_FOUND, "No 
provider found for '" + key + "'");
                 }
-                return;
+                return false;
 
             }
 
@@ -496,7 +468,7 @@
 
 
         // Based on the student and the education, try to find the class.
-        String c = useParameters ? request.getParameter("class") : null;
+        String c = req.getParameter("class");
         if (c != null) {
             if (cloud.hasNode(c)) {
                 userAttributes.put("class", cloud.getNode(c).getNumber());
@@ -520,7 +492,6 @@
 
             if (userClass == null) {
                 if (education != null) {
-                    int userNumber = (Integer) userAttributes.get("user");
                     if (userNumber > 0) {
                         try {
                             Node user = cloud.getNode(userNumber);
@@ -553,11 +524,11 @@
                     value = Casting.wrap(cloud.getNode("" + value), escaper);
                 }
             }
-            request.setAttribute(k, value);
+            req.setAttribute(k, value);
         }
-        
request.setAttribute(org.mmbase.framework.Framework.COMPONENT_INCLUDEPATH_KEY, 
request.getAttribute("includePath"));
+        
req.setAttribute(org.mmbase.framework.Framework.COMPONENT_INCLUDEPATH_KEY, 
req.getAttribute("includePath"));
 
-        assert request.getAttribute("provider") != null : "attributes" + 
attributes;
+        assert req.getAttribute("provider") != null : "attributes" + 
attributes;
         for (Map.Entry<String, Serializable> entry : 
userAttributes.entrySet()) {
             //log.info("Putting " + entry + " " + (entry.getValue() == null ? 
"" : entry.getValue().getClass()));
             //Casting.wrap(claz, escaper));
@@ -568,15 +539,66 @@
                     value = Casting.wrap(cloud.getNode("" + value), escaper);
                 }
             }
-            request.setAttribute(k, value);
+            req.setAttribute(k, value);
         }
+        return true;
 
-        if (request.getAttribute("includePath") == null) {
-            res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, 
"Didactor not yet initialized");
+    }
+
+    /**
+     * Filters the request: tries to find a jumper and redirects to this url 
when found, otherwise the
+     * request will be handled somewhere else in the filterchain.
+     * @param servletRequest The ServletRequest.
+     * @param servletResponse The ServletResponse.
+     * @param filterChain The FilterChain.
+     */
+    public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain filterChain) throws java.io.IOException, ServletException {
+        HttpServletResponse res = (HttpServletResponse) response;
+        HttpServletRequest req = (HttpServletRequest) request;
+
+        String sp = req.getServletPath();
+        if (sp.endsWith(".css") ||
+            sp.endsWith(".png") ||
+            sp.endsWith(".gif") ||
+            sp.endsWith(".js"))
+            {
+            filterChain.doFilter(request, response);
             return;
         }
 
-        filterChain.doFilter(request, response);
+        // if mmbase not yet.503.
+        if (mmbase == null) {
+            res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, 
this.getClass().getName() + ". MMBase not yet initialized (check mmbase log)");
+            return;
+        }
+        if (! mmbase.getState()) {
+            res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, 
this.getClass().getName() + ". MMBase not yet sucessfully initialized (check 
mmbase log)");
+            return;
+        }
+        if (sp.startsWith("/images/") ||
+            sp.startsWith("/attachments/")) {
+            // no jsps here, these are blobs.
+            log.debug("No need to filter for " + sp);
+            filterChain.doFilter(request, response);
+            return;
+        }
+
+
+        if(sp.startsWith("/mmbase/")) {
+            // all kind of generic jsp's can be there. reuest parameters can 
be used for something  else.
+            filterChain.doFilter(request, response);
+        } else {
+            if (decorateRequest(req, res)) {
+
+                if (request.getAttribute("includePath") == null) {
+                    res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, 
"Didactor not yet initialized");
+                    return;
+                }
+
+                filterChain.doFilter(request, response);
+            }
+        }
+
     }
 
 

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to