camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-9587 - camel-restlet - Make it easy to turn on gson or jackson
Date Fri, 08 Apr 2016 11:46:28 GMT
Repository: camel
Updated Branches:
  refs/heads/master d7fc67fa1 -> 8fb1874e0


CAMEL-9587 - camel-restlet - Make it easy to turn on gson or jackson


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8fb1874e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8fb1874e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8fb1874e

Branch: refs/heads/master
Commit: 8fb1874e087f2fe33de5ae991d8fdfc730b224e7
Parents: d7fc67f
Author: lburgazzoli <lburgazzoli@gmail.com>
Authored: Wed Apr 6 12:57:03 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Apr 8 13:44:41 2016 +0200

----------------------------------------------------------------------
 components/camel-restlet/pom.xml                | 11 ++++
 .../component/restlet/RestletComponent.java     | 49 +++++++++++++-
 .../component/restlet/RestletEndpoint.java      |  1 -
 .../restlet/RestletConfigurationTest.java       | 68 ++++++++++++++++++++
 parent/pom.xml                                  | 15 +++++
 .../features/src/main/resources/features.xml    | 22 +++++++
 6 files changed, 164 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8fb1874e/components/camel-restlet/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-restlet/pom.xml b/components/camel-restlet/pom.xml
index dfb75c7..c1b2155 100644
--- a/components/camel-restlet/pom.xml
+++ b/components/camel-restlet/pom.xml
@@ -61,6 +61,17 @@
       <groupId>org.restlet.jee</groupId>
       <artifactId>org.restlet.ext.httpclient</artifactId>
     </dependency>
+    <!-- extensions -->
+    <dependency>
+      <groupId>org.restlet.jse</groupId>
+      <artifactId>org.restlet.ext.jackson</artifactId>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>org.restlet.jse</groupId>
+      <artifactId>org.restlet.ext.gson</artifactId>
+      <optional>true</optional>
+    </dependency>
 
     <!-- camel test dependencies -->
     <dependency>

http://git-wip-us.apache.org/repos/asf/camel/blob/8fb1874e/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
index 573ef9e..322d0ec 100644
--- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
+++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
@@ -21,11 +21,11 @@ import java.net.URI;
 import java.security.GeneralSecurityException;
 import java.security.InvalidParameterException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
-
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLEngine;
 
@@ -50,6 +50,7 @@ import org.restlet.data.ChallengeScheme;
 import org.restlet.data.Method;
 import org.restlet.data.Parameter;
 import org.restlet.data.Protocol;
+import org.restlet.engine.Engine;
 import org.restlet.security.ChallengeAuthenticator;
 import org.restlet.security.MapVerifier;
 import org.restlet.util.Series;
@@ -63,6 +64,7 @@ import org.slf4j.LoggerFactory;
  */
 public class RestletComponent extends HeaderFilterStrategyComponent implements RestConsumerFactory,
RestApiConsumerFactory {
     private static final Logger LOG = LoggerFactory.getLogger(RestletComponent.class);
+    private static final Object LOCK = new Object();
 
     private final Map<String, Server> servers = new HashMap<String, Server>();
     private final Map<String, MethodBasedRouter> routers = new HashMap<String, MethodBasedRouter>();
@@ -87,6 +89,7 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements
R
     private boolean disableStreamCache;
     private int port;
     private Boolean synchronous;
+    private List<String> enabledConverters;
 
     public RestletComponent() {
         this(new Component());
@@ -149,6 +152,8 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements
R
             setProperties(this, config.getComponentProperties());
         }
 
+        cleanupConverters(enabledConverters);
+
         component.start();
     }
 
@@ -663,6 +668,32 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements
R
         this.synchronous = synchronous;
     }
 
+    public List<String> getEnabledConverters() {
+        return enabledConverters;
+    }
+
+
+    /**
+     * A list of converters to enable as full class name or simple class name.
+     * All the converters automatically registered are enabled if empty or null
+     */
+    public void setEnabledConverters(List<String> enabledConverters) {
+        if (enabledConverters != null && !enabledConverters.isEmpty()) {
+            this.enabledConverters = new ArrayList(enabledConverters);
+        }
+    }
+
+    /**
+     * A comma separated list of converters to enable as full class name or simple
+     * class name. All the converters automatically registered are enabled if
+     * empty or null
+     */
+    public void setEnabledConverters(String enabledConverters) {
+        if (ObjectHelper.isNotEmpty(enabledConverters)) {
+            this.enabledConverters = Arrays.asList(enabledConverters.split(","));
+        }
+    }
+
     @Override
     public Consumer createConsumer(CamelContext camelContext, Processor processor, String
verb, String basePath, String uriTemplate,
                                    String consumes, String produces, RestConfiguration configuration,
Map<String, Object> parameters) throws Exception {
@@ -771,4 +802,20 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements
R
         // reuse the createConsumer method we already have. The api need to use GET and match
on uri prefix
         return createConsumer(camelContext, processor, "GET", contextPath, null, null, null,
configuration, parameters);
     }
+
+    protected static void cleanupConverters(List<String> converters) {
+        if (converters != null && !converters.isEmpty()) {
+            // To avoid race conditions this operation relies on a global lock, we
+            // could have used Engine's instance as lock so we'd lock only operations
+            // on the same instance but we do not know how Engine is used by the
+            // restlet framework
+            synchronized (LOCK) {
+                Engine.getInstance().getRegisteredConverters().removeIf(
+                    converter ->
+                        !converters.contains(converter.getClass().getName())
+                        && !converters.contains(converter.getClass().getSimpleName())
+                );
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/8fb1874e/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
index 5543313..5fb8e43 100644
--- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
+++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
@@ -41,7 +41,6 @@ import org.restlet.data.Method;
 @UriEndpoint(scheme = "restlet", title = "Restlet", syntax = "restlet:protocol:host:port/uriPattern",
         consumerClass = RestletConsumer.class, label = "rest", lenientProperties = true)
 public class RestletEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware
{
-
     private static final int DEFAULT_PORT = 80;
     private static final String DEFAULT_PROTOCOL = "http";
     private static final String DEFAULT_HOST = "localhost";

http://git-wip-us.apache.org/repos/asf/camel/blob/8fb1874e/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletConfigurationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletConfigurationTest.java
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletConfigurationTest.java
new file mode 100644
index 0000000..54a207d
--- /dev/null
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletConfigurationTest.java
@@ -0,0 +1,68 @@
+/**
+ * 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.camel.component.restlet;
+
+import java.util.Optional;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+import org.restlet.engine.Engine;
+import org.restlet.engine.converter.ConverterHelper;
+import org.restlet.ext.gson.GsonConverter;
+import org.restlet.ext.jackson.JacksonConverter;
+
+public class RestletConfigurationTest extends RestletTestSupport {
+    @Override
+    protected void doPreSetup() {
+        assertPresent(GsonConverter.class);
+        assertPresent(JacksonConverter.class);
+    }
+
+    @Test
+    public void testConfiguration() throws Exception {
+        assertNotPresent(GsonConverter.class);
+        assertPresent(JacksonConverter.class);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                restConfiguration()
+                    .component("restlet")
+                    .componentProperty("enabledConverters", "JacksonConverter");
+
+                from("direct:start")
+                    .to("restlet:http://localhost:" + portNum + "/users/1/basic")
+                    .to("log:reply");
+            }
+        };
+    }
+
+    protected <T extends ConverterHelper> Optional<ConverterHelper> findByType(Class<T>
type) {
+        return Engine.getInstance().getRegisteredConverters().stream().filter(type::isInstance).findFirst();
+    }
+
+    protected <T extends ConverterHelper> void assertPresent(Class<T> type) {
+        assertTrue(type.getSimpleName(), findByType(type).isPresent());
+    }
+
+    protected <T extends ConverterHelper> void assertNotPresent(Class<T> type)
{
+        assertFalse(type.getSimpleName(), findByType(type).isPresent());
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/8fb1874e/parent/pom.xml
----------------------------------------------------------------------
diff --git a/parent/pom.xml b/parent/pom.xml
index 3e03488..da8c3ec 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -465,6 +465,11 @@
     <regexp-bundle-version>1.4_1</regexp-bundle-version>
     <rest-assured-version>2.7.0</rest-assured-version>
     <restlet-version>2.3.6</restlet-version>
+    <restlet-jackson-version>2.4.4</restlet-jackson-version>
+    <restlet-woodstox-version>4.3.0</restlet-woodstox-version>
+    <restlet-yaml-version>1.13</restlet-yaml-version>
+    <restlet-gson-version>2.3.1</restlet-gson-version>
+    <restlet-joda-time-version>2.3</restlet-joda-time-version>
     <rhino-bundle-version>1.7R2_3</rhino-bundle-version>
     <rhino-version>1.7R2</rhino-version>
     <rome-bundle-version>1.0_3</rome-bundle-version>
@@ -2819,6 +2824,16 @@
         <artifactId>org.restlet.ext.httpclient</artifactId>
         <version>${restlet-version}</version>
       </dependency>
+      <dependency>
+        <groupId>org.restlet.jse</groupId>
+        <artifactId>org.restlet.ext.jackson</artifactId>
+        <version>${restlet-version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.restlet.jse</groupId>
+        <artifactId>org.restlet.ext.gson</artifactId>
+        <version>${restlet-version}</version>
+      </dependency>
 
       <!-- need this to use the new 'maven-archetype' packaging type -->
       <dependency>

http://git-wip-us.apache.org/repos/asf/camel/blob/8fb1874e/platforms/karaf/features/src/main/resources/features.xml
----------------------------------------------------------------------
diff --git a/platforms/karaf/features/src/main/resources/features.xml b/platforms/karaf/features/src/main/resources/features.xml
index d3cc39f..e26b738 100644
--- a/platforms/karaf/features/src/main/resources/features.xml
+++ b/platforms/karaf/features/src/main/resources/features.xml
@@ -1317,6 +1317,28 @@
     <bundle dependency='true'>mvn:http://maven.restlet.org@id=restlet!org.restlet.osgi/org.restlet.ext.httpclient/${restlet-version}</bundle>
     <bundle>mvn:org.apache.camel/camel-restlet/${project.version}</bundle>
   </feature>
+  <feature name='camel-restlet-jackson' version='${project.version}' resolver='(obr)'
start-level='50'>
+    <feature version='${project.version}'>camel-restlet</feature>
+    <bundle dependency='true'>mvn:org.codehaus.woodstox/stax2-api/${stax2-api-bundle-version}</bundle>
+    <bundle dependency='true'>mvn:org.codehaus.woodstox/woodstox-core-asl/${restlet-woodstox-version}</bundle>
+    <bundle dependency='true'>mvn:org.yaml/snakeyaml/${restlet-yaml-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-core/${restlet-jackson-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-databind/${restlet-jackson-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-annotations/${restlet-jackson-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.dataformat/jackson-dataformat-csv/${restlet-jackson-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.dataformat/jackson-dataformat-smile/${restlet-jackson-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.dataformat/jackson-dataformat-xml/${restlet-jackson-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/${restlet-jackson-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.module/jackson-module-jaxb-annotations/${restlet-jackson-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.module/jackson-module-jsonSchema/${restlet-jackson-version}</bundle>
+    <bundle>mvn:http://maven.restlet.org@id=restlet!org.restlet.osgi/org.restlet.ext.jackson/${restlet-version}</bundle>
+  </feature>
+  <feature name='camel-restlet-gson' version='${project.version}' resolver='(obr)' start-level='50'>
+    <feature version='${project.version}'>camel-restlet</feature>
+    <bundle dependency='true'>mvn:joda-time/joda-time/${restlet-joda-time-version}</bundle>
+    <bundle dependency='true'>mvn:com.google.code.gson/gson/${restlet-gson-version}</bundle>
+    <bundle>mvn:http://maven.restlet.org@id=restlet!org.restlet.osgi/org.restlet.ext.gson/${restlet-version}</bundle>
+  </feature>
   <feature name='camel-rmi' version='${project.version}' resolver='(obr)' start-level='50'>
     <feature version='${project.version}'>camel-core</feature>
     <bundle>mvn:org.apache.camel/camel-rmi/${project.version}</bundle>


Mime
View raw message