...
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 | |||||
---|---|---|---|---|---|
|
URI parameters can either be set directly on the endpoint URI or as a header:
Java DSL | |||||
---|---|---|---|---|---|
|
...
Java DSL | Spring DSL | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
HttpEndpoint
Options
Div | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
...
Info |
---|
Note: camel-http 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. camel-http4 has support for NTLMv2. |
HttpComponent
Options
Div | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||
|
HttpConfiguration
contains all the options listed in the table above under the section HttpConfiguration - Setting Authentication and Proxy.
...
Div | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||
|
Info |
---|
Note: The header |
...
names 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 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 aHttpOperationFailedException
with the information. Response code is
400+
, Camel regards it as an external server failure and will throw aHttpOperationFailedException
with the information.Tip title throwExceptionOnFailure The option,
throwExceptionOnFailure
, can be set tofalse
to prevent theHttpOperationFailedException
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:
- Use method provided in header.
GET
if query string is provided in header.GET
if endpoint is configured with a query string.POST
if there is data to send (body is not null).GET
otherwise.
How To Access
...
The HttpServletRequest
and HttpServletResponse
You can get access to these two using the Camel type converter system using:
...
Code Block | ||||
---|---|---|---|---|
| ||||
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 Obtain All Server Responses
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 all responses from the remote server, we set the throwExceptionOnFailure=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.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/tags/camel-2.2.0/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java} |
...
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 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.
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/tags/camel-2.2.0/tests/camel-itest/src/test/resources/org/apache/camel/itest/http/HttpMaxConnectionPerHostTest-context.xml} |
Wiki Markup |
---|
{snippet:id=e2|lang=xml|url=camel/tags/camel-2.2.0/tests/camel-itest/src/test/resources/org/apache/camel/itest/http/HttpMaxConnectionPerHostTest-context.xml} |
Using Pre-
...
Emptive 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 From Camel 2.8, the : the HTTP4
component supports SSL/TLS configuration through the Camel JSSE Configuration 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 the HTTP4
component.
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.
...
Basically camel-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 the keystore
and and truststore
you can do this with Apache HTTP HttpClientConfigurer
, for example:
...
And then you need to create a class that implements HttpClientConfigurer
, and registers HTTPS protocol providing a a keystore
or or truststore
per example above. Then, from your camel route builder Camel RouteBuilder class you can hook it up like so:
...
As long as you implement the HttpClientConfigurer
and configure your your keystore
and and truststore
as described above, it will work fine.
...