Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.rest;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.Registry;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class FromRestGetContentTypeEmptyBodyTest extends ContextTestSupport {

@Override
protected Registry createCamelRegistry() throws Exception {
Registry jndi = super.createCamelRegistry();
jndi.bind("dummy-rest", new DummyRestConsumerFactory());
return jndi;
}

@Test
public void testNoContentTypeOnEmptyBody() {
Exchange out = template.request("seda:delete-say-hello", exchange -> {
// no body
});

assertNotNull(out);
assertNull(out.getMessage().getBody(), "Body should be null");
assertNull(out.getMessage().getHeader(Exchange.CONTENT_TYPE),
"Content-Type should not be set on a body-less response");
}

@Test
public void testContentTypeSetOnNonEmptyBody() {
Exchange out = template.request("seda:get-say-hello", exchange -> {
// no body
});

assertNotNull(out);
assertEquals("{ \"name\" : \"Donald\" }", out.getMessage().getBody());
assertEquals("application/json", out.getMessage().getHeader(Exchange.CONTENT_TYPE));
}

@Test
public void testContentTypeJsonWhenMultiValueProduces() {
Exchange out = template.request("seda:get-say-multi", exchange -> {
// no body
});

assertNotNull(out);
assertEquals("{ \"name\" : \"Donald\" }", out.getMessage().getBody());
assertEquals("application/json", out.getMessage().getHeader(Exchange.CONTENT_TYPE));
}

@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
restConfiguration().host("localhost");

rest("/say/hello")
.produces("application/json")
.delete().to("direct:delete")
.get().to("direct:hello");

rest("/say/multi")
.produces("application/json,text/plain")
.get().to("direct:hello");

from("direct:delete")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(204))
.setBody(constant(null));

from("direct:hello")
.setBody(constant("{ \"name\" : \"Donald\" }"));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ private void marshal(Exchange exchange, Map<String, Object> state) {
// need to prepare exchange first
ExchangeHelper.prepareOutToIn(exchange);

// is the body empty (no Content-Type should be set for body-less responses such as 204 No Content)
if (exchange.getMessage().getBody() == null) {
return;
}

// ensure there is a content type header (even if binding is off)
ensureHeaderContentType(produces, isXml, isJson, exchange);

Expand All @@ -405,11 +410,6 @@ private void marshal(Exchange exchange, Map<String, Object> state) {
return;
}

// is the body empty
if (exchange.getMessage().getBody() == null) {
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);
Expand Down Expand Up @@ -484,27 +484,18 @@ private void setOutputDataType(Exchange exchange, DataType type) {
}

private void ensureHeaderContentType(String contentType, boolean isXml, boolean isJson, Exchange exchange) {
// favor given content type
if (contentType != null) {
String type = ExchangeHelper.getContentType(exchange);
if (type == null) {
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, contentType);
}
String type = ExchangeHelper.getContentType(exchange);
if (type != null) {
return;
}

// favor json over xml
// favor json over xml as a concrete single media type
if (isJson) {
// make sure there is a content-type with json
String type = ExchangeHelper.getContentType(exchange);
if (type == null) {
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
}
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
} else if (isXml) {
// make sure there is a content-type with xml
String type = ExchangeHelper.getContentType(exchange);
if (type == null) {
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml");
}
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml");
} else if (contentType != null) {
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, contentType);
}
}

Expand Down
Loading