You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/12/04 09:11:11 UTC

[2/2] camel git commit: CAMEL-7880: rest-dsl with custom data format must refer to a name, not a preconfigured instance from the registry.

CAMEL-7880: rest-dsl with custom data format must refer to a name, not a preconfigured instance from the registry.


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

Branch: refs/heads/camel-2.14.x
Commit: 09051a4523e4e75498ad151278fa98f0cd8b7271
Parents: 6be4e09
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Dec 4 09:10:04 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Dec 4 09:10:58 2014 +0100

----------------------------------------------------------------------
 .../camel/model/rest/RestBindingDefinition.java | 44 ++++++-------
 .../model/rest/RestConfigurationDefinition.java |  4 ++
 .../org/apache/camel/spi/RestConfiguration.java | 10 ++-
 .../RestRestletCustomDataFormatInvalidTest.java | 69 ++++++++++++++++++++
 .../RestRestletCustomDataFormatTest.java        | 47 +++++++++++++
 5 files changed, 150 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/09051a45/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
index 8d0e9ff..3f07d26 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
@@ -84,20 +84,19 @@ public class RestBindingDefinition extends NoOutputDefinition<RestBindingDefinit
         }
 
         // setup json data format
-        DataFormat json;
-        DataFormat outJson;
-
         String name = context.getRestConfiguration().getJsonDataFormat();
-        if (name == null) {
-            // this will create a new instance as we use the default name
-            name = "json-jackson";
-            json = context.resolveDataFormat(name);
-            outJson = context.resolveDataFormat(name);
+        if (name != null) {
+            // must only be a name, not refer to an existing instance
+            Object instance = context.getRegistry().lookupByName(name);
+            if (instance != null) {
+                throw new IllegalArgumentException("JsonDataFormat name: " + name + " must not be an existing bean instance from the registry");
+            }
         } else {
-            json = context.resolveDataFormat(name);
-            // for out we need to make a new instance of the same class
-            outJson = context.getInjector().newInstance(json.getClass(), json);
+            name = "json-jackson";
         }
+        // this will create a new instance as the name was not already pre-created
+        DataFormat json = context.resolveDataFormat(name);
+        DataFormat outJson = context.resolveDataFormat(name);
 
         // is json binding required?
         if (mode.contains("json") && json == null) {
@@ -131,21 +130,20 @@ public class RestBindingDefinition extends NoOutputDefinition<RestBindingDefinit
         }
 
         // setup xml data format
-        // setup json data format
-        DataFormat jaxb;
-        DataFormat outJaxb;
-
         name = context.getRestConfiguration().getXmlDataFormat();
-        if (name == null) {
-            // this will create a new instance as we use the default name
-            name = "jaxb";
-            jaxb = context.resolveDataFormat(name);
-            outJaxb = context.resolveDataFormat(name);
+        if (name != null) {
+            // must only be a name, not refer to an existing instance
+            Object instance = context.getRegistry().lookupByName(name);
+            if (instance != null) {
+                throw new IllegalArgumentException("XmlDataFormat name: " + name + " must not be an existing bean instance from the registry");
+            }
         } else {
-            jaxb = context.resolveDataFormat(name);
-            // for out we need to make a new instance of the same class
-            outJaxb = context.getInjector().newInstance(jaxb.getClass(), json);
+            name = "jaxb";
         }
+        // this will create a new instance as the name was not already pre-created
+        DataFormat jaxb = context.resolveDataFormat(name);
+        DataFormat outJaxb = context.resolveDataFormat(name);
+
         // is xml binding required?
         if (mode.contains("xml") && jaxb == null) {
             throw new IllegalArgumentException("XML DataFormat " + name + " not found.");

http://git-wip-us.apache.org/repos/asf/camel/blob/09051a45/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java
index 7cd175b..50d3879 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java
@@ -271,6 +271,8 @@ public class RestConfigurationDefinition {
 
     /**
      * To use a specific json data format
+     * <p/>
+     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
      *
      * @param name  name of the data format to {@link org.apache.camel.CamelContext#resolveDataFormat(java.lang.String) resolve}
      */
@@ -281,6 +283,8 @@ public class RestConfigurationDefinition {
 
     /**
      * To use a specific XML data format
+     * <p/>
+     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
      *
      * @param name  name of the data format to {@link org.apache.camel.CamelContext#resolveDataFormat(java.lang.String) resolve}
      */

http://git-wip-us.apache.org/repos/asf/camel/blob/09051a45/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java
index d945aac..50d64f7 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java
@@ -214,6 +214,8 @@ public class RestConfiguration {
 
     /**
      * Gets the name of the json data format.
+     * <p/>
+     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
      *
      * @return the name, or <tt>null</tt> to use default
      */
@@ -223,6 +225,8 @@ public class RestConfiguration {
 
     /**
      * Sets a custom json data format to be used
+     * <p/>
+     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
      *
      * @param name name of the data format
      */
@@ -232,6 +236,8 @@ public class RestConfiguration {
 
     /**
      * Gets the name of the xml data format.
+     * <p/>
+     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
      *
      * @return the name, or <tt>null</tt> to use default
      */
@@ -240,7 +246,9 @@ public class RestConfiguration {
     }
 
     /**
-     * Sets a custom xml data format to be used
+     * Sets a custom xml data format to be used.
+     * <p/>
+     * <b>Important:</b> This option is only for setting a custom name of the data format, not to refer to an existing data format instance.
      *
      * @param name name of the data format
      */

http://git-wip-us.apache.org/repos/asf/camel/blob/09051a45/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletCustomDataFormatInvalidTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletCustomDataFormatInvalidTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletCustomDataFormatInvalidTest.java
new file mode 100644
index 0000000..e75c291
--- /dev/null
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletCustomDataFormatInvalidTest.java
@@ -0,0 +1,69 @@
+/**
+ * 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 org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jackson.JacksonDataFormat;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class RestRestletCustomDataFormatInvalidTest extends RestletTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("bla", new JacksonDataFormat());
+        return jndi;
+    }
+
+    @Test
+    public void testCustom() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.json).jsonDataFormat("bla");
+
+                // use the rest DSL to define the rest services
+                rest("/users/")
+                    .post("lives").type(UserPojo.class).outType(CountryPojo.class)
+                        .route()
+                            .choice()
+                                .when().simple("${body.id} < 100")
+                                    .bean(new UserErrorService(), "idToLowError")
+                                .otherwise()
+                                    .bean(new UserService(), "livesWhere");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            assertEquals("JsonDataFormat name: bla must not be an existing bean instance from the registry", e.getCause().getMessage());
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/09051a45/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletCustomDataFormatTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletCustomDataFormatTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletCustomDataFormatTest.java
new file mode 100644
index 0000000..3c04fd6
--- /dev/null
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletCustomDataFormatTest.java
@@ -0,0 +1,47 @@
+/**
+ * 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 org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+
+/**
+ * @version 
+ */
+public class RestRestletCustomDataFormatTest extends RestRestletPojoInOutCustomErrorResponseTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.json).jsonDataFormat("json-jackson");
+
+                // use the rest DSL to define the rest services
+                rest("/users/")
+                    .post("lives").type(UserPojo.class).outType(CountryPojo.class)
+                        .route()
+                            .choice()
+                                .when().simple("${body.id} < 100")
+                                    .bean(new UserErrorService(), "idToLowError")
+                                .otherwise()
+                                    .bean(new UserService(), "livesWhere");
+            }
+        };
+    }
+
+}