Skip to content

Commit

Permalink
CAMEL-6469: http component now have header with response status line …
Browse files Browse the repository at this point in the history
…text as well (the code is already there)
  • Loading branch information
davsclaus committed Jul 11, 2015
1 parent 2dac9aa commit a4828fa
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions camel-core/src/main/java/org/apache/camel/Exchange.java
Expand Up @@ -145,6 +145,7 @@ public interface Exchange {
String HTTP_QUERY = "CamelHttpQuery";
String HTTP_RAW_QUERY = "CamelHttpRawQuery";
String HTTP_RESPONSE_CODE = "CamelHttpResponseCode";
String HTTP_RESPONSE_TEXT = "CamelHttpResponseText";
String HTTP_URI = "CamelHttpUri";
String HTTP_URL = "CamelHttpUrl";
String HTTP_CHUNKED = "CamelHttpChunked";
Expand Down
Expand Up @@ -191,6 +191,7 @@ public void onStatusReceived(AhcEndpoint endpoint, Exchange exchange, HttpRespon
// Just filter the http protocol headers
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), httpProtocolHeaderFilterStrategy, false);
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, responseStatus.getStatusCode());
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_TEXT, responseStatus.getStatusText());
}

@Override
Expand Down
Expand Up @@ -31,6 +31,7 @@ public void testAhcProduce() throws Exception {
getMockEndpoint("mock:result").expectedHeaderReceived("foo", 123);
getMockEndpoint("mock:result").expectedHeaderReceived("bar", "cool");
getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 200);
getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.HTTP_RESPONSE_TEXT, "OK");
getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.CONTENT_LENGTH, 9);

Map<String, Object> headers = new HashMap<String, Object>();
Expand Down
Expand Up @@ -27,6 +27,7 @@ public class AhcProduceNoThrowExceptionOnFailureTest extends BaseAhcTest {
public void testAhcProduce() throws Exception {
getMockEndpoint("mock:result").expectedBodiesReceived("Does not work");
getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 500);
getMockEndpoint("mock:result").expectedHeaderReceived(Exchange.HTTP_RESPONSE_TEXT, "Server Error");

template.sendBody("direct:start", null);

Expand Down
Expand Up @@ -91,6 +91,7 @@ protected Exchange doReceive(int timeout) {
}
}
message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
message.setHeader(Exchange.HTTP_RESPONSE_TEXT, method.getStatusText());

return exchange;
} catch (IOException e) {
Expand Down
Expand Up @@ -173,6 +173,7 @@ protected void populateResponse(Exchange exchange, HttpMethod method, Message in
Message answer = exchange.getOut();

answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
answer.setHeader(Exchange.HTTP_RESPONSE_TEXT, method.getStatusText());
answer.setBody(response);

// propagate HTTP response headers
Expand Down
Expand Up @@ -100,6 +100,9 @@ protected Exchange doReceive(int timeout) {
}
}
message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
if (response.getStatusLine() != null) {
message.setHeader(Exchange.HTTP_RESPONSE_TEXT, response.getStatusLine().getReasonPhrase());
}

return exchange;
} catch (IOException e) {
Expand Down
Expand Up @@ -189,6 +189,9 @@ protected void populateResponse(Exchange exchange, HttpRequestBase httpRequest,
Message answer = exchange.getOut();

answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
if (httpResponse.getStatusLine() != null) {
answer.setHeader(Exchange.HTTP_RESPONSE_TEXT, httpResponse.getStatusLine().getReasonPhrase());
}
answer.setBody(response);

// propagate HTTP response headers
Expand Down
Expand Up @@ -97,6 +97,7 @@ public void process(Exchange exchange) throws Exception {
Message out = exchange.getOut();
Map<String, Object> headers = out.getHeaders();
assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, headers.get(Exchange.HTTP_RESPONSE_CODE));
assertEquals("Not Implemented", headers.get(Exchange.HTTP_RESPONSE_TEXT));
assertEquals("26", headers.get("Content-Length"));
assertNotNull("Should have Content-Type header", headers.get("Content-Type"));
assertEquals("PATCH method not supported", out.getBody(String.class));
Expand Down
Expand Up @@ -101,6 +101,7 @@ public void process(Exchange exchange) throws Exception {

assertNotNull(out);
assertEquals(HttpStatus.SC_OK, out.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
assertEquals("OK", out.getOut().getHeader(Exchange.HTTP_RESPONSE_TEXT));
assertEquals("Bye World", out.getOut().getBody(String.class));
}

Expand Down
Expand Up @@ -42,6 +42,7 @@ public void process(Exchange exchange) throws Exception {
assertNotNull(reply);
assertTrue(reply.getOut().getBody(String.class).startsWith("java.lang.IllegalArgumentException: I cannot do this"));
assertEquals(500, reply.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
assertEquals("Server Error", reply.getOut().getHeader(Exchange.HTTP_RESPONSE_TEXT));
}

@Override
Expand Down
Expand Up @@ -265,7 +265,7 @@ public void populateCamelHeaders(HttpResponse response, Map<String, Object> head
LOG.trace("populateCamelHeaders: {}", response);

headers.put(Exchange.HTTP_RESPONSE_CODE, response.getStatus().getCode());
headers.put(NettyHttpConstants.HTTP_RESPONSE_TEXT, response.getStatus().getReasonPhrase());
headers.put(Exchange.HTTP_RESPONSE_TEXT, response.getStatus().getReasonPhrase());

for (String name : response.headers().names()) {
// mapping the content-type
Expand Down
Expand Up @@ -16,14 +16,17 @@
*/
package org.apache.camel.component.netty.http;

import org.apache.camel.Exchange;

/**
* Netty HTTP constants.
*/
public final class NettyHttpConstants {

public static final String CONTENT_TYPE_JAVA_SERIALIZED_OBJECT = "application/x-java-serialized-object";
public static final String CONTENT_TYPE_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String HTTP_RESPONSE_TEXT = "CamelHttpResponseText";
@Deprecated
public static final String HTTP_RESPONSE_TEXT = Exchange.HTTP_RESPONSE_TEXT;
public static final String HTTP_AUTHENTICATION = "CamelHttpAuthentication";

private NettyHttpConstants() {
Expand Down
Expand Up @@ -42,7 +42,7 @@ public void process(Exchange exchange) throws Exception {
// except for the response code and response text
Map<String, Object> headers = out.getOut().getHeaders();
for (String key : headers.keySet()) {
if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE) && !key.equalsIgnoreCase(NettyHttpConstants.HTTP_RESPONSE_TEXT)) {
if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE) && !key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_TEXT)) {
assertTrue("Should not contain any Camel internal headers", !key.toLowerCase().startsWith("camel"));
}
}
Expand Down
Expand Up @@ -267,7 +267,7 @@ public void populateCamelHeaders(FullHttpResponse response, Map<String, Object>
LOG.trace("populateCamelHeaders: {}", response);

headers.put(Exchange.HTTP_RESPONSE_CODE, response.getStatus().code());
headers.put(NettyHttpConstants.HTTP_RESPONSE_TEXT, response.getStatus().reasonPhrase());
headers.put(Exchange.HTTP_RESPONSE_TEXT, response.getStatus().reasonPhrase());

for (String name : response.headers().names()) {
// mapping the content-type
Expand Down
Expand Up @@ -16,14 +16,17 @@
*/
package org.apache.camel.component.netty4.http;

import org.apache.camel.Exchange;

/**
* Netty HTTP constants.
*/
public final class NettyHttpConstants {

public static final String CONTENT_TYPE_JAVA_SERIALIZED_OBJECT = "application/x-java-serialized-object";
public static final String CONTENT_TYPE_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String HTTP_RESPONSE_TEXT = "CamelHttpResponseText";
@Deprecated
public static final String HTTP_RESPONSE_TEXT = Exchange.HTTP_RESPONSE_TEXT;
public static final String HTTP_AUTHENTICATION = "CamelHttpAuthentication";

private NettyHttpConstants() {
Expand Down
Expand Up @@ -42,7 +42,7 @@ public void process(Exchange exchange) throws Exception {
// except for the response code and response text
Map<String, Object> headers = out.getOut().getHeaders();
for (String key : headers.keySet()) {
if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE) && !key.equalsIgnoreCase(NettyHttpConstants.HTTP_RESPONSE_TEXT)) {
if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE) && !key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_TEXT)) {
assertTrue("Should not contain any Camel internal headers", !key.toLowerCase().startsWith("camel"));
}
}
Expand Down

0 comments on commit a4828fa

Please sign in to comment.