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

csierra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-jax-rs-whiteboard.git

commit 0d7503cba2ee480e988fb1cf4fa6de109e6d08b9
Author: Carlos Sierra <[email protected]>
AuthorDate: Thu Sep 20 15:28:38 2018 +0200

    [ARIES-1827] Add application.base.prefix to whiteboard
    
    to prefix the path of all the applications deployed in the whiteboard
---
 .../src/main/java/test/WhiteboardFactoryTest.java  | 123 ++++++++++++++++++++-
 .../internal/AriesJaxrsServiceRuntime.java         |   8 +-
 .../jax/rs/whiteboard/internal/Whiteboard.java     |  33 +++---
 .../jax/rs/whiteboard/internal/utils/Utils.java    |  29 +++++
 4 files changed, 168 insertions(+), 25 deletions(-)

diff --git a/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java 
b/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
index efc8f7d..697c13b 100644
--- a/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
+++ b/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
@@ -34,15 +34,14 @@ import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.jaxrs.runtime.JaxrsServiceRuntime;
+import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants;
 import org.osgi.util.tracker.ServiceTracker;
+import test.types.TestApplication;
 import test.types.TestHelper;
 
-public class WhiteboardFactoryTest extends TestHelper {
+import javax.ws.rs.client.WebTarget;
 
-    @Test
-    public void testDefaultDefaultWhiteboardConfig() throws Exception {
-        assertEquals(1, _runtimeTracker.size());
-    }
+public class WhiteboardFactoryTest extends TestHelper {
 
     @Test
     public void testCreateNewInstance() throws Exception {
@@ -125,6 +124,120 @@ public class WhiteboardFactoryTest extends TestHelper {
         }
     }
 
+    @Test
+    public void testDefaultDefaultWhiteboardConfig() throws Exception {
+        assertEquals(1, _runtimeTracker.size());
+    }
+
+    @Test
+    public void testWhiteboardApplicationBasePath() throws Exception {
+        ServiceTracker<ConfigurationAdmin, ConfigurationAdmin> configTracker =
+            new ServiceTracker<>(
+                bundleContext, ConfigurationAdmin.class, null);
+
+        CountDownLatch addedCountLatch = new CountDownLatch(1);
+        CountDownLatch modifiedCountLatch = new CountDownLatch(2);
+        CountDownLatch removedCountLatch = new CountDownLatch(1);
+
+        Configuration configuration = null;
+
+        ServiceTracker<?, ?> serviceTracker = new ServiceTracker
+            <JaxrsServiceRuntime, JaxrsServiceRuntime>(
+            bundleContext, JaxrsServiceRuntime.class, null) {
+
+            @Override
+            public JaxrsServiceRuntime addingService(
+                ServiceReference<JaxrsServiceRuntime> reference) {
+
+                if ("/prefix".equals(
+                    reference.getProperty("application.base.prefix"))) {
+
+                    addedCountLatch.countDown();
+
+                    return super.addingService(reference);
+                }
+
+                return null;
+            }
+
+            @Override
+            public void modifiedService(
+                ServiceReference<JaxrsServiceRuntime> reference,
+                JaxrsServiceRuntime service) {
+
+                modifiedCountLatch.countDown();
+            }
+
+            @Override
+            public void removedService(
+                ServiceReference<JaxrsServiceRuntime> reference,
+                JaxrsServiceRuntime service) {
+
+                removedCountLatch.countDown();
+            }
+        };
+
+        try {
+            configTracker.open();
+
+            serviceTracker.open();
+
+            ServiceReference<JaxrsServiceRuntime> serviceReference =
+                _runtimeTracker.getServiceReference();
+
+            assertNotNull(serviceReference);
+
+            assertEquals(1, _runtimeTracker.size());
+
+            ConfigurationAdmin admin = configTracker.waitForService(5000);
+
+            configuration = admin.createFactoryConfiguration(
+                "org.apache.aries.jax.rs.whiteboard", "?");
+
+            Dictionary<String, Object> properties = new Hashtable<>();
+
+            properties.put(Constants.SERVICE_RANKING, 1000);
+            properties.put("application.base.prefix", "/prefix");
+            properties.put("prefixed", true);
+
+            configuration.update(properties);
+
+            addedCountLatch.await(1, TimeUnit.MINUTES);
+
+            assertEquals(2, _runtimeTracker.size());
+
+            registerApplication(
+                new TestApplication(),
+                JaxrsWhiteboardConstants.JAX_RS_WHITEBOARD_TARGET,
+                "(prefixed=true)");
+
+            modifiedCountLatch.await(1, TimeUnit.MINUTES);
+
+            WebTarget webTarget =
+                createDefaultTarget().path("test-application");
+
+            assertEquals(404, webTarget.request().get().getStatus());
+
+            webTarget =
+                createDefaultTarget().path("prefix").path("test-application");
+
+            assertEquals(200, webTarget.request().get().getStatus());
+        }
+        finally {
+            if (configuration != null) {
+                configuration.delete();
+            }
+
+            removedCountLatch.await(1, TimeUnit.MINUTES);
+
+            assertEquals(1, _runtimeTracker.size());
+
+            configTracker.close();
+
+            serviceTracker.close();
+        }
+    }
+
     private BundleContext bundleContext =
         FrameworkUtil.getBundle(getClass()).getBundleContext();
 
diff --git 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
index c0201be..bf24b66 100644
--- 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
+++ 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxrsServiceRuntime.java
@@ -21,7 +21,6 @@ import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.canonicali
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.generateApplicationName;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.DEFAULT_NAME;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.SUPPORTED_EXTENSION_INTERFACES;
-import static 
org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.getApplicationBase;
 import static 
org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
 import static 
org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_NAME;
 
@@ -72,6 +71,10 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
     private static final Logger _log = LoggerFactory.getLogger(
         AriesJaxrsServiceRuntime.class);
 
+    public AriesJaxrsServiceRuntime(Whiteboard whiteboard) {
+        _whiteboard = whiteboard;
+    }
+
     public static String getServiceName(PropertyHolder properties) {
         Object property = properties.get(JAX_RS_NAME);
 
@@ -431,6 +434,7 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
 
     private ConcurrentHashMap<String, CachingServiceReference<?>>
         _servicesForName = new ConcurrentHashMap<>();
+    private Whiteboard _whiteboard;
 
     private Stream<FailedApplicationDTO>
         contextDependentApplicationsDTOStream() {
@@ -925,7 +929,7 @@ public class AriesJaxrsServiceRuntime implements 
JaxrsServiceRuntime {
 
         applicationDTO.name = getServiceName(
             ari._cachingServiceReference::getProperty);
-        applicationDTO.base = getApplicationBase(
+        applicationDTO.base = _whiteboard.getApplicationBase(
             ari._cachingServiceReference::getProperty);
         applicationDTO.serviceId =
             (Long)ari._cachingServiceReference.getProperty("service.id");
diff --git 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
index ab43e9f..b97c0e2 100644
--- 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
+++ 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java
@@ -77,8 +77,10 @@ import static 
org.apache.aries.jax.rs.whiteboard.internal.AriesJaxrsServiceRunti
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.LogUtils.ifDebugEnabled;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.LogUtils.ifErrorEnabled;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.canonicalize;
+import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.canonicalizeAddress;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.generateApplicationName;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.getProperties;
+import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.getString;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.highestPer;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.mergePropertyMaps;
 import static 
org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.onlyGettables;
@@ -141,6 +143,7 @@ public class Whiteboard {
 
     private static final Logger _log = LoggerFactory.getLogger(
         Whiteboard.class);
+    private final String _applicationBasePrefix;
     private final ApplicationExtensionRegistry _applicationExtensionRegistry;
     private final ExtensionRegistry _extensionRegistry;
 
@@ -168,6 +171,9 @@ public class Whiteboard {
         _applicationExtensionRegistry = new ApplicationExtensionRegistry();
         _extensionRegistry = new ExtensionRegistry();
 
+        _applicationBasePrefix = canonicalizeAddress(
+            getString(_configurationMap.get("application.base.prefix")));
+
         _program =
             all(
                 ignore(registerDefaultApplication()),
@@ -1087,8 +1093,9 @@ public class Whiteboard {
         return program;
     }
 
-    static String getApplicationBase(PropertyHolder properties) {
-        return properties.get(JAX_RS_APPLICATION_BASE).toString();
+    String getApplicationBase(PropertyHolder properties) {
+        return _applicationBasePrefix + getString(
+            properties.get(JAX_RS_APPLICATION_BASE));
     }
 
     private static OSGi<CachingServiceReference<CxfJaxrsServiceRegistrator>>
@@ -1188,17 +1195,8 @@ public class Whiteboard {
 
         Map<String, ?> serviceProperties = servicePropertiesSup.get();
 
-        String address = getApplicationBase(serviceProperties::get);
-
-        if (!address.startsWith("/")) {
-            address = "/" + address;
-        }
-
-        if (address.endsWith("/")) {
-            address = address.substring(0, address.length() - 1);
-        }
-
-        String finalAddress = address;
+        String address = canonicalizeAddress(
+            getApplicationBase(serviceProperties::get));
 
         String applicationName = getServiceName(serviceProperties::get);
 
@@ -1219,7 +1217,7 @@ public class Whiteboard {
 
                 String contextName;
 
-                if ("".equals(finalAddress)) {
+                if ("".equals(address)) {
                     contextName = HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME;
                 } else {
                     contextName = "context.for" + applicationName;
@@ -1228,7 +1226,7 @@ public class Whiteboard {
                 contextProperties.put(
                     HTTP_WHITEBOARD_CONTEXT_NAME, contextName);
                 contextProperties.put(
-                    HTTP_WHITEBOARD_CONTEXT_PATH, finalAddress);
+                    HTTP_WHITEBOARD_CONTEXT_PATH, address);
 
                 return contextProperties;
             };
@@ -1271,8 +1269,7 @@ public class Whiteboard {
             }
             else {
                 servletProperties.put(
-                    HTTP_WHITEBOARD_SERVLET_PATTERN,
-                    finalAddress + "/*");
+                    HTTP_WHITEBOARD_SERVLET_PATTERN, address + "/*");
             }
             servletProperties.put(
                 HTTP_WHITEBOARD_SERVLET_ASYNC_SUPPORTED, true);
@@ -1366,7 +1363,7 @@ public class Whiteboard {
         }
     }
 
-    private static class ApplicationReferenceWithContext
+    private class ApplicationReferenceWithContext
         implements Comparable<ApplicationReferenceWithContext> {
 
         @Override
diff --git 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/Utils.java
 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/Utils.java
index 48a0383..c9fa124 100644
--- 
a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/Utils.java
+++ 
b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/Utils.java
@@ -65,6 +65,35 @@ public class Utils {
         return new String[]{propertyValue.toString()};
     }
 
+    public static String canonicalizeAddress(String address) {
+        if (address == null) {
+            return "";
+        }
+
+        if (address.length() == 0) {
+            return address;
+        }
+
+        if (!address.startsWith("/")) {
+            address = "/" + address;
+        }
+
+        if (address.endsWith("/")) {
+            address = address.substring(0, address.length() - 1);
+        }
+
+        return address;
+    }
+
+    public static String getString(Object string) {
+        if (string == null) {
+            return "";
+        }
+        else {
+            return String.valueOf(string);
+        }
+    }
+
     public static String generateApplicationName(
         PropertyHolder propertyHolder) {
 

Reply via email to