HTTPPage edited by Christian SchneiderChanges (22)
Full ContentHTTP ComponentThe 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>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
HttpOperationFailedExceptionThis exception contains the following information:
Calling using GET or POSTThe following algorithm is used to determine if either GET or POST HTTP method should be used: How to get access to HttpServletRequest and HttpServletResponseYou 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 tineout - SO_TIMEOUTSee the unit test in this link More ExamplesConfiguring a Proxy
There is also support for proxy authentication via the proxyUsername and proxyPassword options. Using proxy settings outside of URI
Options on Endpoint will override options on the context. Configuring charsetIf you are using POST to send data you can configure the charset
setProperty(Exchange.CHARSET_NAME, "iso-8859-1");
Sample with scheduled pollThe 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 CodeYou can get the HTTP response code from the HTTP component by getting the value from the Out message header with HttpProducer.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(HttpProducer.HTTP_RESPONSE_CODE, Integer.class); Using throwExceptionOnFailure=false to get any response backIn 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 CookiesTo disable cookies you can set the HTTP Client to ignore cookies by adding this URI option: Advanced UsageIf 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 MaxConnectionsPerHostThe HTTP Component has a org.apache.commons.httpclient.HttpConnectionManager where you can configure various global configuration for the given component. 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 authenticationAn 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 serverSee this link from a mailing list discussion with some code to outline how to do this with the Apache Commons HTTP API. Setting up SSL for HTTP ClientUsing the JSSE Configuration UtilityAs of Camel 2.8, 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 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. 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 DirectlyBasically 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 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. See Also
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > HTTP confluence
- [CONF] Apache Camel > HTTP confluence
- [CONF] Apache Camel > HTTP confluence
- [CONF] Apache Camel > HTTP confluence