You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2012/07/24 11:07:50 UTC

svn commit: r1364945 - in /sling/trunk/bundles/jcr/resource/src: main/java/org/apache/sling/jcr/resource/ main/java/org/apache/sling/jcr/resource/internal/ main/java/org/apache/sling/jcr/resource/internal/helper/jcr/ test/java/org/apache/sling/jcr/reso...

Author: cziegeler
Date: Tue Jul 24 09:07:49 2012
New Revision: 1364945

URL: http://svn.apache.org/viewvc?rev=1364945&view=rev
Log:
SLING-2542 : Readd initial workspace support

Added:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/WorkspaceAuthInfoPostProcessor.java   (with props)
Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderFactory.java
    sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java?rev=1364945&r1=1364944&r2=1364945&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java Tue Jul 24 09:07:49 2012
@@ -81,7 +81,6 @@ public class JcrResourceConstants {
      * The type of this property, if present, is <code>String</code>.
      *
      * @since 2.1
-     * @deprecated
      */
     public static final String AUTHENTICATION_INFO_WORKSPACE = "user.jcr.workspace";
 

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/WorkspaceAuthInfoPostProcessor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/WorkspaceAuthInfoPostProcessor.java?rev=1364945&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/WorkspaceAuthInfoPostProcessor.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/WorkspaceAuthInfoPostProcessor.java Tue Jul 24 09:07:49 2012
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.auth.core.AuthenticationSupport;
+import org.apache.sling.auth.core.spi.AbstractAuthenticationHandler;
+import org.apache.sling.auth.core.spi.AuthenticationInfo;
+import org.apache.sling.auth.core.spi.AuthenticationInfoPostProcessor;
+import org.apache.sling.jcr.resource.JcrResourceConstants;
+import org.osgi.framework.Constants;
+
+/**
+ * The <code>WorkspaceAuthInfoPostProcessor</code> is a simple
+ * AuthenticationInfo post processor which sets the
+ * {@link JcrResourceConstants#AUTHENTICATION_INFO_WORKSPACE} property (unless
+ * set already) if the {@link #J_WORKSPACE} request attribute or parameter is
+ * set to a non-empty string.
+ * <p>
+ * This allows logging into any workspace for a given request provided the
+ * requested workspace exists.
+ */
+@Component
+@Service
+@Property(name = Constants.SERVICE_DESCRIPTION, value = "JCR Workspace property setter")
+public class WorkspaceAuthInfoPostProcessor implements AuthenticationInfoPostProcessor {
+
+    /**
+     * The name of the request parameter (or request attribute) indicating the
+     * workspace to use.
+     * <p>
+     * The {@link AuthenticationSupport} service implemented by this bundle will
+     * respect this parameter and attribute and ensure the
+     * <code>jcr.user.workspace</code> attribute of the
+     * {@link org.apache.sling.auth.core.spi.AuthenticationInfo} used for
+     * accessing the resource resolver is set to this value (unless the property
+     * has already been set by the
+     * {@link org.apache.sling.auth.core.spi.AuthenticationHandler} providing
+     * the {@link org.apache.sling.auth.core.spi.AuthenticationInfo} instance).
+     */
+    public static final String J_WORKSPACE = "j_workspace";
+
+    /**
+     * Sets the {@link JcrResourceConstants#AUTHENTICATION_INFO_WORKSPACE} if
+     * the {@link #J_WORKSPACE} request parameter or attribute is defined and
+     * the {@link JcrResourceConstants#AUTHENTICATION_INFO_WORKSPACE} does not
+     * exist yet in the authentication info.
+     */
+    public void postProcess(AuthenticationInfo info, HttpServletRequest request, HttpServletResponse response) {
+        final String workspace = AbstractAuthenticationHandler.getAttributeOrParameter(request, J_WORKSPACE, "");
+        if (workspace.length() > 0 && !info.containsKey(JcrResourceConstants.AUTHENTICATION_INFO_WORKSPACE)) {
+            info.put(JcrResourceConstants.AUTHENTICATION_INFO_WORKSPACE, workspace);
+        }
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/WorkspaceAuthInfoPostProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/WorkspaceAuthInfoPostProcessor.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/WorkspaceAuthInfoPostProcessor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderFactory.java?rev=1364945&r1=1364944&r2=1364945&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderFactory.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProviderFactory.java Tue Jul 24 09:07:49 2012
@@ -104,9 +104,10 @@ public class JcrResourceProviderFactory 
         // derive the session to be used
         Session session;
         try {
+            final String workspace = getWorkspace(authenticationInfo);
             if (isAdmin) {
                 // requested admin session to any workspace (or default)
-                session = repository.loginAdministrative(null);
+                session = repository.loginAdministrative(workspace);
 
             } else {
 
@@ -114,7 +115,29 @@ public class JcrResourceProviderFactory 
                 if (session == null) {
                     // requested non-admin session to any workspace (or default)
                     final Credentials credentials = getCredentials(authenticationInfo);
-                    session = repository.login(credentials, null);
+                    session = repository.login(credentials, workspace);
+
+                } else if (workspace != null) {
+                    // session provided by map; but requested a different
+                    // workspace impersonate can only change the user not switch
+                    // the workspace as a workaround we login to the requested
+                    // workspace with admin and then switch to the provided
+                    // session's user (if required)
+                    Session tmpSession = null;
+                    try {
+                        tmpSession = repository.loginAdministrative(workspace);
+                        if (tmpSession.getUserID().equals(session.getUserID())) {
+                            session = tmpSession;
+                            tmpSession = null;
+                        } else {
+                            session = tmpSession.impersonate(new SimpleCredentials(
+                                session.getUserID(), new char[0]));
+                        }
+                    } finally {
+                        if (tmpSession != null) {
+                            tmpSession.logout();
+                        }
+                    }
 
                 } else {
                     // session provided; no special workspace; just make sure
@@ -122,7 +145,7 @@ public class JcrResourceProviderFactory 
                     logoutSession = false;
                 }
             }
-        } catch (RepositoryException re) {
+        } catch (final RepositoryException re) {
             throw getLoginException(re);
         }
 
@@ -149,6 +172,23 @@ public class JcrResourceProviderFactory 
     }
 
     /**
+     * Return the workspace name.
+     * If the workspace name is provided, it is returned, otherwise
+     * <code>null</code> is returned.
+     * @param authenticationInfo Optional authentication info.
+     * @return The configured workspace name or <code>null</code>
+     */
+    private String getWorkspace(final Map<String, Object> authenticationInfo) {
+        if (authenticationInfo != null) {
+            final Object workspaceObject = authenticationInfo.get(JcrResourceConstants.AUTHENTICATION_INFO_WORKSPACE);
+            if (workspaceObject instanceof String) {
+                return (String) workspaceObject;
+            }
+        }
+        return null;
+    }
+
+    /**
      * Handle the sudo if configured. If the authentication info does not
      * contain a sudo info, this method simply returns the passed in session. If
      * a sudo user info is available, the session is tried to be impersonated.

Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java?rev=1364945&r1=1364944&r2=1364945&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java (original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java Tue Jul 24 09:07:49 2012
@@ -266,11 +266,11 @@ public class JcrResourceListenerTest ext
         EventHelper helper = new EventHelper(newSession);
         helper.waitForEvents(5000);
         helper.dispose();
+        listener.dispose();
+
         newSession.logout();
         session.logout();
 
-        listener.dispose();
-
         return events;
     }
 }