camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > AHC
Date Sun, 31 Jul 2011 21:36:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/AHC">AHC</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~dvaleri">David
Valeri</a>
    </h4>
        <br/>
                         <h4>Changes (6)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >| {{transferException}} | {{false}}
| 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 {{AhcOperationFailedException}}. The
caused exception is required to be serialized. | <br>| {{client}} | {{null}} | To use
a custom {{com.ning.http.client.AsyncHttpClient}}. | <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">|
{{clientConfig}} | {{null}} | To configure the {{AsyncHttpClient}}s use a custom {{com.ning.http.client.AsyncHttpClientConfig}}.
| <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{clientConfig}} | {{null}} | To configure the {{AsyncHttpClient}} to use a custom {{com.ning.http.client.AsyncHttpClientConfig}}
instance.  This instance replaces any instance configured at the component level. | <br>|
{{clientConfig.x}} | {{null}} | To configure additional properties of the  {{com.ning.http.client.AsyncHttpClientConfig}}
instance used by the endpoint.  Note that configuration options set using this parameter will
be merged with those set using the {{clientConfig}} parameter or the instance set at the component
level with properties set using this parameter taking priority. | <br></td></tr>
            <tr><td class="diff-unchanged" >| {{binding}} | {{null}} | To use
a custom {{org.apache.camel.component.ahc.AhcBinding}}. | <br>| {{sslContextParameters}}
\\ | {{null}} \\ | *Camel 2.9:* Reference to a {{org.apache.camel.util.jsse.SSLContextParameters}}
in the [CAMEL:Registry].&amp;nbsp; This reference overrides any configured SSLContextParameters
at the component level.&amp;nbsp; See [Using the JSSE Configuration Utility|#Using the
JSSE Configuration Utility].&amp;nbsp; Note that configuring this option will override
any SSL/TLS configuration options provided through the clientConfig option at the endpoint
or component level. | <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >[Async Http Client|http://github.com/sonatype/async-http-client]
for more details. <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">In
Camel *2.8*, configuration is limited to using the builder pattern provided by {{AsyncHttpClientConfig.Builder}}.
 In Camel 2.8, the {{AsyncHttpClientConfig}} doesn&#39;t support getters/setters so its
not easy to create/configure using a Spring bean style (eg the &lt;bean&gt; tag in
the XML file). <br> <br></td></tr>
            <tr><td class="diff-unchanged" >The example below shows how to use
a builder to create the {{AsyncHttpClientConfig}} which we configure on the {{AhcComponent}}.
<br>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcComponentClientConfigTest.java}
<br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">The
{{AsyncHttpClientConfig}} doesn&#39;t support getters/setters so its not easy to create/configure
using a Spring bean style (eg the &lt;bean&gt; tag in the XML file). <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">In
Camel *2.9*, the AHC component uses Async HTTP library 1.6.4.  This newer version provides
added support for plain bean style configuration.  The {{AsyncHttpClientConfigBean}} class
provides getters and setters for the configuration options available in {{AsyncHttpClientConfig}}.
 An instance of {{AsyncHttpClientConfigBean}} may be passed directly to the AHC component
or referenced in an endpoint URI using the {{clientConfig}} URI parameter. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Also
available in Camel *2.9* is the ability to set configuration options directly in the URI.
 URI parameters starting with &quot;clientConfig.&quot; can be used to set the various
configurable properties of {{AsyncHttpClientConfig}}.  The properties specified in the endpoint
URI are merged with those specified in the configuration referenced by the &quot;clientConfig&quot;
URI parameter with those being set using the &quot;clientConfig.&quot; parameter taking
priority.  The {{AsyncHttpClientConfig}} instance referenced is always copied for each endpoint
such that settings on one endpoint will remain independent of settings on any previously created
endpoints.  The example below shows how to configure the AHC component using the &quot;clientConfig.&quot;
type URI parameters. <br> <br>{code:java} <br>from(&quot;direct:start&quot;)
<br>    .to(&quot;ahc:http://localhost:8080/foo?clientConfig.maxRequestRetry=3&amp;clientConfig.followRedirects=true&quot;)
<br>{code} <br></td></tr>
            <tr><td class="diff-unchanged" >h3. SSL Support (HTTPS) <br>
<br></td></tr>
            <tr><td class="diff-unchanged" >h5. Using the JSSE Configuration Utility
<br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="AHC-AsyncHttpClient%28AHC%29Component"></a>Async Http
Client (AHC) Component</h2>

<p><b>Available as of Camel 2.8</b></p>

<p>The <b>ahc:</b> component provides HTTP based <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">endpoints</a> for consuming external HTTP resources (as a client
to call external servers using HTTP).<br/>
The component uses the <a href="http://github.com/sonatype/async-http-client" class="external-link"
rel="nofollow">Async Http Client</a> library.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-ahc<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="AHC-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ahc:http:<span class="code-comment">//hostname[:port][/resourceUri][?options]
</span>ahc:https:<span class="code-comment">//hostname[:port][/resourceUri][?options]</span>
</pre>
</div></div>
<p>Will by default use port 80 for HTTP and 443 for HTTPS.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="AHC-AhcEndpointOptions"></a>AhcEndpoint Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>throwExceptionOnFailure</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Option to disable throwing the <tt>AhcOperationFailedException</tt>
in case of failed responses from the remote server. This allows you to get all responses regardless
of the HTTP status code. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>bridgeEndpoint</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If the option is true, then the Exchange.HTTP_URI header is
ignored, and use the endpoint's URI for request. You may also set the <b>throwExcpetionOnFailure</b>
to be false to let the AhcProducer send all the fault response back. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>transferException</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If enabled and an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> failed processing on the consumer side, and if the
caused <tt>Exception</tt> was send back serialized in the response as a <tt>application/x-java-serialized-object</tt>
content type (for example using <a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a>
or <a href="/confluence/display/CAMEL/SERVLET" title="SERVLET">SERVLET</a> Camel
components). On the producer side the exception will be deserialized and thrown as is, instead
of the <tt>AhcOperationFailedException</tt>. The caused exception is required
to be serialized. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>client</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>com.ning.http.client.AsyncHttpClient</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>clientConfig</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To configure the <tt>AsyncHttpClient</tt> to use
a custom <tt>com.ning.http.client.AsyncHttpClientConfig</tt> instance.  This instance
replaces any instance configured at the component level. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>clientConfig.x</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To configure additional properties of the  <tt>com.ning.http.client.AsyncHttpClientConfig</tt>
instance used by the endpoint.  Note that configuration options set using this parameter will
be merged with those set using the <tt>clientConfig</tt> parameter or the instance
set at the component level with properties set using this parameter taking priority. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>binding</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>org.apache.camel.component.ahc.AhcBinding</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sslContextParameters</tt> <br class="atl-forced-newline"
/> </td>
<td class='confluenceTd'> <tt>null</tt> <br class="atl-forced-newline"
/> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> Reference to a <tt>org.apache.camel.util.jsse.SSLContextParameters</tt>
in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.&nbsp;
This reference overrides any configured SSLContextParameters at the component level.&nbsp;
See <a href="#AHC-UsingtheJSSEConfigurationUtility">Using the JSSE Configuration Utility</a>.&nbsp;
Note that configuring this option will override any SSL/TLS configuration options provided
through the clientConfig option at the endpoint or component level. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="AHC-AhcComponentOptions"></a>AhcComponent Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>client</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>com.ning.http.client.AsyncHttpClient</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>clientConfig</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To configure the <tt>AsyncHttpClient}}s use a custom
{{com.ning.http.client.AsyncHttpClientConfig</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>binding</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>org.apache.camel.component.ahc.AhcBinding</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sslContextParameters</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> To configure custom SSL/TLS
configuration options at the component level.&nbsp; See&nbsp; <a href="#AHC-UsingtheJSSEConfigurationUtility">Using
the JSSE Configuration Utility</a> for more details.&nbsp; Note that configuring
this option will override any SSL/TLS configuration options provided through the clientConfig
option at the endpoint or component level. </td>
</tr>
</tbody></table>
</div>
</div>

<p>Notice that setting any of the options on the <tt>AhcComponent</tt> will
propagate those options to <tt>AhcEndpoint}}s being created. However the {{AhcEndpoint</tt>
can also configure/override a custom option. Options set on endpoints will always take precedence
over options from the <tt>AhcComponent</tt>.</p>

<h3><a name="AHC-MessageHeaders"></a>Message Headers</h3>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.HTTP_URI</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> URI to call. Will override existing URI set directly on the
endpoint. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.HTTP_PATH</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> Request URI's path, the header will be used to build the request
URI with the HTTP_URI. 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 <tt>exchange.getFromEndpoint().getEndpointUri();</tt>
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.HTTP_QUERY</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> URI parameters. Will override existing URI parameters set
directly on the endpoint. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.HTTP_RESPONSE_CODE</tt> </td>
<td class='confluenceTd'> <tt>int</tt> </td>
<td class='confluenceTd'> The HTTP response code from the external server. Is 200 for
OK. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.HTTP_CHARACTER_ENCODING</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> Character encoding. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.CONTENT_TYPE</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> The HTTP content type. Is set on both the IN and OUT message
to provide a content type, such as <tt>text/html</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.CONTENT_ENCODING</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> The HTTP content encoding. Is set on both the IN and OUT message
to provide a content encoding, such as <tt>gzip</tt>. </td>
</tr>
</tbody></table>
</div>
</div>


<h3><a name="AHC-MessageBody"></a>Message Body</h3>

<p>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.</p>

<h3><a name="AHC-Responsecode"></a>Response code</h3>

<p>Camel will handle according to the HTTP response code:</p>
<ul class="alternate" type="square">
	<li>Response code is in the range 100..299, Camel regards it as a success response.</li>
	<li>Response code is in the range 300..399, Camel regards it as a redirection response
and will throw a <tt>AhcOperationFailedException</tt> with the information.</li>
	<li>Response code is 400+, Camel regards it as an external server failure and will
throw a <tt>AhcOperationFailedException</tt> with the information.
<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>throwExceptionOnFailure</b><br
/>The option, <tt>throwExceptionOnFailure</tt>, can be set to <tt>false</tt>
to prevent the <tt>AhcOperationFailedException</tt> from being thrown for failed
response codes. This allows you to get any response from the remote server.</td></tr></table></div></li>
</ul>


<h3><a name="AHC-AhcOperationFailedException"></a>AhcOperationFailedException</h3>

<p>This exception contains the following information:</p>
<ul class="alternate" type="square">
	<li>The HTTP status code</li>
	<li>The HTTP status line (text of the status code)</li>
	<li>Redirect location, if server returned a redirect</li>
	<li>Response body as a <tt>java.lang.String</tt>, if server provided a
body as response</li>
</ul>


<h3><a name="AHC-CallingusingGETorPOST"></a>Calling using GET or POST</h3>

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

<h3><a name="AHC-ConfiguringURItocall"></a>Configuring URI to call</h3>

<p>You can set the HTTP producer's URI directly form the endpoint URI. In the route
below, Camel will call out to the external server, <tt>oldhost</tt>, using HTTP.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
	    .to(<span class="code-quote">"ahc:http:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div>
<p>And the equivalent Spring sample:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://activemq.apache.org/camel/schema/spring"</span>&gt;</span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"ahc:http://oldhost"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>You can override the HTTP endpoint URI by adding a header with the key, <tt>Exchange.HTTP_URI</tt>,
on the message.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
    .setHeader(Exchange.HTTP_URI, constant(<span class="code-quote">"http:<span class="code-comment">//newhost"</span>))
</span>    .to(<span class="code-quote">"ahc:http:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div>

<h3><a name="AHC-ConfiguringURIParameters"></a>Configuring URI Parameters</h3>

<p>The <b>ahc</b> 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 <tt>Exchange.HTTP_QUERY</tt> on the message.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
	    .to(<span class="code-quote">"ahc:http:<span class="code-comment">//oldhost?order=123&amp;detail=<span
class="code-object">short</span>"</span>);</span>
</pre>
</div></div>
<p>Or options provided in a header:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
            .setHeader(Exchange.HTTP_QUERY, constant(<span class="code-quote">"order=123&amp;detail=<span
class="code-object">short</span>"</span>))
	    .to(<span class="code-quote">"ahc:http:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div>


<h3><a name="AHC-Howtosetthehttpmethod%28GET%2FPOST%2FPUT%2FDELETE%2FHEAD%2FOPTIONS%2FTRACE%29totheHTTPproducer"></a>How
to set the http method (GET/POST/PUT/DELETE/HEAD/OPTIONS/TRACE) to the HTTP producer</h3>

<p>The HTTP component provides a way to set the HTTP request method by setting the message
header. Here is an example;</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
            .setHeader(Exchange.HTTP_METHOD, constant(<span class="code-quote">"POST"</span>))
	    .to(<span class="code-quote">"ahc:http:<span class="code-comment">//www.google.com"</span>)
</span>            .to(<span class="code-quote">"mock:results"</span>);
</pre>
</div></div>

<p>And the equivalent Spring sample:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://activemq.apache.org/camel/schema/spring"</span>&gt;</span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
    <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"CamelHttpMethod"</span>&gt;</span>
        <span class="code-tag">&lt;constant&gt;</span>POST<span class="code-tag">&lt;/constant&gt;</span>
    <span class="code-tag">&lt;/setHeader&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"ahc:http://www.google.com"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:results"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="AHC-Configuringcharset"></a>Configuring charset</h3>

<p>If you are using <tt>POST</tt> to send data you can configure the <tt>charset</tt>
using the <tt>Exchange</tt> property:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
exchange.setProperty(Exchange.CHARSET_NAME, <span class="code-quote">"iso-8859-1"</span>);
</pre>
</div></div>

<h4><a name="AHC-URIParametersfromtheendpointURI"></a>URI Parameters from
the endpoint URI</h4>

<p>In 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 <tt>&amp;</tt>
character as separator, just as you would in the web browser. Camel does no tricks here.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// we query <span class="code-keyword">for</span>
Camel at the Google page
</span>template.sendBody(<span class="code-quote">"ahc:http:<span class="code-comment">//www.google.com/search?q=Camel"</span>,
<span class="code-keyword">null</span>);</span>
</pre>
</div></div>

<h4><a name="AHC-URIParametersfromtheMessage"></a>URI Parameters from the
Message</h4>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Map headers = <span class="code-keyword">new</span> HashMap();
headers.put(Exchange.HTTP_QUERY, <span class="code-quote">"q=Camel&amp;lr=lang_en"</span>);
<span class="code-comment">// we query <span class="code-keyword">for</span>
Camel and English language at Google
</span>template.sendBody(<span class="code-quote">"ahc:http:<span class="code-comment">//www.google.com/search"</span>,
<span class="code-keyword">null</span>, headers);</span>
</pre>
</div></div>
<p>In the header value above notice that it should <b>not</b> be prefixed
with <tt>?</tt> and you can separate parameters as usual with the <tt>&amp;</tt>
char.</p>

<h4><a name="AHC-GettingtheResponseCode"></a>Getting the Response Code</h4>

<p>You can get the HTTP response code from the AHC component by getting the value from
the Out message header with <tt>Exchange.HTTP_RESPONSE_CODE</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Exchange exchange = template.send(<span class="code-quote">"ahc:http:<span class="code-comment">//www.google.com/search"</span>,
<span class="code-keyword">new</span> Processor() {
</span>            <span class="code-keyword">public</span> void process(Exchange
exchange) <span class="code-keyword">throws</span> Exception {
                exchange.getIn().setHeader(Exchange.HTTP_QUERY, constant(<span class="code-quote">"hl=en&amp;q=activemq"</span>));
            }
   });
   Message out = exchange.getOut();
   <span class="code-object">int</span> responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE,
<span class="code-object">Integer</span>.class);
</pre>
</div></div>

<h3><a name="AHC-ConfiguringAsyncHttpClient"></a>Configuring AsyncHttpClient</h3>

<p>The <tt>AsyncHttpClient</tt> client uses a <tt>AsyncHttpClientConfig</tt>
to configure the client. See the documentation at<br/>
<a href="http://github.com/sonatype/async-http-client" class="external-link" rel="nofollow">Async
Http Client</a> for more details.</p>

<p>In Camel <b>2.8</b>, configuration is limited to using the builder pattern
provided by <tt>AsyncHttpClientConfig.Builder</tt>.  In Camel 2.8, the <tt>AsyncHttpClientConfig</tt>
doesn't support getters/setters so its not easy to create/configure using a Spring bean style
(eg the &lt;bean&gt; tag in the XML file).</p>

<p>The example below shows how to use a builder to create the <tt>AsyncHttpClientConfig</tt>
which we configure on the <tt>AhcComponent</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// create a client config builder
</span>AsyncHttpClientConfig.Builder builder = <span class="code-keyword">new</span>
AsyncHttpClientConfig.Builder();
<span class="code-comment">// use the builder to set the options we want, in <span
class="code-keyword">this</span> <span class="code-keyword">case</span>
we want to follow redirects and <span class="code-keyword">try</span>
</span><span class="code-comment">// at most 3 retries to send a request to the
host
</span>AsyncHttpClientConfig config = builder.setFollowRedirects(<span class="code-keyword">true</span>).setMaxRequestRetry(3).build();

<span class="code-comment">// lookup AhcComponent
</span>AhcComponent component = context.getComponent(<span class="code-quote">"ahc"</span>,
AhcComponent.class);
<span class="code-comment">// and set our custom client config to be used
</span>component.setClientConfig(config);
</pre>
</div></div>

<p>In Camel <b>2.9</b>, the AHC component uses Async HTTP library 1.6.4.
 This newer version provides added support for plain bean style configuration.  The <tt>AsyncHttpClientConfigBean</tt>
class provides getters and setters for the configuration options available in <tt>AsyncHttpClientConfig</tt>.
 An instance of <tt>AsyncHttpClientConfigBean</tt> may be passed directly to the
AHC component or referenced in an endpoint URI using the <tt>clientConfig</tt>
URI parameter.</p>

<p>Also available in Camel <b>2.9</b> is the ability to set configuration
options directly in the URI.  URI parameters starting with "clientConfig." can be used to
set the various configurable properties of <tt>AsyncHttpClientConfig</tt>.  The
properties specified in the endpoint URI are merged with those specified in the configuration
referenced by the "clientConfig" URI parameter with those being set using the "clientConfig."
parameter taking priority.  The <tt>AsyncHttpClientConfig</tt> instance referenced
is always copied for each endpoint such that settings on one endpoint will remain independent
of settings on any previously created endpoints.  The example below shows how to configure
the AHC component using the "clientConfig." type URI parameters.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
    .to(<span class="code-quote">"ahc:http:<span class="code-comment">//localhost:8080/foo?clientConfig.maxRequestRetry=3&amp;clientConfig.followRedirects=<span
class="code-keyword">true</span>"</span>)</span>
</pre>
</div></div>
<h3><a name="AHC-SSLSupport%28HTTPS%29"></a>SSL Support (HTTPS)</h3>

<h5><a name="AHC-UsingtheJSSEConfigurationUtility"></a>Using the JSSE Configuration
Utility</h5>

<p>As of Camel 2.9, the AHC component supports SSL/TLS configuration through the <a
href="/confluence/display/CAMEL/Camel+Configuration+Utilities" title="Camel Configuration
Utilities">Camel JSSE Configuration Utility</a>.&nbsp;  This utility greatly
decreases the amount of component specific code  you need to write and is configurable at
the endpoint and component  levels.&nbsp; The following examples demonstrate how to use
the utility with  the AHC component.</p>

<h6><a name="AHC-Programmaticconfigurationofthecomponent"></a>Programmatic
configuration of the component</h6>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
KeyStoreParameters ksp = <span class="code-keyword">new</span> KeyStoreParameters();
ksp.setResource(<span class="code-quote">"/users/home/server/keystore.jks"</span>);
ksp.setPassword(<span class="code-quote">"keystorePassword"</span>);

KeyManagersParameters kmp = <span class="code-keyword">new</span> KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword(<span class="code-quote">"keyPassword"</span>);

SSLContextParameters scp = <span class="code-keyword">new</span> SSLContextParameters();
scp.setKeyManagers(kmp);

AhcComponent component = context.getComponent(<span class="code-quote">"ahc"</span>,
AhcComponent.class);
component.setSslContextParameters(scp));
</pre>
</div></div>

<h6><a name="AHC-SpringDSLbasedconfigurationofendpoint"></a>Spring DSL based
configuration of endpoint</h6>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
...
  &lt;camel:sslContextParameters
      id=<span class="code-quote">"sslContextParameters"</span>&gt;
    &lt;camel:keyManagers
        keyPassword=<span class="code-quote">"keyPassword"</span>&gt;
      &lt;camel:keyStore
          resource=<span class="code-quote">"/users/home/server/keystore.jks"</span>
          password=<span class="code-quote">"keystorePassword"</span>/&gt;
    <span class="code-tag">&lt;/camel:keyManagers&gt;</span>
  <span class="code-tag">&lt;/camel:sslContextParameters&gt;</span>...
...
  <span class="code-tag">&lt;to uri=<span class="code-quote">"ahc:https://localhost/foo?sslContextParameters=#sslContextParameters"</span>/&gt;</span>
...
</pre>
</div></div>

<h3><a name="AHC-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP4" title="HTTP4">HTTP4</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/AHC">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=26804727&revisedVersion=4&originalVersion=3">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/AHC?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message