...
Maven users will need to add the following dependency to their pom.xml
for this component:
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
|
URI format
Code Block |
---|
http:hostname[:port][/resourceUri][?param1=value1][¶m2=value2]
|
...
URI parameters can either be set directly on the endpoint URI or as a header
Java DSL | ||
---|---|---|
|
...
Java DSL | Spring DSL | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
...
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 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.
...
- 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.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.
...
You can get access to these two using the Camel type converter system using
Code Block |
---|
HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
HttpServletRequest response = exchange.getIn().getBody(HttpServletResponse.class);
|
...
Java DSL | Spring DSL | ||||
---|---|---|---|---|---|
|
|
...
If you are using POST
to send data you can configure the charset
Code Block |
---|
setProperty(Exchange.CHARSET_NAME, "iso-8859-1");
|
...
The sample polls the Google homepage every 10 seconds and write the page to the file message.html
:
Code Block |
---|
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");
|
...
You can get the HTTP response code from the HTTP component by getting the value from the Out message header with HttpProducerExchange.HTTP_RESPONSE_CODE
.
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(HttpProducerExchange.HTTP_RESPONSE_CODE, Integer.class); |
...
Setting MaxConnectionsPerHost
The Http 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:
...
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.
Code Block |
---|
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");
|
...
However if you just want to specify the keystore and truststore you can do this with Apache HTTP HttpClientConfigurer
, for example:
Code Block |
---|
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:
Code Block |
---|
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:
Code Block |
---|
<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.
Include Page | ||||
---|---|---|---|---|
|