incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r901121 - in /sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl: AuthenticationRequirementHolder.java AuthenticatorWebConsolePlugin.java SlingAuthenticator.java
Date Wed, 20 Jan 2010 10:29:40 GMT
Author: fmeschbe
Date: Wed Jan 20 10:29:40 2010
New Revision: 901121

URL: http://svn.apache.org/viewvc?rev=901121&view=rev
Log:
SLING-1300 Make sure authentication requirements of registered services are not lost due to
dynamic reconfiguration. Also extended the internal storage to be able to list information
on who provided the requirement.

Modified:
    sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticationRequirementHolder.java
    sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticatorWebConsolePlugin.java
    sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java

Modified: sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticationRequirementHolder.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticationRequirementHolder.java?rev=901121&r1=901120&r2=901121&view=diff
==============================================================================
--- sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticationRequirementHolder.java
(original)
+++ sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticationRequirementHolder.java
Wed Jan 20 10:29:40 2010
@@ -18,11 +18,14 @@
  */
 package org.apache.sling.commons.auth.impl;
 
-public class AuthenticationRequirementHolder extends PathBasedHolder {
+class AuthenticationRequirementHolder extends PathBasedHolder {
 
     private final boolean requiresAuthentication;
 
-    static AuthenticationRequirementHolder fromConfig(final String config) {
+    private final String source;
+
+    static AuthenticationRequirementHolder fromConfig(final String config,
+            final String source) {
         if (config == null || config.length() == 0) {
             throw new IllegalArgumentException(
                 "Configuration must not be null or empty");
@@ -41,16 +44,21 @@
             path = config;
         }
 
-        return new AuthenticationRequirementHolder(path, required);
+        return new AuthenticationRequirementHolder(path, required, source);
     }
 
-    protected AuthenticationRequirementHolder(final String fullPath,
-            final boolean requiresAuthentication) {
+    AuthenticationRequirementHolder(final String fullPath,
+            final boolean requiresAuthentication, final String source) {
         super(fullPath);
         this.requiresAuthentication = requiresAuthentication;
+        this.source = source;
     }
 
-    public boolean requiresAuthentication() {
+    boolean requiresAuthentication() {
         return requiresAuthentication;
     }
+
+    String getSource() {
+        return source;
+    }
 }

Modified: sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticatorWebConsolePlugin.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticatorWebConsolePlugin.java?rev=901121&r1=901120&r2=901121&view=diff
==============================================================================
--- sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticatorWebConsolePlugin.java
(original)
+++ sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/AuthenticatorWebConsolePlugin.java
Wed Jan 20 10:29:40 2010
@@ -73,11 +73,11 @@
 
     private void printAuthenticationHandler(PrintWriter pw) {
         pw.println("<tr>");
-        pw.println("<th class='content container' colspan='2'>Registered Authentication
Handler</td>");
+        pw.println("<th class='content container' colspan='3'>Registered Authentication
Handler</td>");
         pw.println("</tr>");
         pw.println("<tr>");
         pw.println("<th class='content'>Path</td>");
-        pw.println("<th class='content'>Handler</td>");
+        pw.println("<th class='content' colspan='2'>Handler</td>");
         pw.println("</tr>");
 
         ArrayList<AbstractAuthenticationHandlerHolder> holderList = slingAuthenticator.getAuthenticationHandler();
@@ -85,7 +85,7 @@
 
             pw.println("<tr class='content'>");
             pw.println("<td class='content'>" + handler.fullPath + "</td>");
-            pw.println("<td class='content'>" + handler + "</td>");
+            pw.println("<td class='content' colspan='2'>" + handler + "</td>");
             pw.println("</tr>");
 
         }
@@ -93,11 +93,12 @@
 
     private void printAuthenticationRequirements(PrintWriter pw) {
         pw.println("<tr>");
-        pw.println("<th class='content container' colspan='2'>Authentication Requirement
Configuration</td>");
+        pw.println("<th class='content container' colspan='3'>Authentication Requirement
Configuration</td>");
         pw.println("</tr>");
         pw.println("<tr>");
         pw.println("<th class='content'>Path</td>");
         pw.println("<th class='content'>Authentication Required</td>");
+        pw.println("<th class='content'>Defining Service (Description or ID)</td>");
         pw.println("</tr>");
 
         ArrayList<AuthenticationRequirementHolder> holderList = slingAuthenticator.getAuthenticationRequirements();
@@ -107,6 +108,7 @@
             pw.println("<td class='content'>" + req.fullPath + "</td>");
             pw.println("<td class='content'>"
                 + (req.requiresAuthentication() ? "Yes" : "No") + "</td>");
+            pw.println("<td class='content'>" + req.getSource() + "</td>");
             pw.println("</tr>");
 
         }

Modified: sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java?rev=901121&r1=901120&r2=901121&view=diff
==============================================================================
--- sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java
(original)
+++ sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java
Wed Jan 20 10:29:40 2010
@@ -51,7 +51,6 @@
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.http.HttpContext;
@@ -87,7 +86,6 @@
  * @scr.service interface="org.apache.sling.commons.auth.Authenticator"
  * @scr.service interface="javax.servlet.ServletRequestListener"
  *
- * @scr.property name="service.description" value="Sling Request Authenticator"
  * @scr.property name="service.vendor" value="The Apache Software Foundation"
  *
  * @scr.reference name="authHandler"
@@ -105,6 +103,9 @@
     /** default log */
     private final Logger log = LoggerFactory.getLogger(SlingAuthenticator.class);
 
+    /** @scr.property name="service.description" */
+    private static final String DESCRIPTION = "Sling Request Authenticator";
+
     /**
      * @scr.property valueRef="DEFAULT_IMPERSONATION_COOKIE"
      */
@@ -152,7 +153,6 @@
      */
     private static final String REQUEST_ATTRIBUTE_SESSION = "javax.jcr.Session";
 
-
     private static ArrayList<AbstractAuthenticationHandlerHolder> EMPTY_INFO = new
ArrayList<AbstractAuthenticationHandlerHolder>();
 
     /** @scr.reference */
@@ -182,7 +182,7 @@
      * The listener for services registered with "sling.auth.requirements" to
      * update the internal authentication requirements
      */
-    private ServiceListener serviceListener;
+    private SlingAuthenticatorServiceListener serviceListener;
 
     // ---------- SCR integration
 
@@ -239,22 +239,28 @@
         boolean flag = OsgiUtil.toBoolean(
             properties.get(PAR_ANONYMOUS_ALLOWED), DEFAULT_ANONYMOUS_ALLOWED);
         authRequiredCache.addHolder(new AuthenticationRequirementHolder("/",
-            !flag));
+            !flag, DESCRIPTION));
 
         String[] authReqs = OsgiUtil.toStringArray(properties.get(PAR_AUTH_REQ));
         if (authReqs != null) {
             for (String authReq : authReqs) {
                 if (authReq != null && authReq.length() > 0) {
-                    authRequiredCache.addHolder(AuthenticationRequirementHolder.fromConfig(authReq));
+                    authRequiredCache.addHolder(AuthenticationRequirementHolder.fromConfig(
+                        authReq, DESCRIPTION));
                 }
             }
         }
 
         // don't require authentication for login/logout servlets
         authRequiredCache.addHolder(new AuthenticationRequirementHolder(
-            LoginServlet.SERVLET_PATH, false));
+            LoginServlet.SERVLET_PATH, false, DESCRIPTION));
         authRequiredCache.addHolder(new AuthenticationRequirementHolder(
-            LogoutServlet.SERVLET_PATH, false));
+            LogoutServlet.SERVLET_PATH, false, DESCRIPTION));
+
+        // add all registered services
+        if (serviceListener != null) {
+            serviceListener.registerServices();
+        }
     }
 
     @SuppressWarnings("unused")
@@ -1054,16 +1060,18 @@
 
         private final SlingAuthenticator authenticator;
 
-        private final HashMap<Object, String[]> props = new HashMap<Object, String[]>();
+        private final HashMap<Object, AuthenticationRequirementHolder[]> props = new
HashMap<Object, AuthenticationRequirementHolder[]>();
 
-        static ServiceListener createListener(final BundleContext context,
+        static SlingAuthenticatorServiceListener createListener(
+                final BundleContext context,
                 final SlingAuthenticator authenticator) {
             SlingAuthenticatorServiceListener listener = new SlingAuthenticatorServiceListener(
                 authenticator);
             try {
-                final String filter =  "(" + PAR_AUTH_REQ + "=*)";
-                context.addServiceListener(listener,filter);
-                ServiceReference[] refs = context.getAllServiceReferences(null, filter);
+                final String filter = "(" + PAR_AUTH_REQ + "=*)";
+                context.addServiceListener(listener, filter);
+                ServiceReference[] refs = context.getAllServiceReferences(null,
+                    filter);
                 if (refs != null) {
                     for (ServiceReference ref : refs) {
                         listener.addService(ref);
@@ -1098,24 +1106,69 @@
             }
         }
 
+        void registerServices() {
+            AuthenticationRequirementHolder[][] authReqsList;
+            synchronized (props) {
+                authReqsList = props.values().toArray(
+                    new AuthenticationRequirementHolder[props.size()][]);
+            }
+
+            for (AuthenticationRequirementHolder[] authReqs : authReqsList) {
+                registerService(authReqs);
+            }
+        }
+
+        private void registerService(
+                final AuthenticationRequirementHolder[] authReqs) {
+            for (AuthenticationRequirementHolder authReq : authReqs) {
+                authenticator.authRequiredCache.addHolder(authReq);
+            }
+        }
+
         private void addService(final ServiceReference ref) {
-            String[] authReqs = OsgiUtil.toStringArray(ref.getProperty(PAR_AUTH_REQ));
-            for (String authReq : authReqs) {
+            final String[] authReqPaths = OsgiUtil.toStringArray(ref.getProperty(PAR_AUTH_REQ));
+            final String source = getSource(ref);
+
+            ArrayList<AuthenticationRequirementHolder> authReqList = new ArrayList<AuthenticationRequirementHolder>();
+            for (String authReq : authReqPaths) {
                 if (authReq != null && authReq.length() > 0) {
-                    authenticator.authRequiredCache.addHolder(AuthenticationRequirementHolder.fromConfig(authReq));
+                    authReqList.add(AuthenticationRequirementHolder.fromConfig(
+                        authReq, source));
                 }
             }
-            props.put(ref.getProperty(Constants.SERVICE_ID), authReqs);
+
+            final AuthenticationRequirementHolder[] authReqs = authReqList.toArray(new AuthenticationRequirementHolder[authReqList.size()]);
+            registerService(authReqs);
+
+            synchronized (props) {
+                props.put(ref.getProperty(Constants.SERVICE_ID), authReqs);
+            }
         }
 
         private void removeService(final ServiceReference ref) {
-            String[] authReqs = props.remove(ref.getProperty(Constants.SERVICE_ID));
-            for (String authReq : authReqs) {
-                if (authReq != null && authReq.length() > 0) {
-                    authenticator.authRequiredCache.removeHolder(AuthenticationRequirementHolder.fromConfig(authReq));
+            final AuthenticationRequirementHolder[] authReqs;
+            synchronized (props) {
+                authReqs = props.remove(ref.getProperty(Constants.SERVICE_ID));
+            }
+
+            if (authReqs != null) {
+                for (AuthenticationRequirementHolder authReq : authReqs) {
+                    authenticator.authRequiredCache.removeHolder(authReq);
                 }
             }
         }
-    };
+
+        private String getSource(final ServiceReference ref) {
+            final String descr = OsgiUtil.toString(
+                ref.getProperty(Constants.SERVICE_DESCRIPTION), null);
+            if (descr != null) {
+                return descr;
+            }
+
+            return "Service "
+                + OsgiUtil.toString(ref.getProperty(Constants.SERVICE_ID),
+                    "unknown");
+        }
+    }
 
 }



Mime
View raw message