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:31 UTC

[sling-org-apache-sling-serviceusermapper] 06/12: SLING-2944 Implement Service User Mapper

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 f39b2fc77d8bfed8677f7be2028c9453cd030b88
Author: Felix Meschberger <fm...@apache.org>
AuthorDate: Mon Aug 5 10:08:50 2013 +0000

    SLING-2944 Implement Service User Mapper
    
    * Add ServiceUserMapper service and implementation bundle
    * Add service login methods to ResourceResolverFactory
      and SlingRepository
    * Add implementations of new methods
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/serviceusermapper@1510413 13f79535-47bb-0310-9956-ffa450edef68
---
 .../serviceusermapping/ServiceUserMapper.java      | 54 +++++++++++++---------
 .../sling/serviceusermapping/impl/Mapping.java     | 14 +++---
 .../impl/ServiceUserMapperImpl.java                | 12 ++---
 .../sling/serviceusermapping/impl/MappingTest.java | 28 +++++------
 .../impl/ServiceUserMapperImplTest.java            | 28 +++++------
 5 files changed, 74 insertions(+), 62 deletions(-)

diff --git a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
index f3988b5..425a08f 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java
@@ -24,7 +24,7 @@ import aQute.bnd.annotation.ProviderType;
 
 /**
  * The <code>ServiceUserMapper</code> service can be used to map a service
- * provided by a bundle to the name of a user account used to access the
+ * provided by a bundle to the ID of a user account used to access the
  * ResourceResolver used by the service to access its data.
  * <p>
  * The goal of this service is to allow services to be implemented accessing the
@@ -36,9 +36,9 @@ import aQute.bnd.annotation.ProviderType;
  * 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
+ * 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.
+ * {@code subServiceName} parameter on the method calls.
  * <p>
  * In addition to allowing to phase out the use of
  * {@code ResourceResolver.getAdministrativeResourceResolver} and
@@ -50,6 +50,10 @@ import aQute.bnd.annotation.ProviderType;
  * {@code SlingRepository} services.
  * <p>
  * This service is not intended to be implemented by clients.
+ *
+ * @see <a href=
+ *      "http://sling.apache.org/documentation/the-sling-engine/service-authentication.html"
+ *      >Service Authentication</a>
  */
 @ProviderType
 public interface ServiceUserMapper {
@@ -62,35 +66,43 @@ public interface ServiceUserMapper {
     String BUNDLE_HEADER_SERVICE_NAME = "Sling-Service";
 
     /**
-     * Returns the name of the service represented by the {@code bundle} and the
-     * {@code serviceInfo}.
+     * Returns the ID of the service represented by the given {@code bundle} and
+     * the {@code subServiceName}.
+     * <p>
+     * The service ID consists of a name derived from the bundle and the
+     * {@code serviceInfo} value if not {@code null} or empty:
+     *
+     * <pre>
+     * serviceID = serviceName [ ":" subServiceName ] .
+     * serviceName = Sling-Service manifest header or bundle symbolic name .
+     * </pre>
      * <p>
-     * The service name consists of a name derived from the bundle and the
-     * {@code serviceInfo} value if not {@code null} or empty.
+     * The service name for a bundle is taken from the
+     * {@value #BUNDLE_HEADER_SERVICE_NAME} manifest header of the bundle. If
+     * there is no such header or the value is empty, the bundle's symbolic name
+     * is used.
      *
      * @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
-     *            an empty string or {@code null}.
-     * @return The name of the service represented by the bundle along with the
+     * @param subServiceName Name of the sub service. This parameter is optional and
+     *            may be an empty string or {@code null}.
+     * @return The ID of the service represented by the bundle along with the
      *         additional service information.
      */
-    String getServiceName(Bundle bundle, String serviceInfo);
+    String getServiceID(Bundle bundle, String subServiceName);
 
     /**
-     * Returns the name of a user to the be used to access the Sling Resource
-     * tree or the JCR Repository.
+     * Returns the ID of a user to access the data store on behalf of the
+     * service.
      *
      * @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
-     *            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
+     * @param subServiceName Name of the sub service. This parameter is optional and
+     *            may be an empty string or {@code null}.
+     * @return The ID 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
      *         optional {@code serviceInfo}.
      */
-    String getUserForService(Bundle bundle, String serviceInfo);
+    String getServiceUserID(Bundle bundle, String subServiceName);
 }
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 3023bd7..b7effb7 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java
@@ -26,7 +26,7 @@ class Mapping {
 
     private final String serviceName;
 
-    private final String serviceInfo;
+    private final String subServiceName;
 
     private final String userName;
 
@@ -34,7 +34,7 @@ class Mapping {
      * Creates a mapping entry for the entry specification of the form:
      *
      * <pre>
-     * spec = serviceName [ ":" serviceInfo ] "=" userName .
+     * spec = serviceName [ ":" subServiceName ] "=" userName .
      * </pre>
      *
      * @param spec The mapping specification.
@@ -57,10 +57,10 @@ class Mapping {
 
         if (colon < 0 || colon > equals) {
             this.serviceName = spec.substring(0, equals);
-            this.serviceInfo = null;
+            this.subServiceName = null;
         } else {
             this.serviceName = spec.substring(0, colon);
-            this.serviceInfo = spec.substring(colon + 1, equals);
+            this.subServiceName = spec.substring(colon + 1, equals);
         }
 
         this.userName = spec.substring(equals + 1);
@@ -72,12 +72,12 @@ class Mapping {
      *
      * @param serviceName The name of the service to match. If this is
      *            {@code null} this mapping will not match.
-     * @param serviceInfo The info of the service to match. This may be
+     * @param subServiceName The Subservice Name to match. This may be
      *            {@code null}.
      * @return The user name if this mapping matches or {@code null} otherwise.
      */
-    String map(final String serviceName, final String serviceInfo) {
-        if (this.serviceName.equals(serviceName) && equals(this.serviceInfo, serviceInfo)) {
+    String map(final String serviceName, final String subServiceName) {
+        if (this.serviceName.equals(serviceName) && equals(this.subServiceName, subServiceName)) {
             return userName;
         }
 
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 c5479c7..9e15be9 100644
--- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
+++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java
@@ -44,8 +44,8 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
     @Property(
             label = "Service Mappings",
             description = "Provides mappings from service name to user names. "
-                + "Each entry is of the form 'serviceName [ \":\" serviceInfo ] \"=\" userName' "
-                + "where serviceName and serviceInfo identify the service and userName "
+                + "Each entry is of the form 'serviceName [ \":\" subServiceName ] \"=\" userName' "
+                + "where serviceName and subServiceName identify the service and userName "
                 + "defines the name of the user to provide to the service. Invalid entries are logged and ignored.",
             unbounded = PropertyUnbounded.ARRAY)
     private static final String PROP_SERVICE2USER_MAPPING = "user.mapping";
@@ -90,17 +90,17 @@ public class ServiceUserMapperImpl implements ServiceUserMapper {
         this.defaultUser = PropertiesUtil.toString(config.get(PROP_DEFAULT_USER), PROP_DEFAULT_USER_DEFAULT);
     }
 
-    public String getServiceName(Bundle bundle, String serviceInfo) {
+    public String getServiceID(Bundle bundle, String subServiceName) {
         final String serviceName = getServiceName(bundle);
-        return (serviceInfo == null || serviceInfo.length() == 0) ? serviceName : serviceName + ":" + serviceInfo;
+        return (subServiceName == null || subServiceName.length() == 0) ? serviceName : serviceName + ":" + subServiceName;
     }
 
-    public String getUserForService(Bundle bundle, String serviceInfo) {
+    public String getServiceUserID(Bundle bundle, String subServiceName) {
         final String serviceName = getServiceName(bundle);
 
         // try with serviceInfo first
         for (Mapping mapping : this.serviceUserMappings) {
-            final String user = mapping.map(serviceName, serviceInfo);
+            final String user = mapping.map(serviceName, subServiceName);
             if (user != null) {
                 return user;
             }
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 fce0475..7764d79 100644
--- a/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java
+++ b/src/test/java/org/apache/sling/serviceusermapping/impl/MappingTest.java
@@ -92,40 +92,40 @@ public class MappingTest {
     @Test
     public void test_constructor_and_map() {
         assertMapping("service", null, "user");
-        assertMapping("service", "serviceInfo", "user");
+        assertMapping("service", "subServiceName", "user");
     }
 
-    private void assertMapping(final String serviceName, final String serviceInfo, final String userName) {
+    private void assertMapping(final String serviceName, final String subServiceName, final String userName) {
         StringBuilder spec = new StringBuilder();
         spec.append(serviceName);
-        if (serviceInfo != null) {
-            spec.append(':').append(serviceInfo);
+        if (subServiceName != null) {
+            spec.append(':').append(subServiceName);
         }
         spec.append('=').append(userName);
 
         // spec analysis
         final Mapping mapping = new Mapping(spec.toString());
         TestCase.assertEquals(getField(mapping, "serviceName"), serviceName);
-        TestCase.assertEquals(getField(mapping, "serviceInfo"), serviceInfo);
+        TestCase.assertEquals(getField(mapping, "subServiceName"), subServiceName);
         TestCase.assertEquals(getField(mapping, "userName"), userName);
 
         // mapping
-        TestCase.assertEquals(userName, mapping.map(serviceName, serviceInfo));
-        if (serviceInfo == null) {
-            // Mapping without serviceInfo must not match request with any
-            // serviceInfo
-            TestCase.assertNull(mapping.map(serviceName, serviceInfo + "-garbage"));
+        TestCase.assertEquals(userName, mapping.map(serviceName, subServiceName));
+        if (subServiceName == null) {
+            // Mapping without subServiceName must not match request with any
+            // subServiceName
+            TestCase.assertNull(mapping.map(serviceName, subServiceName + "-garbage"));
         } else {
-            // Mapping with serviceInfo must not match request without
-            // serviceInfo
+            // Mapping with subServiceName must not match request without
+            // subServiceName
             TestCase.assertNull(mapping.map(serviceName, null));
         }
 
         // no match for different service name
-        TestCase.assertNull(mapping.map(serviceName + "-garbage", serviceInfo));
+        TestCase.assertNull(mapping.map(serviceName + "-garbage", subServiceName));
 
         // no match for null service name
-        TestCase.assertNull(mapping.map(null, serviceInfo));
+        TestCase.assertNull(mapping.map(null, subServiceName));
     }
 
     private String getField(final Object object, final String fieldName) {
diff --git a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
index 9160525..1ea2458 100644
--- a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
+++ b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java
@@ -82,7 +82,7 @@ public class ServiceUserMapperImplTest {
     };
 
     @Test
-    public void test_getServiceName() {
+    public void test_getServiceID() {
         @SuppressWarnings("serial")
         Map<String, Object> config = new HashMap<String, Object>() {
             {
@@ -99,16 +99,16 @@ public class ServiceUserMapperImplTest {
         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));
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1, sum.getServiceID(BUNDLE1, null));
+        TestCase.assertEquals(SRV, sum.getServiceID(BUNDLE2, null));
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1, sum.getServiceID(BUNDLE1, ""));
+        TestCase.assertEquals(SRV, sum.getServiceID(BUNDLE2, ""));
+        TestCase.assertEquals(BUNDLE_SYMBOLIC1 + ":" + SUB, sum.getServiceID(BUNDLE1, SUB));
+        TestCase.assertEquals(SRV + ":" + SUB, sum.getServiceID(BUNDLE2, SUB));
     }
 
     @Test
-    public void test_getUserForService() {
+    public void test_getServiceUserID() {
         @SuppressWarnings("serial")
         Map<String, Object> config = new HashMap<String, Object>() {
             {
@@ -125,11 +125,11 @@ public class ServiceUserMapperImplTest {
         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));
+        TestCase.assertEquals(SAMPLE, sum.getServiceUserID(BUNDLE1, null));
+        TestCase.assertEquals(ANOTHER, sum.getServiceUserID(BUNDLE2, null));
+        TestCase.assertEquals(SAMPLE, sum.getServiceUserID(BUNDLE1, ""));
+        TestCase.assertEquals(ANOTHER, sum.getServiceUserID(BUNDLE2, ""));
+        TestCase.assertEquals(SAMPLE_SUB, sum.getServiceUserID(BUNDLE1, SUB));
+        TestCase.assertEquals(ANOTHER_SUB, sum.getServiceUserID(BUNDLE2, SUB));
     }
 }

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