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 Tue, 17 Apr 2012 22:12: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/HTTP">HTTP</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bibryam">Bilgin Ibryam</a>
    </h4>
        <br/>
                         <h4>Changes (1)</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" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >h3. Using client <span class="diff-changed-words">ti<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">n</span><span class="diff-added-chars"style="background-color: #dfd;">m</span>eout</span> - SO_TIMEOUT <br></td></tr>
            <tr><td class="diff-unchanged" > <br>See the unit test in [this link|http://svn.apache.org/viewvc?view=rev&amp;revision=781775] <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <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-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-http<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="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][?param1=value1][&amp;param2=value2]
</pre>
</div></div>
<p>Will by default use port 80 for HTTP and 443 for HTTPS.</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>camel-http vs camel-jetty</b><br />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> or the <a href="/confluence/display/CAMEL/SERVLET" title="SERVLET">Servlet Component</a></td></tr></table></div>

<h3><a name="HTTP-Examples"></a>Examples</h3>

<p>Call the url with the body using POST and return response as out message. If body is null call URL using GET and return response as out message</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Java DSL</th>
<th class='confluenceTh'>Spring DSL</th>
</tr>
<tr>
<td class='confluenceTd'><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">//myhost/mypath"</span>);</span></pre>
</div></div></td>
<td class='confluenceTd'><div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><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></pre>
</div></div></td>
</tr>
</tbody></table>
</div>


<p>You can override the HTTP endpoint URI by adding a header. Camel will call the <a href="http://newhost" class="external-link" rel="nofollow">http://newhost</a>. This is very handy for e.g. REST urls.</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Java DSL</th>
</tr>
<tr>
<td class='confluenceTd'><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, simple(<span class="code-quote">"http:<span class="code-comment">//myserver/orders/${header.orderId}"</span>))
</span>  .to(<span class="code-quote">"http:<span class="code-comment">//dummyhost"</span>);</span></pre>
</div></div></td>
</tr>
</tbody></table>
</div>


<p>URI parameters can either be set directly on the endpoint URI or as a header</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Java DSL</th>
</tr>
<tr>
<td class='confluenceTd'><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>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">"http:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div></td>
</tr>
</tbody></table>
</div>


<p>Set the HTTP request method to POST</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Java DSL</th>
<th class='confluenceTh'>Spring DSL</th>
</tr>
<tr>
<td class='confluenceTd'><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">"http:<span class="code-comment">//www.google.com"</span>);</span>
</pre>
</div></div></td>
<td class='confluenceTd'><div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<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>
</pre>
</div></div></td>
</tr>
</tbody></table>
</div>


<h3><a name="HTTP-HttpEndpointOptions"></a>HttpEndpoint 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>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'> 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. <br class="atl-forced-newline" />
<b>Camel 2.3:</b> If the option is true, HttpProducer and CamelServlet will skip the gzip processing if the content-encoding is "gzip". </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>disableStreamCache</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> DefaultHttpBinding will copy the request input stream into a stream cache and put it into message body if this option is false to support read it twice, otherwise DefaultHttpBinding will set the request input stream direct into the message body. </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>. From Camel 2.3 onwards prefer to use the <tt>httpBinding</tt> option. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpBinding</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>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>. From Camel 2.3 onwards prefer to use the <tt>httpClientConfigurer</tt> option. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpClientConfigurer</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'> Setting options on the <a href="http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/params/HttpClientParams.html" class="external-link" 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>clientConnectionManager</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>org.apache.http.conn.ClientConnectionManager</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>transferException</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> 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>HttpOperationFailedException</tt>. The caused exception is required to be serialized. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="HTTP-AuthenticationandProxy"></a>Authentication and Proxy</h3>

<p>The following authentication options can also be set on the HttpEndpoint:</p>

<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>authMethod</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Authentication method, either as <tt>Basic</tt>, <tt>Digest</tt> or <tt>NTLM</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>authMethodPriority</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Priority of authentication methods. Is a list separated with comma. For example: <tt>Basic,Digest</tt> to exclude <tt>NTLM</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>authUsername</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Username for authentication </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>authPassword</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Password for authentication </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>authDomain</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Domain for NTML authentication </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>authHost</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Optional host for 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 </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyPort</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The proxy port number </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyAuthMethod</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Authentication method for proxy, either as <tt>Basic</tt>, <tt>Digest</tt> or <tt>NTLM</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyAuthUsername</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Username for proxy authentication </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyAuthPassword</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Password for proxy authentication </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyAuthDomain</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Domain for proxy NTML authentication </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyAuthHost</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Optional host for proxy NTML authentication </td>
</tr>
</tbody></table>
</div>
</div>

<p>When using authentication you <b>must</b> provide the choice of method for the <tt>authMethod</tt> or <tt>authProxyMethod</tt> options.<br/>
You can configure the proxy and authentication details on either the <tt>HttpComponent</tt> or the <tt>HttpEndoint</tt>. Values provided on the <tt>HttpEndpoint</tt> will take precedence over <tt>HttpComponent</tt>. Its most likely best to configure this on the <tt>HttpComponent</tt> which allows you to do this once.</p>

<p>The <a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a> component uses convention over configuration which means that if you have not explicit set a <tt>authMethodPriority</tt> then it will fallback and use the select(ed) <tt>authMethod</tt> as priority as well. So if you use <tt>authMethod.Basic</tt> then the <tt>auhtMethodPriority</tt> will be <tt>Basic</tt> only.</p>

<h3><a name="HTTP-HttpComponentOptions"></a>HttpComponent 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>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>
<tr>
<td class='confluenceTd'> <tt>httpConnectionManager</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>org.apache.commons.httpclient.HttpConnectionManager</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpConfiguration</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To use a custom <tt>org.apache.camel.component.http.HttpConfiguration</tt> </td>
</tr>
</tbody></table>
</div>
</div>
<p><tt>HttpConfiguration</tt> contains all the options listed in the table above under the section <em>HttpConfiguration - Setting Authentication and Proxy</em>.</p>

<h3><a name="HTTP-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_METHOD</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> HTTP Method / Verb to use (GET/POST/PUT/DELETE/HEAD/OPTIONS/TRACE) </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'> 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'> The <tt>HttpServletResponse</tt> object. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>Exchange.HTTP_PROTOCOL_VERSION</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <b>Camel 2.5:</b> You can set the http protocol version with this header, eg. "HTTP/1.0". If you didn't specify the header, HttpProducer will use the default value "HTTP/1.1" </td>
</tr>
</tbody></table>
</div>
</div>

<p>The header name above are constants. For the spring DSL you have to use the value of the constant instead of the name.</p>

<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+, 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 />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.</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.lang.String</tt>, if server provided a body as response</li>
</ul>


<h3><a name="HTTP-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="HTTP-HowtogetaccesstoHttpServletRequestandHttpServletResponse"></a>How to get access to HttpServletRequest and HttpServletResponse</h3>

<p>You can get access to these two using the Camel type converter system using</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-UsingclienttimeoutSOTIMEOUT"></a>Using client timeout - SO_TIMEOUT</h3>

<p>See the unit test in <a href="http://svn.apache.org/viewvc?view=rev&amp;revision=781775" class="external-link" rel="nofollow">this link</a></p>

<h2><a name="HTTP-MoreExamples"></a>More Examples</h2>

<h3><a name="HTTP-ConfiguringaProxy"></a>Configuring a Proxy</h3>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Java DSL</th>
</tr>
<tr>
<td class='confluenceTd'><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></td>
</tr>
</tbody></table>
</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>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Java DSL</th>
<th class='confluenceTh'>Spring DSL</th>
</tr>
<tr>
<td class='confluenceTd'><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></td>
<td class='confluenceTd'><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></td>
</tr>
</tbody></table>
</div>


<p>Options on Endpoint will override options on the context.</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></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
setProperty(Exchange.CHARSET_NAME, <span class="code-quote">"iso-8859-1"</span>);
</pre>
</div></div>

<h3><a name="HTTP-Samplewithscheduledpoll"></a>Sample with scheduled poll</h3>

<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>

<h3><a name="HTTP-GettingtheResponseCode"></a>Getting the Response Code</h3>

<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(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(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>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:{{port}}/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:{{port}}/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>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>

<h4><a name="HTTP-Usingpreemptiveauthentication"></a>Using preemptive authentication</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-Acceptingselfsignedcertificatesfromremoteserver"></a>Accepting self signed certificates 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" class="external-link" 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>


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

<p>As of Camel 2.8, the HTTP4 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 HTTP4 component.</p>

<p>The version of the Apache HTTP client used in this component resolves SSL/TLS information from a global "protocol" registry.&nbsp; This component provides an implementation, <tt>org.apache.camel.component.http.SSLContextParametersSecureProtocolSocketFactory</tt>, of the HTTP client's protocol socket factory in order to support the use of the Camel JSSE Configuration utility.&nbsp; The following example demonstrates how to configure the protocol registry and use the registered protocol information in a route.</p>


<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);

ProtocolSocketFactory factory =
    <span class="code-keyword">new</span> SSLContextParametersSecureProtocolSocketFactory(scp);

Protocol.registerProtocol(<span class="code-quote">"https"</span>,
        <span class="code-keyword">new</span> Protocol(
        <span class="code-quote">"https"</span>,
        factory,
        443));

from(<span class="code-quote">"direct:start"</span>)
        .to(<span class="code-quote">"https:<span class="code-comment">//mail.google.com/mail/"</span>).to(<span class="code-quote">"mock:results"</span>);</span>
</pre>
</div></div>

<h5><a name="HTTP-ConfiguringApacheHTTPClientDirectly"></a>Configuring Apache HTTP Client Directly</h5>

<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="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/HTTP">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=54726&revisedVersion=75&originalVersion=74">View Changes</a>
                |
        <a href="https://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