You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:13:30 UTC

[sling-org-apache-sling-serviceusermapper] 05/12: Implement support for service based ResourceResolver and Session access

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.serviceusermapper-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-serviceusermapper.git

commit 3ecf9df7b40436a9de6b1839fac7e73234623fd9
Author: Felix Meschberger <fm...@apache.org>
AuthorDate: Wed Jul 3 18:08:20 2013 +0000

    Implement support for service based ResourceResolver and Session access
    
    - Clarify empty serviceInfo to be the same as null
    - Clarify empty serviceInfo in configuration is an error
      (error sample: service:=user)
    - Add missing service label and description for Metatype Service
    - Add unit tests for ServiceUserMapperImpl service
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/whiteboard/fmeschbe/deprecate_login_administrative/serviceusermapper@1499501 13f79535-47bb-0310-9956-ffa450edef68
---
 .../serviceusermapping/ServiceUserMapper.java      |  14 ++-
 .../sling/serviceusermapping/impl/Mapping.java     |  12 +-
 .../impl/ServiceUserMapperImpl.java                |  12 +-
 .../sling/serviceusermapping/impl/MappingTest.java |  16 ++-
 .../impl/ServiceUserMapperImplTest.java            | 135 +++++++++++++++++++++
 5 files changed, 175 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
index 35938c8..f3988b5 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
@@ -32,6 +32,14 @@ import aQute.bnd.annotation.ProviderType;
  * service appropriate access without requiring administrative level access to
  * the storage.
  * <p>
+ * In general a service is implement in a single bundle such as the JSP compiler
+ * bundle. Other services may be implemented in multiple bundles. In certain
+ * cases there may be sub-services requiring different access levels. For
+ * example a couple of bundles may implement a "mail" service where each bundle
+ * implement implements a part of the service such as the "smtp", "queuing", and
+ * "delivery" sub services. Such sub services are identified with the
+ * {@code serviceInfo} parameter on the method calls.
+ * <p>
  * In addition to allowing to phase out the use of
  * {@code ResourceResolver.getAdministrativeResourceResolver} and
  * {@code SlingRepository.loginAdministrative} it also allows to better account
@@ -58,13 +66,13 @@ public interface ServiceUserMapper {
      * {@code serviceInfo}.
      * <p>
      * The service name consists of a name derived from the bundle and the
-     * {@code serviceInfo} value if not {@code null}.
+     * {@code serviceInfo} value if not {@code null} or empty.
      *
      * @param bundle The bundle implementing the service request access to
      *            resources.
      * @param serviceInfo Additional information about the concrete service
      *            requesting access. This parameter is optional and may be
-     *            {@code null}.
+     *            an empty string or {@code null}.
      * @return The name of the service represented by the bundle along with the
      *         additional service information.
      */
@@ -78,7 +86,7 @@ public interface ServiceUserMapper {
      *            resources.
      * @param serviceInfo Additional information about the concrete service
      *            requesting access. This parameter is optional and may be
-     *            {@code null}.
+     *            an empty string or {@code null}.
      * @return The name of the user to use to provide access to the resources
      *         for the service. This may be {@code null} if no particular user
      *         can be derived for the service identified by the bundle and the
diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java b/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java
index 679b5dc..3023bd7 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java
@@ -47,15 +47,15 @@ class Mapping {
         final int colon = spec.indexOf(':');
         final int equals = spec.indexOf('=');
 
-        if (equals <= 0) {
-            throw new IllegalArgumentException("serviceName missing");
+        if (colon == 0 || equals <= 0) {
+            throw new IllegalArgumentException("serviceName is required");
         } else if (equals == spec.length() - 1) {
-            throw new IllegalArgumentException("userName missing");
+            throw new IllegalArgumentException("userName is required");
+        } else if (colon + 1 == equals) {
+            throw new IllegalArgumentException("serviceInfo must not be empty");
         }
 
-        if (colon == 0) {
-            throw new IllegalArgumentException("serviceName missing");
-        } else if (colon < 0 || colon > equals) {
+        if (colon < 0 || colon > equals) {
             this.serviceName = spec.substring(0, equals);
             this.serviceInfo = null;
         } else {
diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
index bc14ccd..c5479c7 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
@@ -33,7 +33,11 @@ import org.osgi.framework.Bundle;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@Component(metatype = true, ds = true)
+@Component(
+        metatype = true,
+        ds = true,
+        label = "Apache Sling Service User Mapper Service",
+        description = "Configuration for the service mapping service names to names of users.")
 @Service()
 public class ServiceUserMapperImpl implements ServiceUserMapper {
 
@@ -55,7 +59,7 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
             label = "Default User",
             description = "The name of the user to use as the default if no service mapping"
                 + "applies. If this property is missing or empty no default user is defined.")
-    private static final String PROP_DEFAULT_USER_DEFAULT = "";
+    private static final String PROP_DEFAULT_USER_DEFAULT = null;
 
     /** default log */
     private final Logger log = LoggerFactory.getLogger(getClass());
@@ -66,7 +70,7 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
 
     @Activate
     @Modified
-    private void configure(Map<String, Object> config) {
+    void configure(Map<String, Object> config) {
         final String[] props = PropertiesUtil.toStringArray(config.get(PROP_SERVICE2USER_MAPPING),
             PROP_SERVICE2USER_MAPPING_DEFAULT);
 
@@ -88,7 +92,7 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
 
     public String getServiceName(Bundle bundle, String serviceInfo) {
         final String serviceName = getServiceName(bundle);
-        return (serviceInfo == null) ? serviceName : serviceName +  ":" + serviceInfo;
+        return (serviceInfo == null || serviceInfo.length() == 0) ? serviceName : serviceName + ":" + serviceInfo;
     }
 
     public String getUserForService(Bundle bundle, String serviceInfo) {
diff --git a/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java b/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java
index 7a1728a..fce0475 100644
--- a/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java
+++ b/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java
@@ -75,10 +75,24 @@ public class MappingTest {
     }
 
     @Test
+    public void test_constructor_empty_service_info() {
+        try {
+            new Mapping("srv:=user");
+            TestCase.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException iae) {
+            // expected
+        }
+    }
+
+    @Test
+    public void test_constructor_user_with_colon() {
+        new Mapping("srv=jcr:user");
+    }
+
+    @Test
     public void test_constructor_and_map() {
         assertMapping("service", null, "user");
         assertMapping("service", "serviceInfo", "user");
-        assertMapping("service", "", "user");
     }
 
     private void assertMapping(final String serviceName, final String serviceInfo, final String userName) {
diff --git a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
new file mode 100644
index 0000000..9160525
--- /dev/null
+++ b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
@@ -0,0 +1,135 @@
+/*
+ * 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.serviceusermapping.impl;
+
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.sling.commons.testing.osgi.MockBundle;
+import org.apache.sling.serviceusermapping.ServiceUserMapper;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+
+public class ServiceUserMapperImplTest {
+    private static final String BUNDLE_SYMBOLIC1 = "bundle1";
+
+    private static final String BUNDLE_SYMBOLIC2 = "bundle2";
+
+    private static final String SRV = "srv";
+
+    private static final String SUB = "sub";
+
+    private static final String NONE = "none";
+
+    private static final String SAMPLE = "sample";
+
+    private static final String ANOTHER = "another";
+
+    private static final String SAMPLE_SUB = "sample_sub";
+
+    private static final String ANOTHER_SUB = "another_sub";
+
+    private static final Bundle BUNDLE1 = new MockBundle(10) {
+        public String getSymbolicName() {
+            return BUNDLE_SYMBOLIC1;
+        };
+
+        public java.util.Dictionary<?, ?> getHeaders() {
+            return new Hashtable<String, Object>();
+        };
+
+        public java.util.Dictionary<?, ?> getHeaders(String locale) {
+            return getHeaders();
+        };
+    };
+
+    private static final Bundle BUNDLE2 = new MockBundle(10) {
+        public String getSymbolicName() {
+            return BUNDLE_SYMBOLIC2;
+        };
+
+        @SuppressWarnings("serial")
+        public java.util.Dictionary<?, ?> getHeaders() {
+            return new Hashtable<String, Object>() {
+                {
+                    put(ServiceUserMapper.BUNDLE_HEADER_SERVICE_NAME, SRV);
+                }
+            };
+        };
+
+        public java.util.Dictionary<?, ?> getHeaders(String locale) {
+            return getHeaders();
+        };
+    };
+
+    @Test
+    public void test_getServiceName() {
+        @SuppressWarnings("serial")
+        Map<String, Object> config = new HashMap<String, Object>() {
+            {
+                put("user.mapping", new String[] {
+                    BUNDLE_SYMBOLIC1 + "=" + SAMPLE, //
+                    SRV + "=" + ANOTHER, //
+                    BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, //
+                    SRV + ":" + SUB + "=" + ANOTHER_SUB //
+                });
+                put("user.default", NONE);
+            }
+        };
+
+        final ServiceUserMapperImpl sum = new ServiceUserMapperImpl();
+        sum.configure(config);
+
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1, sum.getServiceName(BUNDLE1, null));
+        TestCase.assertEquals(SRV, sum.getServiceName(BUNDLE2, null));
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1, sum.getServiceName(BUNDLE1, ""));
+        TestCase.assertEquals(SRV, sum.getServiceName(BUNDLE2, ""));
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1 + ":" + SUB, sum.getServiceName(BUNDLE1, SUB));
+        TestCase.assertEquals(SRV + ":" + SUB, sum.getServiceName(BUNDLE2, SUB));
+    }
+
+    @Test
+    public void test_getUserForService() {
+        @SuppressWarnings("serial")
+        Map<String, Object> config = new HashMap<String, Object>() {
+            {
+                put("user.mapping", new String[] {
+                    BUNDLE_SYMBOLIC1 + "=" + SAMPLE, //
+                    SRV + "=" + ANOTHER, //
+                    BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, //
+                    SRV + ":" + SUB + "=" + ANOTHER_SUB //
+                });
+                put("user.default", NONE);
+            }
+        };
+
+        final ServiceUserMapperImpl sum = new ServiceUserMapperImpl();
+        sum.configure(config);
+
+        TestCase.assertEquals(SAMPLE, sum.getUserForService(BUNDLE1, null));
+        TestCase.assertEquals(ANOTHER, sum.getUserForService(BUNDLE2, null));
+        TestCase.assertEquals(SAMPLE, sum.getUserForService(BUNDLE1, ""));
+        TestCase.assertEquals(ANOTHER, sum.getUserForService(BUNDLE2, ""));
+        TestCase.assertEquals(SAMPLE_SUB, sum.getUserForService(BUNDLE1, SUB));
+        TestCase.assertEquals(ANOTHER_SUB, sum.getUserForService(BUNDLE2, SUB));
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.