CAMEL-7619: Rest DSL - adding support for xml/json binding using Camel's data formats. Work in progress.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cae408e0 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cae408e0 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cae408e0 Branch: refs/heads/master Commit: cae408e0d0963ae452adaa79713a8c4860988d02 Parents: ccb9836 Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Jul 25 12:42:04 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Jul 25 12:42:04 2014 +0200 ---------------------------------------------------------------------- .../camel/model/rest/RestBindingDefinition.java | 7 ++- .../org/apache/camel/spi/RestConfiguration.java | 2 +- .../restlet/RestRestletPojoInOutTest.java | 4 +- .../RestRestletPostJsonJaxbPojoTest.java | 4 +- .../RestRestletPostJsonPojoListTest.java | 4 +- .../restlet/RestRestletPostJsonPojoTest.java | 4 +- .../restlet/RestRestletPostXmlJaxbPojoTest.java | 4 +- components/camel-spark-rest/pom.xml | 13 +++++ .../sparkrest/DefaultSparkBinding.java | 2 + .../camel/component/sparkrest/CountryPojo.java | 40 +++++++++++++++ .../sparkrest/RestCamelSparkPojoInOutTest.java | 52 ++++++++++++++++++++ .../camel/component/sparkrest/UserPojo.java | 40 +++++++++++++++ .../camel/component/sparkrest/UserService.java | 33 +++++++++++++ 13 files changed, 201 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/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 80535f3..e0a0c32 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 @@ -71,8 +71,6 @@ public class RestBindingDefinition extends NoOutputDefinition { return "rest"; } - // TODO: allow to configure if json/xml only or auto detect (now) - @Override public Processor createProcessor(RouteContext routeContext) throws Exception { @@ -84,6 +82,11 @@ public class RestBindingDefinition extends NoOutputDefinition { mode = bindingMode.name(); } + if (mode == null || "off".equals(mode)) { + // binding mode is off, so create a off mode binding processor + return new RestBindingProcessor(null, null, null, null, consumes, produces, mode); + } + // setup json data format String name = jsonDataFormat; if (name == null) { http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/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 fc5367b..78bf4d2 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 @@ -32,7 +32,7 @@ public class RestConfiguration { private String scheme; private String host; private int port; - private RestBindingMode bindingMode = RestBindingMode.auto; + private RestBindingMode bindingMode = RestBindingMode.off; private Map<String, Object> componentProperties; private Map<String, Object> endpointProperties; private Map<String, Object> consumerProperties; http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPojoInOutTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPojoInOutTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPojoInOutTest.java index 824e4e1..860668c 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPojoInOutTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPojoInOutTest.java @@ -17,6 +17,7 @@ package org.apache.camel.component.restlet; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.rest.RestBindingMode; import org.junit.Test; /** @@ -39,7 +40,8 @@ public class RestRestletPojoInOutTest extends RestletTestSupport { @Override public void configure() throws Exception { // configure to use restlet on localhost with the given port - restConfiguration().component("restlet").host("localhost").port(portNum); + // and enable auto binding mode + restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto); // use the rest DSL to define the rest services rest("/users/") http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java index 1135c78..18819c8 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java @@ -18,6 +18,7 @@ package org.apache.camel.component.restlet; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; import org.junit.Test; /** @@ -48,7 +49,8 @@ public class RestRestletPostJsonJaxbPojoTest extends RestletTestSupport { @Override public void configure() throws Exception { // configure to use restlet on localhost with the given port - restConfiguration().component("restlet").host("localhost").port(portNum); + // and enable auto binding mode + restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto); // use the rest DSL to define the rest services rest("/users/") http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoListTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoListTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoListTest.java index 0a2cf46..b0a8cbe 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoListTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoListTest.java @@ -20,6 +20,7 @@ import java.util.List; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; import org.junit.Test; /** @@ -55,7 +56,8 @@ public class RestRestletPostJsonPojoListTest extends RestletTestSupport { @Override public void configure() throws Exception { // configure to use restlet on localhost with the given port - restConfiguration().component("restlet").host("localhost").port(portNum); + // and enable auto binding mode + restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto); // use the rest DSL to define the rest services rest("/users/") http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java index 2bcb379..9bc01a7 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java @@ -18,6 +18,7 @@ package org.apache.camel.component.restlet; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; import org.junit.Test; /** @@ -48,7 +49,8 @@ public class RestRestletPostJsonPojoTest extends RestletTestSupport { @Override public void configure() throws Exception { // configure to use restlet on localhost with the given port - restConfiguration().component("restlet").host("localhost").port(portNum); + // and enable auto binding mode + restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto); // use the rest DSL to define the rest services rest("/users/") http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java index 7a22443..6e22327 100644 --- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java @@ -19,6 +19,7 @@ package org.apache.camel.component.restlet; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.rest.RestBindingMode; import org.junit.Test; /** @@ -66,7 +67,8 @@ public class RestRestletPostXmlJaxbPojoTest extends RestletTestSupport { @Override public void configure() throws Exception { // configure to use restlet on localhost with the given port - restConfiguration().component("restlet").host("localhost").port(portNum); + // and enable auto binding mode + restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto); // use the rest DSL to define the rest services rest("/users/") http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-spark-rest/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-spark-rest/pom.xml b/components/camel-spark-rest/pom.xml index db5b05e..592bb31 100644 --- a/components/camel-spark-rest/pom.xml +++ b/components/camel-spark-rest/pom.xml @@ -89,6 +89,19 @@ <artifactId>camel-http</artifactId> <scope>test</scope> </dependency> + + <!-- for testing rest-dsl --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-jackson</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-jaxb</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/DefaultSparkBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/DefaultSparkBinding.java b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/DefaultSparkBinding.java index 7b43be1..27c74c7 100644 --- a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/DefaultSparkBinding.java +++ b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/DefaultSparkBinding.java @@ -182,6 +182,8 @@ public class DefaultSparkBinding implements SparkBinding { if (body != null) { String str = tc.mandatoryConvertTo(String.class, message.getExchange(), body); response.body(str); + // and must set body to the response body as Spark otherwise may output something else + message.setBody(str); } } http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CountryPojo.java ---------------------------------------------------------------------- diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CountryPojo.java b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CountryPojo.java new file mode 100644 index 0000000..3670c0e --- /dev/null +++ b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/CountryPojo.java @@ -0,0 +1,40 @@ +/** + * 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.sparkrest; + +public class CountryPojo { + + private String iso; + private String country; + + public String getIso() { + return iso; + } + + public void setIso(String iso) { + this.iso = iso; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkPojoInOutTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkPojoInOutTest.java b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkPojoInOutTest.java new file mode 100644 index 0000000..4a97600 --- /dev/null +++ b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/RestCamelSparkPojoInOutTest.java @@ -0,0 +1,52 @@ +/** + * 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.sparkrest; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestCamelSparkPojoInOutTest extends BaseSparkTest { + + @Test + public void testRestletPojoInOut() throws Exception { + String body = "{\"id\": 123, \"name\": \"Donald Duck\"}"; + String out = template.requestBody("http://localhost:" + getPort() + "/users/lives", body, String.class); + + assertNotNull(out); + assertEquals("{\"iso\":\"EN\",\"country\":\"England\"}", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use spark on localhost with the given port + // and enable auto binding mode + restConfiguration().component("spark-rest").host("localhost").port(getPort()).bindingMode(RestBindingMode.auto); + + // use the rest DSL to define the rest services + rest("/users/") + .post("lives").type(UserPojo.class).outType(CountryPojo.class) + .route() + .bean(new UserService(), "livesWhere"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/UserPojo.java ---------------------------------------------------------------------- diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/UserPojo.java b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/UserPojo.java new file mode 100644 index 0000000..32be9c7 --- /dev/null +++ b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/UserPojo.java @@ -0,0 +1,40 @@ +/** + * 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.sparkrest; + +public class UserPojo { + + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/cae408e0/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/UserService.java ---------------------------------------------------------------------- diff --git a/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/UserService.java b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/UserService.java new file mode 100644 index 0000000..2500ec9 --- /dev/null +++ b/components/camel-spark-rest/src/test/java/org/apache/camel/component/sparkrest/UserService.java @@ -0,0 +1,33 @@ +/** + * 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.sparkrest; + +public class UserService { + + public CountryPojo livesWhere(UserPojo user) { + CountryPojo answer = new CountryPojo(); + if (user.getId() < 500) { + answer.setIso("EN"); + answer.setCountry("England"); + } else { + answer.setIso("SE"); + answer.setCountry("Sweden"); + } + return answer; + } + +}