use of org.apache.commons.httpclient.Header in project camel by apache.
the class HttpPollingConsumer method doReceive.
protected Exchange doReceive(int timeout) {
Exchange exchange = endpoint.createExchange();
HttpMethod method = createMethod(exchange);
// set optional timeout in millis
if (timeout > 0) {
method.getParams().setSoTimeout(timeout);
}
try {
// execute request
int responseCode = httpClient.executeMethod(method);
Object body = HttpHelper.readResponseBodyFromInputStream(method.getResponseBodyAsStream(), exchange);
// lets store the result in the output message.
Message message = exchange.getOut();
message.setBody(body);
// lets set the headers
Header[] headers = method.getResponseHeaders();
HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
for (Header header : headers) {
String name = header.getName();
// mapping the content-type
if (name.toLowerCase().equals("content-type")) {
name = Exchange.CONTENT_TYPE;
}
String value = header.getValue();
if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
message.setHeader(name, value);
}
}
message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
message.setHeader(Exchange.HTTP_RESPONSE_TEXT, method.getStatusText());
return exchange;
} catch (IOException e) {
throw new RuntimeCamelException(e);
} finally {
method.releaseConnection();
}
}
use of org.apache.commons.httpclient.Header in project camel by apache.
the class HttpProducer method populateHttpOperationFailedException.
protected Exception populateHttpOperationFailedException(Exchange exchange, HttpMethod method, int responseCode) throws IOException, ClassNotFoundException, URISyntaxException {
Exception answer;
String uri = method.getURI().toString();
String statusText = method.getStatusLine() != null ? method.getStatusLine().getReasonPhrase() : null;
Map<String, String> headers = extractResponseHeaders(method.getResponseHeaders());
// handle cookies
if (getEndpoint().getCookieHandler() != null) {
Map<String, List<String>> m = new HashMap<String, List<String>>();
for (Entry<String, String> e : headers.entrySet()) {
m.put(e.getKey(), Collections.singletonList(e.getValue()));
}
getEndpoint().getCookieHandler().storeCookies(exchange, new URI(method.getURI().getEscapedURI()), m);
}
Object responseBody = extractResponseBody(method, exchange, getEndpoint().isIgnoreResponseBody());
if (transferException && responseBody != null && responseBody instanceof Exception) {
// if the response was a serialized exception then use that
return (Exception) responseBody;
}
// make a defensive copy of the response body in the exception so its detached from the cache
String copy = null;
if (responseBody != null) {
copy = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, responseBody);
}
if (responseCode >= 300 && responseCode < 400) {
String redirectLocation;
Header locationHeader = method.getResponseHeader("location");
if (locationHeader != null) {
redirectLocation = locationHeader.getValue();
answer = new HttpOperationFailedException(uri, responseCode, statusText, redirectLocation, headers, copy);
} else {
// no redirect location
answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
}
} else {
// internal server error (error code 500)
answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy);
}
return answer;
}
use of org.apache.commons.httpclient.Header in project camel by apache.
the class HttpProducer method populateResponse.
protected void populateResponse(Exchange exchange, HttpMethod method, Message in, HeaderFilterStrategy strategy, int responseCode) throws IOException, ClassNotFoundException, URISyntaxException {
//We just make the out message is not create when extractResponseBody throws exception,
Object response = extractResponseBody(method, exchange, getEndpoint().isIgnoreResponseBody());
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
Header[] headers = method.getResponseHeaders();
Map<String, List<String>> m = new HashMap<String, List<String>>();
for (Header header : headers) {
String name = header.getName();
String value = header.getValue();
m.put(name, Collections.singletonList(value));
if (name.toLowerCase().equals("content-type")) {
name = Exchange.CONTENT_TYPE;
exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.getCharsetNameFromContentType(value));
}
// use http helper to extract parameter value as it may contain multiple values
Object extracted = HttpHelper.extractHttpParameterValue(value);
if (strategy != null && !strategy.applyFilterToExternalHeaders(name, extracted, exchange)) {
HttpHelper.appendHeader(answer.getHeaders(), name, extracted);
}
}
// handle cookies
if (getEndpoint().getCookieHandler() != null) {
getEndpoint().getCookieHandler().storeCookies(exchange, new URI(method.getURI().getEscapedURI()), m);
}
// filter the http protocol headers
if (getEndpoint().isCopyHeaders()) {
MessageHelper.copyHeaders(exchange.getIn(), answer, httpProtocolHeaderFilterStrategy, false);
}
}
use of org.apache.commons.httpclient.Header in project camel by apache.
the class EnableCORSTest method testCORSenabled.
@Test
public void testCORSenabled() throws Exception {
HttpClient httpclient = new HttpClient();
HttpMethod httpMethod = new GetMethod("http://localhost:" + getPort2() + "/test2");
httpMethod.addRequestHeader("Origin", "http://localhost:9000");
httpMethod.addRequestHeader("Referer", "http://localhost:9000");
int status = httpclient.executeMethod(httpMethod);
assertEquals("Get a wrong response status", 200, status);
Header responseHeader = httpMethod.getResponseHeader("Access-Control-Allow-Credentials");
assertTrue("CORS not enabled", Boolean.valueOf(responseHeader.getValue()));
}
use of org.apache.commons.httpclient.Header in project camel by apache.
the class EnableCORSTest method testCORSdisabled.
@Test
public void testCORSdisabled() throws Exception {
HttpClient httpclient = new HttpClient();
HttpMethod httpMethod = new GetMethod("http://localhost:" + getPort() + "/test1");
httpMethod.addRequestHeader("Origin", "http://localhost:9000");
httpMethod.addRequestHeader("Referer", "http://localhost:9000");
int status = httpclient.executeMethod(httpMethod);
assertEquals("Get a wrong response status", 200, status);
Header responseHeader = httpMethod.getResponseHeader("Access-Control-Allow-Credentials");
assertNull("Access-Control-Allow-Credentials HEADER should not be set", responseHeader);
}
Aggregations