HTTP4Page edited by Claus IbsenChanges (1)
Full ContentHTTP4 ComponentAvailable as of Camel 2.3 The http4: component provides HTTP based endpoints for calling 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-http4</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 whether the 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); Configuring URI to callYou can set the HTTP producer's URI directly form the endpoint URI. In the route below, Camel will call out to the external server, oldhost, using HTTP. from("direct:start") .to("http4://oldhost"); And the equivalent Spring sample: <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="direct:start"/> <to uri="http4://oldhost"/> </route> </camelContext> You can override the HTTP endpoint URI by adding a header with the key, Exchange.HTTP_URI, on the message. from("direct:start") .setHeader(Exchange.HTTP_URI, constant("http://newhost")) .to("http4://oldhost"); In the sample above Camel will call the http://newhost despite the endpoint is configured with http4://oldhost. Configuring URI ParametersThe http producer supports URI parameters to be sent to the HTTP server. The URI parameters can either be set directly on the endpoint URI or as a header with the key Exchange.HTTP_QUERY on the message. from("direct:start") .to("http4://oldhost?order=123&detail=short"); Or options provided in a header: from("direct:start") .setHeader(Exchange.HTTP_QUERY, constant("order=123&detail=short")) .to("http4://oldhost"); How to set the http method (GET/POST/PUT/DELETE/HEAD/OPTIONS/TRACE) to the HTTP producerThe HTTP4 component provides a way to set the HTTP request method by setting the message header. Here is an example: from("direct:start") .setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.POST)) .to("http4://www.google.com") .to("mock:results"); The method can be written a bit shorter using the string constants: .setHeader("CamelHttpMethod", constant("POST")) And the equivalent Spring sample: <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="direct:start"/> <setHeader headerName="CamelHttpMethod"> <constant>POST</constant> </setHeader> <to uri="http4://www.google.com"/> <to uri="mock:results"/> </route> </camelContext> Using client timeout - SO_TIMEOUTSee the HttpSOTimeoutTest unit test. Configuring a ProxyThe HTTP4 component provides a way to configure a proxy. from("direct:start") .to("http4://oldhost?proxyAuthHost=www.myproxy.com&proxyAuthPort=80"); There is also support for proxy authentication via the proxyAuthUsername and proxyAuthPassword options. Using proxy settings outside of URITo avoid System properties conflicts, you can set proxy configuration only from the CamelContext or URI. context.getProperties().put("http.proxyHost", "172.168.18.9"); context.getProperties().put("http.proxyPort" "8080"); Spring XML <camelContext> <properties> <property key="http.proxyHost" value="172.168.18.9"/> <property key="http.proxyPort" value="8080"/> </properties> </camelContext> Camel will first set the settings from Java System or CamelContext Properties and then the endpoint proxy options if provided. Notice in Camel 2.8 there is also a http.proxyScheme property you can set to explicit configure the scheme to use. Configuring charsetIf you are using POST to send data you can configure the charset using the Exchange property:
exchange.setProperty(Exchange.CHARSET_NAME, "ISO-8859-1");
Sample with scheduled pollThis 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("http4://www.google.com") .setHeader(FileComponent.HEADER_FILE_NAME, "message.html") .to("file:target/google"); URI Parameters from the endpoint URIIn this sample we have the complete URI endpoint that is just what you would have typed in a web browser. Multiple URI parameters can of course be set using the & character as separator, just as you would in the web browser. Camel does no tricks here. // we query for Camel at the Google page template.sendBody("http4://www.google.com/search?q=Camel", null); URI Parameters from the MessageMap headers = new HashMap(); headers.put(HttpProducer.QUERY, "q=Camel&lr=lang_en"); // we query for Camel and English language at Google template.sendBody("http4://www.google.com/search", null, headers); In the header value above notice that it should not be prefixed with ? and you can separate parameters as usual with the & char. Getting the Response CodeYou can get the HTTP response code from the HTTP4 component by getting the value from the Out message header with HttpProducer.HTTP_RESPONSE_CODE. Exchange exchange = template.send("http4://www.google.com/search", new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setHeader(HttpProducer.QUERY, constant("hl=en&q=activemq")); } }); Message out = exchange.getOut(); int responseCode = out.getHeader(HttpProducer.HTTP_RESPONSE_CODE, Integer.class); 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 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. Programmatic configuration of the componentKeyStoreParameters 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); HttpComponent httpComponent = getContext().getComponent("http4", HttpComponent.class); httpComponent.setSslContextParameters(scp); Spring DSL based configuration of endpoint... <camel:sslContextParameters id="sslContextParameters"> <camel:keyManagers keyPassword="keyPassword"> <camel:keyStore resource="/users/home/server/keystore.jks" password="keystorePassword"/> </camel:keyManagers> </camel:sslContextParameters>... ... <to uri="https4://127.0.0.1/mail/?sslContextParametersRef=sslContextParameters"/>... Configuring Apache HTTP Client DirectlyBasically camel-http4 component is built on the top of Apache HTTP client. Please refer to SSL/TLS customization for details or have a look into the org.apache.camel.component.http4.HttpsServerTestSupport unit test base class. However if you just want to specify the keystore and truststore you can do this with Apache HTTP HttpClientConfigurer, for example: KeyStore keystore = ...; KeyStore truststore = ...; SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", 443, new SSLSocketFactory(keystore, "mypassword", truststore))); 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("http4", 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="https4://myhostname.com:443/myURL?httpClientConfigurer=myHttpClientConfigurer"/> As long as you implement the HttpClientConfigurer and configure your keystore and truststore as described above, it will work fine.
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > HTTP4 confluence
- [CONF] Apache Camel > HTTP4 confluence
- [CONF] Apache Camel > HTTP4 confluence
- [CONF] Apache Camel > HTTP4 confluence
- [CONF] Apache Camel > HTTP4 Claus Ibsen (Confluence)