Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 70. HTTP

HTTP Component

The http: component provides HTTP-based endpoints for consuming external HTTP resources (as a client to call external servers using HTTP).
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-http</artifactId>
    <version>2.17.0.redhat-630xxx</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

http:hostname[:port][/resourceUri][?param1=value1][&param2=value2]
Will by default use port 80 for HTTP and 443 for HTTPS.
camel-http vs camel-jetty
You can only produce to endpoints generated by the HTTP component. Therefore it should never be used as input into your camel Routes. To bind/expose an HTTP endpoint via a HTTP server as input to a camel route, you can use the Jetty Component or the Servlet component.

Examples

Call the url with the body using POST and return response as out message. If body is null call URL using GET and return response as out message
Java DSL Spring DSL
from("direct:start")
  .to("http://myhost/mypath");
<from uri="direct:start"/>
<to uri="http://oldhost"/>
You can override the HTTP endpoint URI by adding a header. Camel will call the http://newhost. This is very handy for e.g. REST urls.
Java DSL
from("direct:start")
  .setHeader(Exchange.HTTP_URI, simple("http://myserver/orders/${header.orderId}"))
  .to("http://dummyhost");
URI parameters can either be set directly on the endpoint URI or as a header
Java DSL
from("direct:start")
  .to("http://oldhost?order=123&detail=short");
from("direct:start")
  .setHeader(Exchange.HTTP_QUERY, constant("order=123&detail=short"))
  .to("http://oldhost");
Set the HTTP request method to POST
Java DSL Spring DSL
from("direct:start")
  .setHeader(Exchange.HTTP_METHOD, constant("POST"))
  .to("http://www.google.com");
<from uri="direct:start"/>
<setHeader headerName="CamelHttpMethod">
  <constant>POST</constant>
</setHeader>
<to uri="http://www.google.com"/>
<to uri="mock:results"/>

HttpEndpoint Options

Name Default Value Description
throwExceptionOnFailure true Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code.
bridgeEndpoint false
If the option is true , HttpProducer will ignore the Exchange.HTTP_URI header, and use the endpoint's URI for request. You may also set the throwExceptionOnFailure to be false to let the HttpProducer send all the fault response back. Camel 2.3: If the option is true, HttpProducer and CamelServlet will skip the gzip processing if the content-encoding is "gzip".
disableStreamCache false DefaultHttpBinding will copy the request input stream into a stream cache and put it into message body if this option is false to support read it twice, otherwise DefaultHttpBinding will set the request input stream direct into the message body. Camel 2.17: This option is now also supported by the producer to enable using the response stream directly, instead of stream caching by default.
httpBinding null Reference to a org.apache.camel.component.http.HttpBinding in the Registry.
httpClientConfigurer null Reference to a org.apache.camel.component.http.HttpClientConfigurer in the Registry.
httpClient.XXX null Setting options on the HttpClientParams. For instance httpClient.soTimeout=5000 will set the SO_TIMEOUT to 5 seconds.
clientConnectionManager null To use a custom org.apache.http.conn.ClientConnectionManager.
transferException false Camel 2.6: If enabled and an Exchange failed processing on the consumer side, and if the caused Exception was send back serialized in the response as a application/x-java-serialized-object content type (for example using Jetty or Servlet Camel components). On the producer side the exception will be deserialized and thrown as is, instead of the HttpOperationFailedException. The caused exception is required to be serialized.
headerFilterStrategy null Camel 2.11: Reference to a instance of org.apache.camel.spi.HeaderFilterStrategy in the Registry. It will be used to apply the custom headerFilterStrategy on the new create HttpEndpoint.
eagerCheckContentAvailable false Camel 2.15.3/2.16: Consumer only. Whether to eager check whether the HTTP requests has content if the Content-Length header is 0 or not present. This can be turned on in case HTTP clients do not send streamed data.
copyHeaders true Camel 2.16: If true, IN exchange headers will be copied to OUT exchange headers in accordance with the copy strategy. If false, only the headers from the HTTP response will be included (IN headers will not be copied).
okStatusCodeRange 200-299 Camel 2.16: The status codes which are considered a success response. The values are inclusive. The range must be defined using the syntax, from-to.
ignoreResponseBody false Camel 2.16: If true, the http producer will not read the response body and cache the input stream.

Authentication and Proxy

The following authentication options can also be set on the HttpEndpoint:
Name Default Value Description
authMethod null Authentication method, either as Basic, Digest or NTLM.
authMethodPriority null Priority of authentication methods. Is a list separated with comma. For example: Basic,Digest to exclude NTLM.
authUsername null Username for authentication
authPassword null Password for authentication
authDomain null Domain for NTML authentication
authHost null Optional host for NTML authentication
proxyHost null The proxy host name
proxyPort null The proxy port number
proxyAuthMethod null Authentication method for proxy, either as Basic, Digest or NTLM.
proxyAuthUsername null Username for proxy authentication
proxyAuthPassword null Password for proxy authentication
proxyAuthDomain null Domain for proxy NTML authentication
proxyAuthHost null Optional host for proxy NTML authentication
When using authentication you must provide the choice of method for the authMethod or authProxyMethod options. You can configure the proxy and authentication details on either the HttpComponent or the HttpEndoint. Values provided on the HttpEndpoint will take precedence over HttpComponent. Its most likely best to configure this on the HttpComponent which allows you to do this once.
The HTTP component uses convention over configuration which means that if you have not explicit set a authMethodPriority then it will fallback and use the select(ed) authMethod as priority as well. So if you use authMethod.Basic then the auhtMethodPriority will be Basic only.
Note
The Camel HTTP component is based on HttpClient v3.x and as such has only limited support for what is known as NTLMv1, the early version of the NTLM protocol. It does not support NTLMv2 at all. The Camel HTTP4 component has support for NTLMv2.

HttpComponent Options

Name Default Value Description
httpBinding null To use a custom org.apache.camel.component.http.HttpBinding.
httpClientConfigurer null To use a custom org.apache.camel.component.http.HttpClientConfigurer.
httpConnectionManager null To use a custom org.apache.commons.httpclient.HttpConnectionManager.
httpConfiguration null To use a custom org.apache.camel.component.http.HttpConfiguration
allowJavaSerializedObject false
Camel 2.16.1/2.15.5: Whether to allow java serialization when a request uses context-type=application/x-java-serialized-object. This option is turned off by default. Warning: If you enable this option, then be aware that Java will deserialize the incoming data from the request to Java, and that can be a potential security risk.

Message Headers

Name Type Description
Exchange.HTTP_URI String URI to call. Will override existing URI set directly on the endpoint. This URI is the URI of the HTTP server to call. Its not the same as the Camel endpoint URI, where you can configure endpoint options such as security and so on. This header does not support that, its only the UTI of the HTTP server.
Exchange.HTTP_METHOD String HTTP Method / Verb to use (GET/POST/PUT/DELETE/HEAD/OPTIONS/TRACE)
Exchange.HTTP_PATH String Request URI's path, the header will be used to build the request URI with the HTTP_URI. Camel 2.3.0: If the path is start with "/", http producer will try to find the relative path based on the Exchange.HTTP_BASE_URI header or the exchange.getFromEndpoint().getEndpointUri();
Exchange.HTTP_QUERY String URI parameters. Will override existing URI parameters set directly on the endpoint.
Exchange.HTTP_RESPONSE_CODE int The HTTP response code from the external server. Is 200 for OK.
Exchange.HTTP_CHARACTER_ENCODING String Character encoding.
Exchange.CONTENT_TYPE String The HTTP content type. Is set on both the IN and OUT message to provide a content type, such as text/html.
Exchange.CONTENT_ENCODING String The HTTP content encoding. Is set on both the IN and OUT message to provide a content encoding, such as gzip.
Exchange.HTTP_SERVLET_REQUEST HttpServletRequest The HttpServletRequest object.
Exchange.HTTP_SERVLET_RESPONSE HttpServletResponse The HttpServletResponse object.
Exchange.HTTP_PROTOCOL_VERSION String Camel 2.5: You can set the http protocol version with this header, eg. "HTTP/1.0". If you didn't specify the header, HttpProducer will use the default value "HTTP/1.1"
The header name above are constants. For the spring DSL you have to use the value of the constant instead of the name.

Message Body

Camel will store the HTTP response from the external server on the OUT body. All headers from the IN message will be copied to the OUT message, so headers are preserved during routing. Additionally Camel will add the HTTP response headers as well to the OUT message headers.

Response code

Camel will handle according to the HTTP response code:
  • Response code is in the range 100..299, Camel regards it as a success response.
  • Response code is in the range 300..399, Camel regards it as a redirection response and will throw a HttpOperationFailedException with the information.
  • Response code is 400+, Camel regards it as an external server failure and will throw a HttpOperationFailedException with the information.
    throwExceptionOnFailure
    The option, throwExceptionOnFailure, can be set to false to prevent the HttpOperationFailedException from being thrown for failed response codes. This allows you to get any response from the remote server. There is a sample below demonstrating this.

HttpOperationFailedException

This exception contains the following information:
  • The HTTP status code
  • The HTTP status line (text of the status code)
  • Redirect location, if server returned a redirect
  • Response body as a java.lang.String, if server provided a body as response

Calling using GET or POST

The following algorithm is used to determine if either GET or POST HTTP method should be used: 1. Use method provided in header. 2. GET if query string is provided in header. 3. GET if endpoint is configured with a query string. 4. POST if there is data to send (body is not null). 5. GET otherwise.

How to get access to HttpServletRequest and HttpServletResponse

You can get access to these two using the Camel type converter system using
HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
HttpServletRequest response = exchange.getIn().getBody(HttpServletResponse.class);

Using client timeout - SO_TIMEOUT

See the unit test in this link

Configuring a Proxy

Java DSL
from("direct:start")
  .to("http://oldhost?proxyHost=www.myproxy.com&proxyPort=80");
There is also support for proxy authentication via the proxyUsername and proxyPassword options.

Using proxy settings outside of URI

Java DSL Spring DSL
 context.getProperties().put("http.proxyHost", "172.168.18.9");
 context.getProperties().put("http.proxyPort" "8080");
   <camelContext>
       <properties>
           <property key="http.proxyHost" value="172.168.18.9"/>
           <property key="http.proxyPort" value="8080"/>
      </properties>
   </camelContext>
Options on Endpoint will override options on the context.

Configuring charset

If you are using POST to send data you can configure the charset
setProperty(Exchange.CHARSET_NAME, "iso-8859-1");

Sample with scheduled poll

The sample polls the Google homepage every 10 seconds and write the page to the file message.html:
from("timer://foo?fixedRate=true&delay=0&period=10000")
    .to("http://www.google.com")
    .setHeader(FileComponent.HEADER_FILE_NAME, "message.html").to("file:target/google");

Getting the Response Code

You can get the HTTP response code from the HTTP component by getting the value from the Out message header with Exchange.HTTP_RESPONSE_CODE.
   Exchange exchange = template.send("http://www.google.com/search", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setHeader(Exchange.HTTP_QUERY, constant("hl=en&q=activemq"));
            }
   });
   Message out = exchange.getOut();
   int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);

Using throwExceptionOnFailure=false to get any response back

In the route below we want to route a message that we enrich with data returned from a remote HTTP call. As we want any response from the remote server, we set the throwExceptionOnFailure option to false so we get any response in the AggregationStrategy. As the code is based on a unit test that simulates a HTTP status code 404, there is some assertion code etc.
// We set throwExceptionOnFailure to false to let Camel return any response from the remove HTTP server without thrown
// HttpOperationFailedException in case of failures.
// This allows us to handle all responses in the aggregation strategy where we can check the HTTP response code
// and decide what to do. As this is based on an unit test we assert the code is 404
from("direct:start").enrich("http://localhost:{{port}}/myserver?throwExceptionOnFailure=false&user=Camel", new AggregationStrategy() {
    public Exchange aggregate(Exchange original, Exchange resource) {
        // get the response code
        Integer code = resource.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
        assertEquals(404, code.intValue());
        return resource;
    }
}).to("mock:result");

// this is our jetty server where we simulate the 404
from("jetty://http://localhost:{{port}}/myserver")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getOut().setBody("Page not found");
                exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
            }
        });

Disabling Cookies

To disable cookies you can set the HTTP Client to ignore cookies by adding this URI option: httpClient.cookiePolicy=ignoreCookies

Advanced Usage

If you need more control over the HTTP producer you should use the HttpComponent where you can set various classes to give you custom behavior.

Setting MaxConnectionsPerHost

The HTTP Component has a org.apache.commons.httpclient.HttpConnectionManager where you can configure various global configuration for the given component. By global, we mean that any endpoint the component creates has the same shared HttpConnectionManager. So, if we want to set a different value for the max connection per host, we need to define it on the HTTP component and not on the endpoint URI that we usually use. So here comes:
First, we define the http component in Spring XML. Yes, we use the same scheme name, http, because otherwise Camel will auto-discover and create the component with default settings. What we need is to overrule this so we can set our options. In the sample below we set the max connection to 5 instead of the default of 2.
<bean id="http" class="org.apache.camel.component.http.HttpComponent">
    <property name="camelContext" ref="camel"/>
    <property name="httpConnectionManager" ref="myHttpConnectionManager"/>
</bean>

<bean id="myHttpConnectionManager" class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
    <property name="params" ref="myHttpConnectionManagerParams"/>
</bean>

<bean id="myHttpConnectionManagerParams" class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
    <property name="defaultMaxConnectionsPerHost" value="5"/>
</bean>

And then we can just use it as we normally do in our routes:
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring" trace="true">
    <route>
        <from uri="direct:start"/>
        <to uri="http://www.google.com"/>
        <to uri="mock:result"/>
    </route>
</camelContext>

Using preemptive authentication

An end user reported that he had problem with authenticating with HTTPS. The problem was eventually resolved when he discovered the HTTPS server did not return a HTTP code 401 Authorization Required. The solution was to set the following URI option: httpClient.authenticationPreemptive=true

Accepting self signed certificates from remote server

See this link from a mailing list discussion with some code to outline how to do this with the Apache Commons HTTP API.

Using the JSSE Configuration Utility

As of Camel 2.8, the HTTP4 component supports SSL/TLS configuration through the JSSE utility. This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels. The following examples demonstrate how to use the utility with the HTTP4 component. Also see chapter "Configuring Transport Security for Camel Components" in "Security Guide".
The version of the Apache HTTP client used in this component resolves SSL/TLS information from a global "protocol" registry. This component provides an implementation, org.apache.camel.component.http.SSLContextParametersSecureProtocolSocketFactory, of the HTTP client's protocol socket factory in order to support the use of the Camel JSSE Configuration utility. The following example demonstrates how to configure the protocol registry and use the registered protocol information in a route.
KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource("/users/home/server/keystore.jks");
ksp.setPassword("keystorePassword");

KeyManagersParameters kmp = new KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword("keyPassword");

SSLContextParameters scp = new SSLContextParameters();
scp.setKeyManagers(kmp);

ProtocolSocketFactory factory =
    new SSLContextParametersSecureProtocolSocketFactory(scp);

Protocol.registerProtocol("https",
        new Protocol(
        "https",
        factory,
        443));

from("direct:start")
        .to("https://mail.google.com/mail/").to("mock:results");

Configuring Apache HTTP Client Directly

Basically the HTTP component is built on the top of Apache HTTP client, and you can implement a custom org.apache.camel.component.http.HttpClientConfigurer to do some configuration on the http client if you need full control of it.
However if you just want to specify the keystore and truststore you can do this with Apache HTTP HttpClientConfigurer, for example:
Protocol authhttps = new Protocol("https", new AuthSSLProtocolSocketFactory(
  new URL("file:my.keystore"), "mypassword",
  new URL("file:my.truststore"), "mypassword"), 443);

Protocol.registerProtocol("https", authhttps);
And then you need to create a class that implements HttpClientConfigurer, and registers https protocol providing a keystore or truststore per example above. Then, from your camel route builder class you can hook it up like so:
HttpComponent httpComponent = getContext().getComponent("http", HttpComponent.class);
httpComponent.setHttpClientConfigurer(new MyHttpClientConfigurer());
If you are doing this using the Spring DSL, you can specify your HttpClientConfigurer using the URI. For example:
<bean id="myHttpClientConfigurer"
 class="my.https.HttpClientConfigurer">
</bean>

<to uri="https://myhostname.com:443/myURL?httpClientConfigurerRef=myHttpClientConfigurer"/>
As long as you implement the HttpClientConfigurer and configure your keystore and truststore as described above, it will work fine.