incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r941214 - in /sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl: AuthenticatorWebConsolePlugin.java PathBasedHolderCache.java SlingAuthenticator.java
Date Wed, 05 May 2010 09:17:01 GMT
Author: cziegeler
Date: Wed May  5 09:15:52 2010
New Revision: 941214

URL: http://svn.apache.org/viewvc?rev=941214&view=rev
Log:
SLING-1508 : Default auth handlers are not considered if a handler has a protocol or host

Modified:
    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/PathBasedHolderCache.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/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=941214&r1=941213&r2=941214&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 May  5 09:15:52 2010
@@ -20,7 +20,7 @@ package org.apache.sling.commons.auth.im
 
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.util.ArrayList;
+import java.util.List;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -80,7 +80,7 @@ public class AuthenticatorWebConsolePlug
         pw.println("<th class='content' colspan='2'>Handler</td>");
         pw.println("</tr>");
 
-        ArrayList<AbstractAuthenticationHandlerHolder> holderList = slingAuthenticator.getAuthenticationHandler();
+        final List<AbstractAuthenticationHandlerHolder> holderList = slingAuthenticator.getAuthenticationHandler();
         for (AbstractAuthenticationHandlerHolder handler : holderList) {
 
             pw.println("<tr class='content'>");
@@ -101,7 +101,7 @@ public class AuthenticatorWebConsolePlug
         pw.println("<th class='content'>Defining Service (Description or ID)</td>");
         pw.println("</tr>");
 
-        ArrayList<AuthenticationRequirementHolder> holderList = slingAuthenticator.getAuthenticationRequirements();
+        final List<AuthenticationRequirementHolder> holderList = slingAuthenticator.getAuthenticationRequirements();
         for (AuthenticationRequirementHolder req : holderList) {
 
             pw.println("<tr class='content'>");

Modified: sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/PathBasedHolderCache.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/PathBasedHolderCache.java?rev=941214&r1=941213&r2=941214&view=diff
==============================================================================
--- sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/PathBasedHolderCache.java
(original)
+++ sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/PathBasedHolderCache.java
Wed May  5 09:15:52 2010
@@ -21,99 +21,124 @@ package org.apache.sling.commons.auth.im
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import javax.servlet.http.HttpServletRequest;
 
 public class PathBasedHolderCache<Type extends PathBasedHolder> {
 
-    private final Map<String, Map<String, ArrayList<Type>>> cache = new
HashMap<String, Map<String, ArrayList<Type>>>();
+    private final Map<String, Map<String, List<Type>>> cache = new HashMap<String,
Map<String, List<Type>>>();
 
-    public synchronized void clear() {
-        cache.clear();
-    }
-
-    public synchronized void addHolder(final Type holder) {
+    /** Read/write lock to synchronize the cache access. */
+    private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
 
-        Map<String, ArrayList<Type>> byHostMap = cache.get(holder.protocol);
-        if (byHostMap == null) {
-            byHostMap = new HashMap<String, ArrayList<Type>>();
-            cache.put(holder.protocol, byHostMap);
+    public void clear() {
+        this.rwLock.writeLock().lock();
+        try {
+            cache.clear();
+        } finally {
+            this.rwLock.writeLock().unlock();
         }
+    }
 
-        final ArrayList<Type> byPathList = new ArrayList<Type>();
+    public void addHolder(final Type holder) {
+        this.rwLock.writeLock().lock();
+        try {
+
+            Map<String, List<Type>> byHostMap = cache.get(holder.protocol);
+            if (byHostMap == null) {
+                byHostMap = new HashMap<String, List<Type>>();
+                cache.put(holder.protocol, byHostMap);
+            }
 
-        // preset with current list
-        final ArrayList<Type> currentPathList = byHostMap.get(holder.host);
-        if (currentPathList != null) {
-            byPathList.addAll(currentPathList);
-        }
+            final List<Type> byPathList = new ArrayList<Type>();
+
+            // preset with current list
+            final List<Type> currentPathList = byHostMap.get(holder.host);
+            if (currentPathList != null) {
+                byPathList.addAll(currentPathList);
+            }
 
-        // add the new holder
-        byPathList.add(holder);
+            // add the new holder
+            byPathList.add(holder);
 
-        // sort the list according to the path length (longest path first)
-        Collections.sort(byPathList);
+            // sort the list according to the path length (longest path first)
+            Collections.sort(byPathList);
 
-        // replace old list with new list
-        byHostMap.put(holder.host, byPathList);
+            // replace old list with new list
+            byHostMap.put(holder.host, byPathList);
+        } finally {
+            this.rwLock.writeLock().unlock();
+        }
     }
 
-    public synchronized void removeHolder(final Type holder) {
-        final Map<String, ArrayList<Type>> byHostMap = cache.get(holder.protocol);
-        if (byHostMap != null) {
-            final ArrayList<Type> byPathList = byHostMap.get(holder.host);
-            if (byPathList != null) {
-
-                // create a new list without the removed holder
-                final ArrayList<Type> list = new ArrayList<Type>();
-                list.addAll(byPathList);
-                list.remove(holder);
-
-                // replace the old list with the new one (or remove if empty)
-                if (list.isEmpty()) {
-                    byHostMap.remove(holder.host);
-                } else {
-                    byHostMap.put(holder.host, list);
+    public void removeHolder(final Type holder) {
+        this.rwLock.writeLock().lock();
+        try {
+            final Map<String, List<Type>> byHostMap = cache.get(holder.protocol);
+            if (byHostMap != null) {
+                final List<Type> byPathList = byHostMap.get(holder.host);
+                if (byPathList != null) {
+
+                    // create a new list without the removed holder
+                    final List<Type> list = new ArrayList<Type>();
+                    list.addAll(byPathList);
+                    list.remove(holder);
+
+                    // replace the old list with the new one (or remove if empty)
+                    if (list.isEmpty()) {
+                        byHostMap.remove(holder.host);
+                    } else {
+                        byHostMap.put(holder.host, list);
+                    }
                 }
             }
+        } finally {
+            this.rwLock.writeLock().unlock();
         }
     }
 
-    public synchronized ArrayList<Type> findApplicableHolder(
-            HttpServletRequest request) {
-
-        Map<String, ArrayList<Type>> byHostMap = cache.get(request.getScheme());
-        if (byHostMap == null) {
-            byHostMap = cache.get("");
-        }
-
-        String hostname = request.getServerName()
-            + (request.getServerPort() != 80 && request.getServerPort() != 443
+    public List<Type>[] findApplicableHolder(final HttpServletRequest request) {
+        this.rwLock.readLock().lock();
+        try {
+            final String hostname = request.getServerName()
+                  + (request.getServerPort() != 80 && request.getServerPort() !=
443
                     ? ":" + request.getServerPort()
                     : "");
 
-        ArrayList<Type> infos = null;
-        if (byHostMap != null) {
-            infos = byHostMap.get(hostname);
-            if (infos == null) {
-                infos = byHostMap.get("");
+            @SuppressWarnings("unchecked")
+            final List<Type>[] result = new ArrayList[4];
+
+            final Map<String, List<Type>> byHostMap = cache.get(request.getScheme());
+            if ( byHostMap != null ) {
+                result[0] = byHostMap.get(hostname);
+                result[1] = byHostMap.get("");
             }
-            if (infos != null) {
-                return infos;
+            final Map<String, List<Type>> defaultByHostMap = cache.get("");
+            if ( defaultByHostMap != null ) {
+                result[0] = defaultByHostMap.get(hostname);
+                result[1] = defaultByHostMap.get("");
             }
+            return result;
+        } finally {
+            this.rwLock.readLock().unlock();
         }
-
-        return null;
     }
 
-    public synchronized ArrayList<Type> getHolders() {
-        final ArrayList<Type> result = new ArrayList<Type>();
-        for (Map<String, ArrayList<Type>> byHostEntry : cache.values()) {
-            for (ArrayList<Type> holderList : byHostEntry.values()) {
-                result.addAll(holderList);
+    public List<Type> getHolders() {
+        this.rwLock.readLock().lock();
+        try {
+            final List<Type> result = new ArrayList<Type>();
+            for (Map<String, List<Type>> byHostEntry : cache.values()) {
+                for (List<Type> holderList : byHostEntry.values()) {
+                    result.addAll(holderList);
+                }
             }
+            return result;
+        } finally {
+            this.rwLock.readLock().unlock();
         }
-        return result;
     }
 }

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=941214&r1=941213&r2=941214&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 May  5 09:15:52 2010
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.jcr.Credentials;
@@ -169,8 +170,6 @@ public class SlingAuthenticator implemen
      */
     private static final String AUTH_INFO_PROP_FEEDBACK_HANDLER = "$$sling.auth.AuthenticationFeedbackHandler$$";
 
-    private static ArrayList<AbstractAuthenticationHandlerHolder> EMPTY_INFO = new
ArrayList<AbstractAuthenticationHandlerHolder>();
-
     /** @scr.reference */
     private SlingRepository repository;
 
@@ -433,22 +432,27 @@ public class SlingAuthenticator implemen
         }
 
         // select path used for authentication handler selection
-        final ArrayList<AbstractAuthenticationHandlerHolder> holderList = findApplicableAuthenticationHandlers(request);
+        final List<AbstractAuthenticationHandlerHolder>[] holderListArray = this.authHandlerCache.findApplicableHolder(request);
         final String path = getHandlerSelectionPath(request);
         boolean done = false;
-        for (int i = 0; !done && i < holderList.size(); i++) {
-            final AbstractAuthenticationHandlerHolder holder = holderList.get(i);
-            if (path.startsWith(holder.path)) {
-                log.debug("login: requesting authentication using handler: {}",
-                    holder);
-
-                try {
-                    done = holder.requestCredentials(request, response);
-                } catch (IOException ioe) {
-                    log.error(
-                        "login: Failed sending authentication request through handler "
-                            + holder + ", access forbidden", ioe);
-                    done = true;
+        for(int m = 0; !done && m < holderListArray.length; m++) {
+            final List<AbstractAuthenticationHandlerHolder> holderList = holderListArray[m];
+            if ( holderList != null ) {
+                for (int i = 0; !done && i < holderList.size(); i++) {
+                    final AbstractAuthenticationHandlerHolder holder = holderList.get(i);
+                    if (path.startsWith(holder.path)) {
+                        log.debug("login: requesting authentication using handler: {}",
+                            holder);
+
+                        try {
+                            done = holder.requestCredentials(request, response);
+                        } catch (IOException ioe) {
+                            log.error(
+                                "login: Failed sending authentication request through handler
"
+                                    + holder + ", access forbidden", ioe);
+                            done = true;
+                        }
+                    }
                 }
             }
         }
@@ -460,8 +464,13 @@ public class SlingAuthenticator implemen
 
         // no handler could send an authentication request, throw
         if (!done) {
-            log.info("login: No handler for request ({} handlers available)",
-                holderList.size());
+            int size = 0;
+            for(int m = 0; !done && m < holderListArray.length; m++) {
+                if ( holderListArray[m] != null ) {
+                    size += holderListArray[m].size();
+                }
+            }
+            log.info("login: No handler for request ({} handlers available)", size);
             throw new NoAuthenticationHandlerException();
         }
     }
@@ -478,24 +487,28 @@ public class SlingAuthenticator implemen
             throw new IllegalStateException("Response already committed");
         }
 
-        final ArrayList<AbstractAuthenticationHandlerHolder> holderList = findApplicableAuthenticationHandlers(request);
         final String path = getHandlerSelectionPath(request);
-        for (int i = 0; i < holderList.size(); i++) {
-            AbstractAuthenticationHandlerHolder holder = holderList.get(i);
-            if (path.startsWith(holder.path)) {
-                log.debug("logout: dropping authentication using handler: {}",
-                    holder);
-
-                try {
-                    holder.dropCredentials(request, response);
-                } catch (IOException ioe) {
-                    log.error(
-                        "logout: Failed dropping authentication through handler "
-                            + holder, ioe);
+        final List<AbstractAuthenticationHandlerHolder>[] holderListArray = this.authHandlerCache.findApplicableHolder(request);
+        for(int m = 0; m < holderListArray.length; m++) {
+            final List<AbstractAuthenticationHandlerHolder> holderList = holderListArray[m];
+            if ( holderList != null ) {
+                for (int i = 0; i < holderList.size(); i++) {
+                    AbstractAuthenticationHandlerHolder holder = holderList.get(i);
+                    if (path.startsWith(holder.path)) {
+                        log.debug("logout: dropping authentication using handler: {}",
+                            holder);
+
+                        try {
+                            holder.dropCredentials(request, response);
+                        } catch (IOException ioe) {
+                            log.error(
+                                "logout: Failed dropping authentication through handler "
+                                    + holder, ioe);
+                        }
+                    }
                 }
             }
         }
-
         httpBasicHandler.dropCredentials(request, response);
 
         redirectAfterLogout(request, response);
@@ -521,27 +534,16 @@ public class SlingAuthenticator implemen
 
     // ---------- WebConsolePlugin support
 
-    ArrayList<AbstractAuthenticationHandlerHolder> getAuthenticationHandler() {
+    List<AbstractAuthenticationHandlerHolder> getAuthenticationHandler() {
         return authHandlerCache.getHolders();
     }
 
-    ArrayList<AuthenticationRequirementHolder> getAuthenticationRequirements() {
+    List<AuthenticationRequirementHolder> getAuthenticationRequirements() {
         return authRequiredCache.getHolders();
     }
 
     // ---------- internal
 
-    private ArrayList<AbstractAuthenticationHandlerHolder> findApplicableAuthenticationHandlers(
-            HttpServletRequest request) {
-
-        final ArrayList<AbstractAuthenticationHandlerHolder> infos = authHandlerCache.findApplicableHolder(request);
-        if (infos != null) {
-            return infos;
-        }
-
-        return EMPTY_INFO;
-    }
-
     private AuthenticationInfo getAuthenticationInfo(
             HttpServletRequest request, HttpServletResponse response) {
 
@@ -553,22 +555,27 @@ public class SlingAuthenticator implemen
             pathInfo = "/";
         }
 
-        ArrayList<AbstractAuthenticationHandlerHolder> local = findApplicableAuthenticationHandlers(request);
-        for (int i = 0; i < local.size(); i++) {
-            AbstractAuthenticationHandlerHolder holder = local.get(i);
-            if (pathInfo.startsWith(holder.path)) {
-                final AuthenticationInfo authInfo = holder.extractCredentials(
-                    request, response);
-
-                if (authInfo != null) {
-                    // post process the AuthenticationInfo object
-                    postProcess(authInfo, request, response);
-
-                    // add the feedback handler to the info (may be null)
-                    authInfo.put(AUTH_INFO_PROP_FEEDBACK_HANDLER,
-                        holder.getFeedbackHandler());
+        final List<AbstractAuthenticationHandlerHolder>[] localArray = this.authHandlerCache.findApplicableHolder(request);
+        for(int m = 0; m < localArray.length; m++) {
+            final List<AbstractAuthenticationHandlerHolder> local = localArray[m];
+            if ( local != null ) {
+                for (int i = 0; i < local.size(); i++) {
+                    AbstractAuthenticationHandlerHolder holder = local.get(i);
+                    if (pathInfo.startsWith(holder.path)) {
+                        final AuthenticationInfo authInfo = holder.extractCredentials(
+                            request, response);
+
+                        if (authInfo != null) {
+                            // post process the AuthenticationInfo object
+                            postProcess(authInfo, request, response);
+
+                            // add the feedback handler to the info (may be null)
+                            authInfo.put(AUTH_INFO_PROP_FEEDBACK_HANDLER,
+                                holder.getFeedbackHandler());
 
-                    return authInfo;
+                            return authInfo;
+                        }
+                    }
                 }
             }
         }
@@ -751,12 +758,15 @@ public class SlingAuthenticator implemen
             pathInfo = "/";
         }
 
-        ArrayList<AuthenticationRequirementHolder> holderList = authRequiredCache.findApplicableHolder(request);
-        if (holderList != null && !holderList.isEmpty()) {
-            for (int i = 0; i < holderList.size(); i++) {
-                AuthenticationRequirementHolder holder = holderList.get(i);
-                if (pathInfo.startsWith(holder.path)) {
-                    return !holder.requiresAuthentication();
+        final List<AuthenticationRequirementHolder>[] holderListArray = authRequiredCache.findApplicableHolder(request);
+        for(int m = 0; m < holderListArray.length; m++) {
+            final List<AuthenticationRequirementHolder> holderList = holderListArray[m];
+            if ( holderList != null ) {
+                for (int i = 0; i < holderList.size(); i++) {
+                    final AuthenticationRequirementHolder holder = holderList.get(i);
+                    if (pathInfo.startsWith(holder.path)) {
+                        return !holder.requiresAuthentication();
+                    }
                 }
             }
         }



Mime
View raw message