[DOSGI-110] Support for relative endpoint addresses when used with HTTP contexts...
authorSergey Beryozkin <sergeyb@apache.org>
Thu, 26 Jul 2012 13:36:30 +0000 (13:36 +0000)
committerSergey Beryozkin <sergeyb@apache.org>
Thu, 26 Jul 2012 13:36:30 +0000 (13:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/cxf/dosgi/trunk@1365986 13f79535-47bb-0310-9956-ffa450edef68

distribution/multi-bundle/pom.xml
distribution/multi-bundle/src/main/resources/distro_bundles.xml
distribution/single-bundle/pom.xml
dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandler.java
dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/JaxRSHttpServiceConfigurationTypeHandler.java
samples/greeter_rest/impl2/pom.xml [new file with mode: 0644]
samples/greeter_rest/impl2/src/main/java/org/apache/cxf/dosgi/samples/greeter/impl2/rest/Activator.java [new file with mode: 0644]
samples/greeter_rest/impl2/src/main/java/org/apache/cxf/dosgi/samples/greeter/impl2/rest/GreeterServiceImpl2.java [new file with mode: 0644]
samples/greeter_rest/pom.xml

index 8757324..99af74d 100644 (file)
       <version>1.1.1</version>
     </dependency>
     <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.6</version>
+    </dependency>
+    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.enterprise</artifactId>
        <version>${osgi.version}</version>
index 87885e5..8229c34 100644 (file)
@@ -7,6 +7,7 @@
   <bundle>cxf-dosgi-ri-multibundle-distribution-${project.version}-dir/apache-cxf-dosgi-ri-${project.version}/dosgi_bundles/geronimo-ws-metadata_2.0_spec-1.1.3.jar</bundle>
   <bundle>cxf-dosgi-ri-multibundle-distribution-${project.version}-dir/apache-cxf-dosgi-ri-${project.version}/dosgi_bundles/com.springsource.org.apache.commons.logging-1.1.1.jar</bundle>
   <bundle>cxf-dosgi-ri-multibundle-distribution-${project.version}-dir/apache-cxf-dosgi-ri-${project.version}/dosgi_bundles/com.springsource.org.jdom-1.1.0.jar</bundle>
+  <bundle>cxf-dosgi-ri-multibundle-distribution-${project.version}-dir/apache-cxf-dosgi-ri-${project.version}/dosgi_bundles/commons-lang-2.6.jar</bundle>
   <bundle>cxf-dosgi-ri-multibundle-distribution-${project.version}-dir/apache-cxf-dosgi-ri-${project.version}/dosgi_bundles/spring-core-${spring.version}.jar</bundle>
   <bundle>cxf-dosgi-ri-multibundle-distribution-${project.version}-dir/apache-cxf-dosgi-ri-${project.version}/dosgi_bundles/spring-beans-${spring.version}.jar</bundle>
   <bundle>cxf-dosgi-ri-multibundle-distribution-${project.version}-dir/apache-cxf-dosgi-ri-${project.version}/dosgi_bundles/spring-context-${spring.version}.jar</bundle>
index 0bd385d..3417074 100644 (file)
       <artifactId>com.springsource.org.apache.log4j</artifactId> 
       <version>${log4j.version}</version>
     </dependency>
-    
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.6</version>
+    </dependency>
     <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-bundle-minimal</artifactId>
index adb704e..fb36d6c 100644 (file)
@@ -18,6 +18,7 @@
  */
 package org.apache.cxf.dosgi.dsw.handlers;
 
+import java.net.URI;
 import java.net.UnknownHostException;
 import java.util.Collections;
 import java.util.HashMap;
@@ -28,6 +29,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.cxf.Bus;
 import org.apache.cxf.aegis.databinding.AegisDatabinding;
 import org.apache.cxf.common.logging.LogUtils;
@@ -148,10 +150,12 @@ public class HttpServiceConfigurationTypeHandler extends AbstractPojoConfigurati
         
         ServerFactoryBean factory = 
                createServerFactoryBean(frontEndImpl != null ? frontEndImpl : frontEndImpl2);
-        String address = constructAddress(dswContext, contextRoot);
+        
         factory.setBus(bus);
         factory.setServiceClass(iClass);
-        factory.setAddress("/");
+        
+        String relativeEndpointAddress = getRelativeEndpointAddress(sd);
+        factory.setAddress(relativeEndpointAddress);
         factory.getServiceFactory().setDataBinding(databinding);
         factory.setServiceBean(serviceBean);
         
@@ -159,6 +163,9 @@ public class HttpServiceConfigurationTypeHandler extends AbstractPojoConfigurati
         
         setWsdlProperties(factory, callingContext, sd, false);
         
+        String completeEndpointAddress = 
+                       constructAddress(dswContext, contextRoot, relativeEndpointAddress);
+        
         ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
         try {
             String[] intents = applyIntents(dswContext, callingContext, factory.getFeatures(), factory, sd);
@@ -166,7 +173,7 @@ public class HttpServiceConfigurationTypeHandler extends AbstractPojoConfigurati
             // The properties for the EndpointDescription
             Map<String, Object> endpointProps = createEndpointProps(sd, iClass, new String[] {
                 Constants.WS_CONFIG_TYPE
-            }, address,intents);
+            }, completeEndpointAddress, intents);
             EndpointDescription endpdDesc = null;
             
             Thread.currentThread().setContextClassLoader(ServerFactoryBean.class.getClassLoader());
@@ -202,7 +209,8 @@ public class HttpServiceConfigurationTypeHandler extends AbstractPojoConfigurati
         return cxf.getBus();
     }
 
-    protected String constructAddress(BundleContext ctx, String contextRoot) {
+    protected String constructAddress(BundleContext ctx, String contextRoot, 
+                                         String relativeEndpointAddress) {
         String port = null;
         boolean https = false;
         if ("true".equalsIgnoreCase(ctx.getProperty("org.osgi.service.http.secure.enabled"))) {
@@ -221,8 +229,13 @@ public class HttpServiceConfigurationTypeHandler extends AbstractPojoConfigurati
         } catch (UnknownHostException e) {
             hostName = "localhost";
         }
-
-        return getAddress(https ? "https" : "http", hostName, port, contextRoot);
+        
+        String address = getAddress(https ? "https" : "http", hostName, port, contextRoot);
+        if (!StringUtils.isEmpty(relativeEndpointAddress) 
+               && !relativeEndpointAddress.equals("/")) {
+               address += relativeEndpointAddress;
+        }
+        return address;
     }
 
     protected HttpService getHttpService() {
@@ -355,4 +368,18 @@ public class HttpServiceConfigurationTypeHandler extends AbstractPojoConfigurati
         }
     }
 
+    protected String getRelativeEndpointAddress(Map sd) {
+        String address = OsgiUtils.getProperty(sd, Constants.RS_ADDRESS_PROPERTY);
+        if (address != null) {
+               if (URI.create(address).isAbsolute()) {
+                       LOG.info("Ignoring an absolute endpoint address, the value of " 
+                                        + Constants.RS_ADDRESS_PROPERTY + " property can only be a relative URI"
+                                        + " when " + Constants.RS_HTTP_SERVICE_CONTEXT 
+                                        + " property is set");
+               } else {
+                       return address;
+               }
+        }
+        return "/";
+    }
 }
index 2a40d04..06ab47b 100644 (file)
@@ -67,7 +67,9 @@ public class JaxRSHttpServiceConfigurationTypeHandler extends HttpServiceConfigu
             factory.setResourceProvider(iClass, new SingletonResourceProvider(serviceBean));
         }
         
-        factory.setAddress("/");
+        String relativeEndpointAddress = getRelativeEndpointAddress(sd);
+        factory.setAddress(relativeEndpointAddress);
+        
         List<Object> providers = JaxRSUtils.getProviders(callingContext, dswContext, sd);
         if (providers != null && providers.size() > 0) {
             factory.setProviders(providers);
@@ -81,7 +83,8 @@ public class JaxRSHttpServiceConfigurationTypeHandler extends HttpServiceConfigu
                 }
        }        
 
-        String address = constructAddress(dswContext, contextRoot);
+       String completeEndpointAddress = 
+                       constructAddress(dswContext, contextRoot, relativeEndpointAddress);
 
         ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
         try {
@@ -92,7 +95,7 @@ public class JaxRSHttpServiceConfigurationTypeHandler extends HttpServiceConfigu
             // The properties for the EndpointDescription
             Map<String, Object> endpointProps = createEndpointProps(sd, iClass, new String[] {
                 Constants.RS_CONFIG_TYPE
-            }, address,intents);
+            }, completeEndpointAddress, intents);
             EndpointDescription endpdDesc = null;
             
             
@@ -123,5 +126,4 @@ public class JaxRSHttpServiceConfigurationTypeHandler extends HttpServiceConfigu
         }
         return context;
     }
-
 }
diff --git a/samples/greeter_rest/impl2/pom.xml b/samples/greeter_rest/impl2/pom.xml
new file mode 100644 (file)
index 0000000..b5fbc54
--- /dev/null
@@ -0,0 +1,79 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<!--
+  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.
+-->
+
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>org.apache.cxf.dosgi.samples</groupId>
+    <artifactId>cxf-dosgi-ri-samples-greeter-rest-impl2</artifactId>
+    <packaging>bundle</packaging>
+    <name>Distributed OSGI Greeter Implementation2 Bundle</name>
+    <version>1.4-SNAPSHOT</version>
+
+    <parent>
+      <groupId>org.apache.cxf.dosgi</groupId>
+      <artifactId>cxf-dosgi-ri-parent</artifactId>
+      <version>1.4-SNAPSHOT</version>
+      <relativePath>../../../parent/pom.xml</relativePath>
+    </parent>
+
+    <properties>
+        <bundle.import.package>javax.ws.rs.*,*</bundle.import.package>                               
+        <bundle.private.package>org.apache.cxf.dosgi.samples.greeter.impl2.rest</bundle.private.package>
+    </properties>
+    
+    <dependencies>
+        <dependency> 
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.framework</artifactId>
+            <version>${felix.version}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.felix</groupId>
+                    <artifactId>org.osgi.foundation</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency> 
+        <dependency> 
+            <groupId>org.apache.cxf.dosgi.samples</groupId>
+            <artifactId>cxf-dosgi-ri-samples-greeter-rest-interface</artifactId>
+            <version>${project.version}</version>
+        </dependency> 
+    </dependencies> 
+
+    <build>
+      <plugins>
+        <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <configuration>
+                    <instructions>
+                        <Bundle-Name>CXF Distributed OSGi Greeter Demo Service Rest Implementation2 Bundle</Bundle-Name>
+                        <Bundle-Description>This bundle contains the implementation of the CXF Distributed OSGi Greeter Rest Demo Implementation2 Bundle</Bundle-Description>
+                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+                        <Bundle-Activator>org.apache.cxf.dosgi.samples.greeter.impl2.rest.Activator</Bundle-Activator>
+                        <Import-Package>${bundle.import.package}</Import-Package>
+                        <Private-Package>${bundle.private.package}</Private-Package> 
+                        <DynamicImport-Package>org.apache.cxf.dosgi.dsw.qos,org.apache.cxf</DynamicImport-Package>
+                    </instructions>
+                </configuration>
+            </plugin> 
+         </plugins>
+    </build>    
+
+</project>
diff --git a/samples/greeter_rest/impl2/src/main/java/org/apache/cxf/dosgi/samples/greeter/impl2/rest/Activator.java b/samples/greeter_rest/impl2/src/main/java/org/apache/cxf/dosgi/samples/greeter/impl2/rest/Activator.java
new file mode 100644 (file)
index 0000000..08e1126
--- /dev/null
@@ -0,0 +1,53 @@
+/** 
+  * 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.cxf.dosgi.samples.greeter.impl2.rest;
+
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.cxf.dosgi.samples.greeter.rest.GreeterService;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+public class Activator implements BundleActivator {
+    private ServiceRegistration registration;
+    public void start(BundleContext bc) throws Exception {
+        Dictionary props = getProperties("/rest");
+        registration = bc.registerService(GreeterService.class.getName(), 
+                                          new GreeterServiceImpl2(), props);
+    }
+
+    @SuppressWarnings("unchecked")
+       private Dictionary getProperties(String address) { 
+       Dictionary props = new Hashtable();
+
+        props.put("service.exported.interfaces", "*");
+        props.put("service.exported.configs", "org.apache.cxf.rs");
+        props.put("service.exported.intents", "HTTP");
+        props.put("org.apache.cxf.rs.httpservice.context", address);
+        props.put("org.apache.cxf.rs.address", "/service");
+        return props;
+    }
+    
+    public void stop(BundleContext bc) throws Exception {
+        registration.unregister();
+    }
+}
diff --git a/samples/greeter_rest/impl2/src/main/java/org/apache/cxf/dosgi/samples/greeter/impl2/rest/GreeterServiceImpl2.java b/samples/greeter_rest/impl2/src/main/java/org/apache/cxf/dosgi/samples/greeter/impl2/rest/GreeterServiceImpl2.java
new file mode 100644 (file)
index 0000000..cefead8
--- /dev/null
@@ -0,0 +1,50 @@
+/** 
+  * 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.cxf.dosgi.samples.greeter.impl2.rest;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.cxf.dosgi.samples.greeter.rest.GreeterException;
+import org.apache.cxf.dosgi.samples.greeter.rest.GreeterInfo;
+import org.apache.cxf.dosgi.samples.greeter.rest.GreeterService;
+import org.apache.cxf.dosgi.samples.greeter.rest.GreetingPhrase;
+
+public class GreeterServiceImpl2 implements GreeterService {
+
+    private final static String STRANGER_NAME = "Stranger";
+
+    public GreeterInfo greetMe(String name) throws GreeterException {
+        System.out.println("Invoking from GreeterServiceImpl2: greetMe(" + name + ")");
+        
+        if (name.equals(STRANGER_NAME)) {
+            throw new GreeterException(name);
+        }
+
+        GreeterInfo info = new GreeterInfo();
+        List<GreetingPhrase> list = new ArrayList<GreetingPhrase>();
+        list.add(new GreetingPhrase("Hello", name));
+        list.add(new GreetingPhrase("Hoi", name));
+        list.add(new GreetingPhrase("Hola", name));
+        list.add(new GreetingPhrase("Bonjour", name));
+        info.setGreetings(list);
+        return info;
+    }
+
+}
index 3779e59..024c577 100644 (file)
@@ -36,6 +36,7 @@
     <modules>    
       <module>interface</module>
       <module>impl</module>
+      <module>impl2</module>
       <module>client</module>
     </modules>