Author: fmeschbe
Date: Thu Aug 12 15:01:02 2010
New Revision: 984816

URL: http://svn.apache.org/viewvc?rev=984816&view=rev
Log:
SLING-1647 Remove old constant definitions (now moved to 
ResourceResolverFactory and JcrResourceConstants) and adapt the code and remove 
the check for the Credentials property not appropriate here.

Modified:
    
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/SlingAuthenticator.java
    
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java
    
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java
    
sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java

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=984816&r1=984815&r2=984816&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
 Thu Aug 12 15:01:02 2010
@@ -910,7 +910,7 @@ public class SlingAuthenticator implemen
 
         // sudo the session if needed
         if (sudo != null && sudo.length() > 0) {
-            authInfo.put(ResourceResolverFactory.SUDO_USER_ID, sudo);
+            authInfo.put(ResourceResolverFactory.USER_IMPERSONATION, sudo);
         }
     }
 
@@ -930,7 +930,7 @@ public class SlingAuthenticator implemen
 
     private void setSudoCookie(HttpServletRequest req,
             HttpServletResponse res, AuthenticationInfo authInfo) {
-        String sudo = (String) 
authInfo.get(ResourceResolverFactory.SUDO_USER_ID);
+        String sudo = (String) 
authInfo.get(ResourceResolverFactory.USER_IMPERSONATION);
         String currentSudo = getSudoCookieValue(req);
 
         // set the (new) impersonation

Modified: 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java?rev=984816&r1=984815&r2=984816&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java
 (original)
+++ 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/impl/engine/EngineAuthenticationHandlerHolder.java
 Thu Aug 12 15:01:02 2010
@@ -68,9 +68,12 @@ public final class EngineAuthenticationH
             return AuthenticationInfo.DOING_AUTH;
         }
 
+        // backwards compatibility support for JCR credentials and workspace
+        // name now encapsulated in the JCR Resource bundle
         AuthenticationInfo info = new AuthenticationInfo(
             engineAuthInfo.getAuthType());
-        info.put(AuthenticationInfo.CREDENTIALS, 
engineAuthInfo.getCredentials());
+        info.put("user.jcr.credentials", engineAuthInfo.getCredentials());
+        info.put("user.jcr.workspace", engineAuthInfo.getWorkspaceName());
 
         return info;
     }

Modified: 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java?rev=984816&r1=984815&r2=984816&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java
 (original)
+++ 
sling/trunk/bundles/commons/auth/src/main/java/org/apache/sling/commons/auth/spi/AuthenticationInfo.java
 Thu Aug 12 15:01:02 2010
@@ -21,6 +21,8 @@ package org.apache.sling.commons.auth.sp
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.sling.api.resource.ResourceResolverFactory;
+
 /**
  * The <code>AuthenticationInfo</code> conveys any authentication credentials
  * and/or details extracted by the
@@ -76,41 +78,6 @@ public class AuthenticationInfo extends 
     public static final String AUTH_TYPE = "sling.authType";
 
     /**
-     * The name of the property providing the name of the user on whose behalf
-     * the request is being handled. This property is set by the
-     * {...@link #AuthenticationInfo(String, String, char[])} constructor
-     * and may be <code>null</code> if this instance is created by either the
-     * {...@link #AuthenticationInfo(String, String)} or
-     * {...@link #AuthenticationInfo(String, String, char[])} constructors.
-     * <p>
-     * The type of this property, if present, is <code>String</code>.
-     */
-    public static final String USER = "user.name";
-
-    /**
-     * The name of the property providing the password of the user on whose
-     * behalf the request is being handled. This property is set by the
-     * {...@link #AuthenticationInfo(String, String, char[])} constructor
-     * and may be <code>null</code> if this instance is created by either the
-     * {...@link #AuthenticationInfo(String, String)} or
-     * {...@link #AuthenticationInfo(String, String, char[])} constructors.
-     * <p>
-     * The type of this property, if present, is <code>char[]</code>.
-     */
-    public static final String PASSWORD = "user.password";
-
-    /**
-     * The name of the property providing the JCR credentials. These 
credentials
-     * are preset to the credentials given to the
-     * {...@link #AuthenticationInfo(String, String)} or
-     * {...@link #AuthenticationInfo(String, String, char[])} constructors.
-     * is used the credentials property is set to a JCR
-     * <code>SimpleCredentials</code> instance containing the user id and
-     * password passed to the constructor.
-     */
-    public static final String CREDENTIALS = "user.jcr.credentials";
-
-    /**
      * Creates an instance of this class with just the authentication type. To
      * effectively use this instance the user Id with optional password and/or
      * the credentials should be set.
@@ -150,8 +117,8 @@ public class AuthenticationInfo extends 
     public AuthenticationInfo(final String authType, final String userId,
             final char[] password) {
         super.put(AUTH_TYPE, authType);
-        putIfNotNull(USER, userId);
-        putIfNotNull(PASSWORD, password);
+        putIfNotNull(ResourceResolverFactory.USER, userId);
+        putIfNotNull(ResourceResolverFactory.PASSWORD, password);
     }
 
     /**
@@ -181,7 +148,7 @@ public class AuthenticationInfo extends 
      *            <code>null</code> the current user name is not replaced.
      */
     public final void setUser(String user) {
-        putIfNotNull(USER, user);
+        putIfNotNull(ResourceResolverFactory.USER, user);
     }
 
     /**
@@ -189,7 +156,7 @@ public class AuthenticationInfo extends 
      * <code>null</code> if the user is not set in this map.
      */
     public final String getUser() {
-        return (String) get(USER);
+        return (String) get(ResourceResolverFactory.USER);
     }
 
     /**
@@ -197,7 +164,7 @@ public class AuthenticationInfo extends 
      *            <code>null</code> the current password is not replaced.
      */
     public final void setPassword(char[] password) {
-        putIfNotNull(PASSWORD, password);
+        putIfNotNull(ResourceResolverFactory.PASSWORD, password);
     }
 
     /**
@@ -205,7 +172,7 @@ public class AuthenticationInfo extends 
      * <code>null</code> if the password is not set in this map.
      */
     public final char[] getPassword() {
-        return (char[]) get(PASSWORD);
+        return (char[]) get(ResourceResolverFactory.PASSWORD);
     }
 
     /**
@@ -225,10 +192,6 @@ public class AuthenticationInfo extends 
      * <td>{...@link #PASSWORD}</td>
      * <td><code>char[]</code></td>
      * </tr>
-     * <tr>
-     * <td>{...@link #CREDENTIALS}</td>
-     * <td><code>javax.jcr.Credentials</code></td>
-     * </tr>
      * </table>
      * <p>
      * If the value for the special key does not match the required type an
@@ -251,42 +214,21 @@ public class AuthenticationInfo extends 
                 + " property must be a String");
         }
 
-        if (USER.equals(key) && !(value instanceof String)) {
-            throw new IllegalArgumentException(USER
+        if (ResourceResolverFactory.USER.equals(key)
+            && !(value instanceof String)) {
+            throw new IllegalArgumentException(ResourceResolverFactory.USER
                 + " property must be a String");
         }
 
-        if (PASSWORD.equals(key) && !(value instanceof char[])) {
-            throw new IllegalArgumentException(PASSWORD
+        if (ResourceResolverFactory.PASSWORD.equals(key)
+            && !(value instanceof char[])) {
+            throw new IllegalArgumentException(ResourceResolverFactory.PASSWORD
                 + " property must be a char[]");
         }
 
-        if (CREDENTIALS.equals(key) && !isCredentialsObject(value)) {
-            throw new IllegalArgumentException(CREDENTIALS
-                + " property must be a javax.jcr.Credentials instance");
-        }
-
         return super.put(key, value);
     }
 
-    /** We do this check in order to avoid an import to the javax.jcr.* 
package */
-    private boolean isCredentialsObject(final Object value) {
-        if ( value == null ) {
-            return false;
-        }
-        Class<?> checkClass = value.getClass();
-        do {
-            final Class<?>[] interfaces = value.getClass().getInterfaces();
-            for(final Class<?> current : interfaces) {
-                if ( current.getName().equals("javax.jcr.Credentials") ) {
-                    return true;
-                }
-            }
-            checkClass = checkClass.getSuperclass();
-        } while ( checkClass != null );
-        return false;
-    }
-
     /**
      * Removes the entry with the given <code>key</code> and returns its former
      * value (if existing). If the <code>key</code> is {...@link #AUTH_TYPE} 
the

Modified: 
sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java?rev=984816&r1=984815&r2=984816&view=diff
==============================================================================
--- 
sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java
 (original)
+++ 
sling/trunk/bundles/commons/auth/src/test/java/org/apache/sling/commons/auth/spi/AuthenticationInfoTest.java
 Thu Aug 12 15:01:02 2010
@@ -27,11 +27,16 @@ import static org.junit.Assert.fail;
 import javax.jcr.Credentials;
 import javax.jcr.SimpleCredentials;
 
+import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.junit.Assert;
 import org.junit.Test;
 
 public class AuthenticationInfoTest {
 
+    // backwards compatible constant tied to jcr/resource
+    // JcrResourceResolverFactory.CREDENTIALS
+    private static final String CREDENTIALS = "user.jcr.credentials";
+
     @Test
     public void testClear() {
         final char[] pwd = new char[6];
@@ -45,8 +50,8 @@ public class AuthenticationInfoTest {
 
         Assert.assertEquals(1, info.size()); // AUTH_TYPE still contained
         Assert.assertEquals("test", info.getAuthType());
-        assertFalse(info.containsKey(AuthenticationInfo.USER));
-        assertFalse(info.containsKey(AuthenticationInfo.PASSWORD));
+        assertFalse(info.containsKey(ResourceResolverFactory.USER));
+        assertFalse(info.containsKey(ResourceResolverFactory.PASSWORD));
     }
 
     @Test
@@ -147,11 +152,11 @@ public class AuthenticationInfoTest {
     @Test
     public void testGetUser() {
         final AuthenticationInfo info = new AuthenticationInfo("test");
-        info.put(AuthenticationInfo.USER, "name");
+        info.put(ResourceResolverFactory.USER, "name");
 
         Assert.assertEquals("name", info.getUser());
-        Assert.assertEquals("name", info.get(AuthenticationInfo.USER));
-        Assert.assertEquals(info.get(AuthenticationInfo.USER), info.getUser());
+        Assert.assertEquals("name", info.get(ResourceResolverFactory.USER));
+        Assert.assertEquals(info.get(ResourceResolverFactory.USER), 
info.getUser());
     }
 
     @Test
@@ -159,13 +164,13 @@ public class AuthenticationInfoTest {
         final char[] pwd = new char[6];
         final AuthenticationInfo info = new AuthenticationInfo("test", "name");
 
-        assertFalse(info.containsKey(AuthenticationInfo.PASSWORD));
+        assertFalse(info.containsKey(ResourceResolverFactory.PASSWORD));
 
         info.setPassword(pwd);
-        assertSame(pwd, info.get(AuthenticationInfo.PASSWORD));
+        assertSame(pwd, info.get(ResourceResolverFactory.PASSWORD));
 
         info.setPassword(null);
-        assertSame(pwd, info.get(AuthenticationInfo.PASSWORD));
+        assertSame(pwd, info.get(ResourceResolverFactory.PASSWORD));
     }
 
     @Test
@@ -175,8 +180,8 @@ public class AuthenticationInfoTest {
             pwd);
 
         assertSame(pwd, info.getPassword());
-        assertEquals(pwd, (char[]) info.get(AuthenticationInfo.PASSWORD));
-        Assert.assertEquals(info.get(AuthenticationInfo.PASSWORD),
+        assertEquals(pwd, (char[]) info.get(ResourceResolverFactory.PASSWORD));
+        Assert.assertEquals(info.get(ResourceResolverFactory.PASSWORD),
             info.getPassword());
     }
 
@@ -185,21 +190,21 @@ public class AuthenticationInfoTest {
         final Credentials creds = new SimpleCredentials("user", new char[0]);
         final AuthenticationInfo info = new AuthenticationInfo("test");
 
-        info.put(AuthenticationInfo.CREDENTIALS, creds);
-        Assert.assertSame(creds, info.get(AuthenticationInfo.CREDENTIALS));
+        info.put(CREDENTIALS, creds);
+        Assert.assertSame(creds, info.get(CREDENTIALS));
     }
 
     @Test
     public void testGetCredentials() {
         final AuthenticationInfo info = new AuthenticationInfo("test");
 
-        assertNull(info.get(AuthenticationInfo.CREDENTIALS));
-        assertFalse(info.containsKey(AuthenticationInfo.CREDENTIALS));
+        assertNull(info.get(CREDENTIALS));
+        assertFalse(info.containsKey(CREDENTIALS));
 
         final Credentials creds = new SimpleCredentials("user", new char[0]);
-        info.put(AuthenticationInfo.CREDENTIALS, creds);
+        info.put(CREDENTIALS, creds);
 
-        assertSame(creds, info.get(AuthenticationInfo.CREDENTIALS));
+        assertSame(creds, info.get(CREDENTIALS));
 
         final String user = "user";
         final char[] pwd = new char[5];
@@ -207,7 +212,7 @@ public class AuthenticationInfoTest {
             user, pwd);
 
         // credentials not stored in the object
-        assertFalse(infoCred.containsKey(AuthenticationInfo.CREDENTIALS));
+        assertFalse(infoCred.containsKey(CREDENTIALS));
     }
 
     @Test
@@ -232,25 +237,21 @@ public class AuthenticationInfoTest {
     public void testPutStringObject() {
         final AuthenticationInfo info = new AuthenticationInfo("test", "user",
             new char[2]);
-        info.put(AuthenticationInfo.CREDENTIALS,
+        info.put(CREDENTIALS,
                 new SimpleCredentials("user", new char[2]));
 
         test_put_fail(info, AuthenticationInfo.AUTH_TYPE, null);
-        test_put_fail(info, AuthenticationInfo.USER, null);
-        test_put_fail(info, AuthenticationInfo.PASSWORD, null);
-        test_put_fail(info, AuthenticationInfo.CREDENTIALS, null);
+        test_put_fail(info, ResourceResolverFactory.USER, null);
+        test_put_fail(info, ResourceResolverFactory.PASSWORD, null);
 
         test_put_fail(info, AuthenticationInfo.AUTH_TYPE, 42);
-        test_put_fail(info, AuthenticationInfo.USER, 42);
-        test_put_fail(info, AuthenticationInfo.PASSWORD, "string");
-        test_put_fail(info, AuthenticationInfo.CREDENTIALS, "string");
+        test_put_fail(info, ResourceResolverFactory.USER, 42);
+        test_put_fail(info, ResourceResolverFactory.PASSWORD, "string");
 
         test_put_success(info, AuthenticationInfo.AUTH_TYPE, "new_type");
-        test_put_success(info, AuthenticationInfo.USER, "new_user");
-        test_put_success(info, AuthenticationInfo.PASSWORD,
+        test_put_success(info, ResourceResolverFactory.USER, "new_user");
+        test_put_success(info, ResourceResolverFactory.PASSWORD,
             "new_pwd".toCharArray());
-        test_put_success(info, AuthenticationInfo.CREDENTIALS,
-            new SimpleCredentials("new_user", new char[0]));
     }
 
     private void test_put_success(final AuthenticationInfo info,


Reply via email to