CAMEL-9625: rest-dsl - Marshal should only happen if content-type is json or xml
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b51cf41a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b51cf41a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b51cf41a Branch: refs/heads/camel-2.16.x Commit: b51cf41a2d306d3423f0b10f806e8c567d959e8a Parents: ff1578c Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Feb 19 19:45:01 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Feb 19 19:45:17 2016 +0100 ---------------------------------------------------------------------- .../processor/binding/RestBindingProcessor.java | 13 +++- .../rest/RestJettyCustomContentTypeTest.java | 77 ++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/b51cf41a/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java index f901f31..41744b6 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/processor/binding/RestBindingProcessor.java @@ -331,12 +331,21 @@ public class RestBindingProcessor extends ServiceSupport implements AsyncProcess return; } + String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); + // need to lower-case so the contains check below can match if using upper case + contentType = contentType.toLowerCase(Locale.US); try { // favor json over xml if (isJson && jsonMarshal != null) { - jsonMarshal.process(exchange); + // only marshal if its json content type + if (contentType.contains("json")) { + jsonMarshal.process(exchange); + } } else if (isXml && xmlMarshal != null) { - xmlMarshal.process(exchange); + // only marshal if its xml content type + if (contentType.contains("xml")) { + xmlMarshal.process(exchange); + } } else { // we could not bind if (bindingMode.equals("auto")) { http://git-wip-us.apache.org/repos/asf/camel/blob/b51cf41a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyCustomContentTypeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyCustomContentTypeTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyCustomContentTypeTest.java new file mode 100644 index 0000000..3019ac9 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyCustomContentTypeTest.java @@ -0,0 +1,77 @@ +/** + * 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.model.rest.RestBindingMode; +import org.junit.Test; + +public class RestJettyCustomContentTypeTest extends BaseJettyTest { + + @Test + public void testBlob() throws Exception { + Exchange out = template.request("http://localhost:" + getPort() + "/users/blob", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + } + }); + + assertEquals("application/foobar", out.getOut().getHeader(Exchange.CONTENT_TYPE)); + assertEquals("Some foobar stuff goes here", out.getOut().getBody(String.class)); + } + + @Test + public void testJSon() throws Exception { + Exchange out = template.request("http://localhost:" + getPort() + "/users/lives", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + } + }); + + assertEquals("application/json", out.getOut().getHeader(Exchange.CONTENT_TYPE)); + assertEquals("{\"iso\":\"EN\",\"country\":\"England\"}", out.getOut().getBody(String.class)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // enable json binding + restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.json); + + rest("/users/").consumes("application/json").produces("application/json") + .get("blob").to("direct:blob") + .get("lives").to("direct:lives"); + + from("direct:blob") + // but send back non json data + .setHeader(Exchange.CONTENT_TYPE, constant("application/foobar")) + .transform().constant("Some foobar stuff goes here"); + + CountryPojo country = new CountryPojo(); + country.setIso("EN"); + country.setCountry("England"); + from("direct:lives").transform().constant(country); + } + }; + } + +}