camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > HTTP
Date Mon, 29 Mar 2010 00:57:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/HTTP">HTTP</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~njiang">willem jiang</a>
    </h4>
     CAMEL-2592
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     CAMEL-2592<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <h2><a name="HTTP-HTTPComponent"></a>HTTP Component</h2>

<p>The <b>http:</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).</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-java">
&lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-http&lt;/artifactId&gt;
    &lt;version&gt;x.x.x&lt;/version&gt;
    &lt;!-- use the same version as your Camel core version --&gt;
&lt;/dependency&gt;
</pre>
</div></div>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
http:hostname[:port][/resourceUri][?options]
</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>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>About HTTP Client version</b><br /><p>Notice <b>Camel 2.2 or older</b> uses the Apache HTTP Client 3.1. We upgraded to HTTP Client 4.0.1 in <b>Camel 2.3</b>. This has an impact if you need advanced configuration as these two versions is much different how you do that. Consult the <a href="http://hc.apache.org/httpcomponents-client/tutorial/html/" rel="nofollow">HTTP Client 4.x documentation</a>.</p></td></tr></table></div>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>camel-http vs camel-jetty</b><br /><p>You can only produce to endpoints generated by the HTTP component. Therefore it should never be used as input into your camel Routes. To bind/expose an HTTP endpoint via a HTTP server as input to a camel route, you can use the <a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty Component</a></p></td></tr></table></div>

<h3><a name="HTTP-HttpEndpointOptions"></a>HttpEndpoint Options</h3>

<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'> <b>Camel 2.0:</b> Option to disable throwing the <tt>HttpOperationFailedException</tt> in case of failed responses from the remote server. This allows you to get all responses regardles 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'><b>Camel 2.1:</b> If the option is true , HttpProducer will ignore the Exchange.HTTP_URI header, and use the endpoint's URI for request. You may also set the <b>throwExcpetionOnFailure</b> to be false to let the HttpProducer send all the fault response back.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpBindingRef</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Reference to a <tt>org.apache.camel.component.http.HttpBinding</tt> in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>username</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Username for Basic HTTP/NTML Authentication. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>password</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Password for Basic HTTP/NTML Authentication. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>domain</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.1:</b> Domain for NTML Authentication. This option must be used to force NTML authentication. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyHost</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The proxy host name * only for &gt;= Camel 1.6.2 *. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyPort</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The proxy port number * only for &gt;= Camel 1.6.2 *. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyUsername</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Username for proxy authentication * only for &gt;= Camel 1.6.2 *. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyPassword</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Password for proxy authentication * only for &gt;= Camel 1.6.2 *. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpClientConfigurerRef</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Reference to a <tt>org.apache.camel.component.http.HttpClientConfigurer</tt> in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpClient.XXX</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.2 or older:</b> Setting options on the <a href="http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/params/HttpClientParams.html" rel="nofollow">HttpClientParams</a>. For instance <tt>httpClient.soTimeout=5000</tt> will set the <tt>SO_TIMEOUT</tt> to 5 seconds. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpClient.XXX</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Setting options on HttpParam on both HttpClient and HttpClientConnectionManager. For instance <tt>httpClient.soTimeout=5000</tt> will set the <tt>SO_TIMEOUT</tt> to 5 seconds. Notice that the HttpClientConnectionManager is shared among all producers and consumers created by the same HttpComponent. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>clientConnectionManager</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> To use a custom <tt>org.apache.http.conn.ClientConnectionManager</tt>. </td>
</tr>
</tbody></table>

<h3><a name="HTTP-HttpComponentOptions"></a>HttpComponent Options</h3>

<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>maxTotalConnections</tt> </td>
<td class='confluenceTd'> <tt>200</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Defines the maximum number of connections in total. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>connectionsPerRoute</tt> </td>
<td class='confluenceTd'> <tt>20</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Defines the maximum number of connections per route. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>clientConnectionManager</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> To use a custom <tt>org.apache.http.conn.ClientConnectionManager</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpBinding</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>org.apache.camel.component.http.HttpBinding</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpClientConfigurer</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>org.apache.camel.component.http.HttpClientConfigurer</tt>. </td>
</tr>
</tbody></table>


<h3><a name="HTTP-Camel2.3ornewer"></a>Camel 2.3 or newer</h3>
<p>In <b>Camel 2.3</b> we upgraded to use Apache HTTP Client 4.0.1 which is a major upgrade over the older Client 3.1 release. A significant change is that the <tt>HTTPClient</tt> is using a shared <tt>HttpClientConnectionManager</tt> as one giant thread pool. By default its configured to allow 200 concurrent threads. That means you cannot use different options for the <tt>HttpClientConnectionManager</tt>. If you for some odd reason want that you can define a 2nd <tt>CamelHttpComponent</tt> and let it use a different configured <tt>HttpClientConnectionManager</tt>. </p>

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

<h4><a name="HTTP-Camel1.x"></a>Camel 1.x</h4>

<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>HttpProducer.HTTP_URI</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <b>Camel 1.6.0</b>: URI to call. Will override existing URI set directly on the endpoint. Is set on the In message. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>HttpProducer.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. Is set on the Out message. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>HttpProducer.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. Is set on the In message. </td>
</tr>
</tbody></table>

<h4><a name="HTTP-Camel2.x"></a>Camel 2.x</h4>

<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. <b>Camel 2.3.0:</b> 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 exchange.getFromEndpoint().getEndpointUri();</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>
<tr>
<td class='confluenceTd'> <tt>Exchange.HTTP_SERVLET_REQUEST</tt> </td>
<td class='confluenceTd'> <tt>HttpServletRequest</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3.0:</b> The <tt>HttpServletRequest</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.HTTP_SERVLET_RESPONSE</tt> </td>
<td class='confluenceTd'> <tt>HttpServletResponse</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3.0:</b> The <tt>HttpServletResponse</tt> object. </td>
</tr>
</tbody></table>


<h3><a name="HTTP-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="HTTP-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>HttpOperationFailedException</tt> with the information.</li>
	<li>Response code is 400&#43;, Camel regards it as an external server failure and will throw a <tt>HttpOperationFailedException</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 /><p>The option, <tt>throwExceptionOnFailure</tt>, can be set to <tt>false</tt> to prevent the <tt>HttpOperationFailedException</tt> from being thrown for failed response codes. This allows you to get any response from the remote server.<br/>
There is a sample below demonstrating this.</p></td></tr></table></div></li>
</ul>


<h3><a name="HTTP-HttpOperationFailedException"></a>HttpOperationFailedException</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.io.InputStream</tt>, if server provided a body as response</li>
</ul>


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

<p>In <b>Camel 1.5</b> 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="HTTP-HowtogetaccesstoHttpServletRequestandHttpServletResponse"></a>How to get access to HttpServletRequest and HttpServletResponse</h3>
<p><b>Available as of Camel 2.0</b></p>

<p>You can get access to these two using the Camel type converter system using<br/>
<b>NOTE</b> from Camel 2.3.0 you can get the request and response not just from the processor after the camel-jetty or camel-cxf endpoint.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
HttpServletRequest response = exchange.getIn().getBody(HttpServletResponse.class);
</pre>
</div></div>

<h3><a name="HTTP-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">"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">"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>In <b>Camel 1.5.1</b> you can override the HTTP endpoint URI by adding a header with the key, <tt>HttpProducer.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(org.apache.camel.component.http.HttpProducer.HTTP_URI, constant(<span class="code-quote">"http:<span class="code-comment">//newhost"</span>))
</span>	    .to(<span class="code-quote">"http:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div>
<p>In the sample above Camel will call the <a href="http://newhost" rel="nofollow">http://newhost</a> despite the endpoint is configured with <a href="http://oldhost" rel="nofollow">http://oldhost</a>.</p>

<p>And the same code in Camel 2.0:</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(HttpConstants.HTTP_URI, constant(<span class="code-quote">"http:<span class="code-comment">//newhost"</span>))
</span>	    .to(<span class="code-quote">"http:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div>
<p>Where Constants is the class, <tt>org.apache.camel.component.http.Constants</tt>.</p>

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

<p>The <b>http</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>HttpProducer.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">"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(HttpConstants.HTTP_QUERY, constant(<span class="code-quote">"order=123&amp;detail=<span class="code-object">short</span>"</span>))
	    .to(<span class="code-quote">"http:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div>

<h3><a name="HTTP-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>

<p><b>Camel 1.x</b></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(HttpConstants.HTTP_METHOD, constant(org.apache.camel.component.http.HttpMethods.POST))
	    .to(<span class="code-quote">"http:<span class="code-comment">//www.google.com"</span>)
</span>            .to(<span class="code-quote">"mock:results"</span>);
</pre>
</div></div>

<p><b>Camel 2.x</b></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(org.apache.camel.component.http.HttpMethods.POST))
	    .to(<span class="code-quote">"http:<span class="code-comment">//www.google.com"</span>)
</span>            .to(<span class="code-quote">"mock:results"</span>);
</pre>
</div></div>

<p>The method can be written a bit shorter using the string constants:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.setHeader(<span class="code-quote">"CamelHttpMethod"</span>, constant(<span class="code-quote">"POST"</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">"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="HTTP-UsingclienttineoutSOTIMEOUT"></a>Using client tineout - SO_TIMEOUT</h3>
<p>See the unit test in <a href="http://svn.apache.org/viewvc?view=rev&amp;revision=781775" rel="nofollow">this link</a></p>


<h3><a name="HTTP-ConfiguringaProxy"></a>Configuring a Proxy</h3>
<p><b>Only for &gt;= Camel 1.6.2</b><br/>
The HTTP component provides a way to configure a proxy.</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">"http:<span class="code-comment">//oldhost?proxyHost=www.myproxy.com&amp;proxyPort=80"</span>);</span>
</pre>
</div></div>

<p>There is also support for proxy authentication via the <tt>proxyUsername</tt> and <tt>proxyPassword</tt> options.</p>

<h4><a name="HTTP-UsingproxysettingsoutsideofURI"></a>Using proxy settings outside of URI</h4>
<p>*Only for &gt;= Camel 1.6.2 and &lt; Camel 2.2.0 *<br/>
The HTTP component will detect Java System Properties for <tt>http.proxyHost</tt> and <tt>http.proxyPort</tt> and use them if provided.<br/>
See more at <a href="http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html" rel="nofollow">SUN http proxy documentation</a>.</p>

<p>To avoid the System properties conflicts, from Camel 2.2.0 you can only set the proxy configure from CameContext or URI.<br/>
Java DSL :</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
 context.getProperties().put(<span class="code-quote">"http.proxyHost"</span>, <span class="code-quote">"172.168.18.9"</span>);
 context.getProperties().put(<span class="code-quote">"http.proxyPort"</span> <span class="code-quote">"8080"</span>);
</pre>
</div></div>
<p>Spring XML</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   &lt;camelContext&gt;
       &lt;properties&gt;
           &lt;property key=<span class="code-quote">"http.proxyHost"</span> value=<span class="code-quote">"172.168.18.9"</span>/&gt;
           &lt;property key=<span class="code-quote">"http.proxyPort"</span> value=<span class="code-quote">"8080"</span>/&gt;
      &lt;/properties&gt;
   &lt;/camelContext&gt;
</pre>
</div></div>
<p>Camel will first set the settings from Java System or CamelContext Properties and then the endpoint proxy options if provided.<br/>
So you can override the system properties with the endpoint options.</p>

<h3><a name="HTTP-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="HTTP-Samplewithscheduledpoll"></a>Sample with scheduled poll</h4>

<p>The sample polls the Google homepage every 10 seconds and write the page to the file <tt>message.html</tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"timer:<span class="code-comment">//foo?fixedRate=<span class="code-keyword">true</span>&amp;delay=0&amp;period=10000"</span>)
</span>    .to(<span class="code-quote">"http:<span class="code-comment">//www.google.com"</span>)
</span>    .setHeader(FileComponent.HEADER_FILE_NAME, <span class="code-quote">"message.html"</span>).to(<span class="code-quote">"file:target/google"</span>);
</pre>
</div></div>

<h4><a name="HTTP-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">"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="HTTP-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(HttpProducer.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">"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="HTTP-GettingtheResponseCode"></a>Getting the Response Code</h4>

<p>You can get the HTTP response code from the HTTP component by getting the value from the Out message header with <tt>HttpProducer.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">"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(HttpProducer.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(HttpProducer.HTTP_RESPONSE_CODE, <span class="code-object">Integer</span>.class);
</pre>
</div></div>

<h3><a name="HTTP-Using%7B%7BthrowExceptionOnFailure%3Dfalse%7D%7Dtogetanyresponseback"></a>Using <tt>throwExceptionOnFailure=false</tt> to get any response back</h3>

<p><b>Available as of Camel 2.0</b><br/>
In the route below we want to route a message that we <a href="/confluence/display/CAMEL/Content+Enricher" title="Content Enricher">enrich</a> with data returned from a remote HTTP call. As we want any response from the remote server, we set the <tt>throwExceptionOnFailure</tt> option to <tt>false</tt> so we get any response in the <tt>AggregationStrategy</tt>. As the code is based on a unit test that simulates a HTTP status code 404, there is some assertion code etc.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// We set throwExceptionOnFailure to <span class="code-keyword">false</span> to let Camel <span class="code-keyword">return</span> any response from the remove HTTP server without thrown
</span><span class="code-comment">// HttpOperationFailedException in <span class="code-keyword">case</span> of failures.
</span><span class="code-comment">// This allows us to handle all responses in the aggregation strategy where we can check the HTTP response code
</span><span class="code-comment">// and decide what to <span class="code-keyword">do</span>. As <span class="code-keyword">this</span> is based on an unit test we <span class="code-keyword">assert</span> the code is 404
</span>from(<span class="code-quote">"direct:start"</span>).enrich(<span class="code-quote">"http:<span class="code-comment">//localhost:8222/myserver?throwExceptionOnFailure=<span class="code-keyword">false</span>&amp;user=Camel"</span>, <span class="code-keyword">new</span> AggregationStrategy() {
</span>    <span class="code-keyword">public</span> Exchange aggregate(Exchange original, Exchange resource) {
        <span class="code-comment">// get the response code
</span>        <span class="code-object">Integer</span> code = resource.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, <span class="code-object">Integer</span>.class);
        assertEquals(404, code.intValue());
        <span class="code-keyword">return</span> resource;
    }
}).to(<span class="code-quote">"mock:result"</span>);

<span class="code-comment">// <span class="code-keyword">this</span> is our jetty server where we simulate the 404
</span>from(<span class="code-quote">"jetty:<span class="code-comment">//http://localhost:8222/myserver"</span>)
</span>        .process(<span class="code-keyword">new</span> Processor() {
            <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
                exchange.getOut().setBody(<span class="code-quote">"Page not found"</span>);
                exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
            }
        });
</pre>
</div></div>

<h3><a name="HTTP-DisablingCookies"></a>Disabling Cookies</h3>
<p>To disable cookies you can set the HTTP Client to ignore cookies by adding this URI option:<br/>
<tt>httpClient.cookiePolicy=ignoreCookies</tt></p>

<h3><a name="HTTP-AdvancedUsage"></a>Advanced Usage</h3>

<p>If you need more control over the HTTP producer you should use the <tt>HttpComponent</tt> where you can set various classes to give you custom behavior.</p>

<h4><a name="HTTP-SettingMaxConnectionsPerHost"></a>Setting MaxConnectionsPerHost</h4>
<p><b>Camel 2.2 or older</b><br/>
The <a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a> Component has a <tt>org.apache.commons.httpclient.HttpConnectionManager</tt> where you can configure various global configuration for the given component.<br/>
By global, we mean that any endpoint the component creates has the same shared <tt>HttpConnectionManager</tt>. 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 <b>not</b> on the endpoint URI that we usually use. So here comes:</p>

<p>First, we define the <tt>http</tt> component in Spring XML. Yes, we use the same scheme name, <tt>http</tt>, 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.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;bean id=<span class="code-quote">"http"</span> class=<span class="code-quote">"org.apache.camel.component.http.HttpComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"camelContext"</span> ref=<span class="code-quote">"camel"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"httpConnectionManager"</span> ref=<span class="code-quote">"myHttpConnectionManager"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myHttpConnectionManager"</span> class=<span class="code-quote">"org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"params"</span> ref=<span class="code-quote">"myHttpConnectionManagerParams"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myHttpConnectionManagerParams"</span> class=<span class="code-quote">"org.apache.commons.httpclient.params.HttpConnectionManagerParams"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"defaultMaxConnectionsPerHost"</span> value=<span class="code-quote">"5"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

</pre>
</div></div>
<p>And then we can just use it as we normally do in our routes:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span> trace=<span class="code-quote">"true"</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">"http://www.google.com"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p><b>Camel 2.3 or newer</b><br/>
We can se the <tt>MaxConnectionsPerHost</tt> option, which is named <tt>connectionsPerRoute</tt> in HTTP Client 4.x directly on the Camel <tt>HttpComponent</tt>, which can be done as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"http"</span> class=<span class="code-quote">"org.apache.camel.component.http.HttpComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"connectionsPerRoute"</span> value=<span class="code-quote">"5"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<h4><a name="HTTP-UsingHTTPStoauthenticategotchas"></a>Using HTTPS to authenticate gotchas</h4>
<p>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: <tt>httpClient.authenticationPreemptive=true</tt></p>

<h4><a name="HTTP-Acceptingselfsignedcertificationsfromremoteserver"></a>Accepting self signed certifications from remote server</h4>
<p>See this <a href="http://www.nabble.com/Using-HTTPS-in-camel-http-when-remote-side-has-self-signed-cert-td25916878.html" rel="nofollow">link</a> from a mailing list discussion with some code to outline how to do this with the Apache Commons HTTP API.</p>

<h4><a name="HTTP-SettingupSSLforHTTPClient"></a>Setting up SSL for HTTP Client</h4>
<p>Basically camel-http component is built on the top of Apache HTTP client, and you can implement a custom <tt>org.apache.camel.component.http.HttpClientConfigurer</tt> to do some configuration on the http client if you need full control of it.</p>

<p>However if you <em>just</em> want to specify the keystore and truststore you can do this with Apache HTTP <tt>HttpClientConfigurer</tt>, for example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Protocol authhttps = <span class="code-keyword">new</span> Protocol(<span class="code-quote">"https"</span>, <span class="code-keyword">new</span> AuthSSLProtocolSocketFactory(
  <span class="code-keyword">new</span> URL(<span class="code-quote">"file:my.keystore"</span>), <span class="code-quote">"mypassword"</span>,
  <span class="code-keyword">new</span> URL(<span class="code-quote">"file:my.truststore"</span>), <span class="code-quote">"mypassword"</span>), 443);

Protocol.registerProtocol(<span class="code-quote">"https"</span>, authhttps);
</pre>
</div></div>

<p>And then you need to create a class that implements <tt>HttpClientConfigurer</tt>, 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:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
HttpComponent httpComponent = getContext().getComponent(<span class="code-quote">"http"</span>, HttpComponent.class);
httpComponent.setHttpClientConfigurer(<span class="code-keyword">new</span> MyHttpClientConfigurer());
</pre>
</div></div>

<p>If you are doing this using the Spring DSL, you can specify your <tt>HttpClientConfigurer</tt> using the URI.  For example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;bean id=<span class="code-quote">"myHttpClientConfigurer"</span>
 class=<span class="code-quote">"my.https.HttpClientConfigurer"</span>&gt;
&lt;/bean&gt;

&lt;to uri=<span class="code-quote">"https:<span class="code-comment">//myhostname.com:443/myURL?httpClientConfigurerRef=myHttpClientConfigurer"</span>/&gt; </span>
</pre>
</div></div>

<p>As long as you implement the HttpClientConfigurer and configure your keystore and truststore as described above, it will work fine.</p>

<h3><a name="HTTP-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>
</ul>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/HTTP">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=54726&revisedVersion=54&originalVersion=53">View Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/HTTP?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message