CAMEL-9596: rest-dsl with jetty. Allow to use custom http binding
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d59cac1d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d59cac1d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d59cac1d Branch: refs/heads/camel-2.16.x Commit: d59cac1d4aa6410637d76c823bcbf71757ac1bb4 Parents: 3f25826 Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Feb 13 13:22:20 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Feb 13 13:23:00 2016 +0100 ---------------------------------------------------------------------- .../component/jetty/JettyHttpComponent.java | 10 +-- .../jetty/rest/MyCustomHttpBinding.java | 39 ++++++++++++ .../rest/RestJettyGetCustomHttpBindingTest.java | 64 ++++++++++++++++++++ .../component/jetty/rest/RestJettyGetTest.java | 12 +--- 4 files changed, 110 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d59cac1d/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java index adeb3c8..c64513e 100644 --- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java +++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java @@ -1069,10 +1069,12 @@ public abstract class JettyHttpComponent extends HttpCommonComponent implements JettyHttpEndpoint endpoint = camelContext.getEndpoint(url, JettyHttpEndpoint.class); setProperties(endpoint, parameters); - // disable this filter as we want to use ours - endpoint.setEnableMultipartFilter(false); - // use the rest binding - endpoint.setBinding(new JettyRestHttpBinding(endpoint)); + if (!map.containsKey("httpBindingRef")) { + // use the rest binding, if not using a custom http binding + endpoint.setHttpBinding(new JettyRestHttpBinding(endpoint)); + // disable this filter as we want to use ours + endpoint.setEnableMultipartFilter(false); + } // configure consumer properties Consumer consumer = endpoint.createConsumer(processor); http://git-wip-us.apache.org/repos/asf/camel/blob/d59cac1d/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/MyCustomHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/MyCustomHttpBinding.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/MyCustomHttpBinding.java new file mode 100644 index 0000000..89d317a --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/MyCustomHttpBinding.java @@ -0,0 +1,39 @@ +/** + * 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.jetty.rest; + +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.jetty.JettyRestHttpBinding; + +public class MyCustomHttpBinding extends JettyRestHttpBinding { + + private String greeting; + + public MyCustomHttpBinding(String greeting) { + this.greeting = greeting; + } + + @Override + public void doWriteResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException { + message.setBody(greeting + message.getBody()); + super.doWriteResponse(message, response, exchange); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/d59cac1d/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetCustomHttpBindingTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetCustomHttpBindingTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetCustomHttpBindingTest.java new file mode 100644 index 0000000..ef6b68b --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetCustomHttpBindingTest.java @@ -0,0 +1,64 @@ +/** + * 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.jetty.rest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.apache.camel.impl.JndiRegistry; +import org.junit.Test; + +public class RestJettyGetCustomHttpBindingTest extends BaseJettyTest { + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("mybinding", new MyCustomHttpBinding("I was here;")); + return jndi; + } + + @Test + public void testJettyProducerGet() throws Exception { + String out = template.requestBody("http://localhost:" + getPort() + "/users/123/basic", null, String.class); + assertEquals("I was here;123;Donald Duck", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use jetty on localhost with the given port + restConfiguration().component("jetty").host("localhost").port(getPort()).endpointProperty("httpBindingRef", "#mybinding"); + + // use the rest DSL to define the rest services + rest("/users/") + .get("{id}/basic") + .route() + .to("mock:input") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + String id = exchange.getIn().getHeader("id", String.class); + exchange.getOut().setBody(id + ";Donald Duck"); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/d59cac1d/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java index 5022681..04a26e2 100644 --- a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyGetTest.java @@ -20,20 +20,10 @@ import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.jetty.BaseJettyTest; -import org.apache.camel.component.jetty.JettyRestHttpBinding; -import org.apache.camel.impl.JndiRegistry; import org.junit.Test; public class RestJettyGetTest extends BaseJettyTest { - - @Override - protected JndiRegistry createRegistry() throws Exception { - JndiRegistry jndi = super.createRegistry(); - jndi.bind("mybinding", new JettyRestHttpBinding()); - return jndi; - } - @Test public void testJettyProducerGet() throws Exception { String out = template.requestBody("http://localhost:" + getPort() + "/users/123/basic", null, String.class); @@ -46,7 +36,7 @@ public class RestJettyGetTest extends BaseJettyTest { @Override public void configure() throws Exception { // configure to use jetty on localhost with the given port - restConfiguration().component("jetty").host("localhost").port(getPort()).endpointProperty("httpBindingRef", "#mybinding"); + restConfiguration().component("jetty").host("localhost").port(getPort()); // use the rest DSL to define the rest services rest("/users/")