camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > HTTP4
Date Sun, 30 Jan 2011 20:36:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2036/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/HTTP4">HTTP4</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="HTTP4-HTTP4Component"></a>HTTP4 Component</h2>

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

<p>The <b>http4:</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-http4<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>

<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-http4
vs camel-http</b><br />Camel-http4 uses HttpClient 4.x while camel-http uses HttpClient
3.x.</td></tr></table></div>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
http4: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>camel-http4
vs camel-jetty</b><br />You can only produce to endpoints generated by the HTTP4
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></td></tr></table></div>

<h3><a name="HTTP4-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>x509HostnameVerifier</tt> </td>
<td class='confluenceTd'>  <tt>org.apache.http.conn.ssl.BrowserCompatHostnameVerifier</tt>
</td>
<td class='confluenceTd'> <b>Camel 2.7:</b> You can refer to a different
<tt>org.apache.http.conn.ssl.X509HostnameVerifier</tt> instance in the <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a> such as <tt>org.apache.http.conn.ssl.StrictHostnameVerifier</tt>
or <tt>org.apache.http.conn.ssl.AllowAllHostnameVerifier</tt>. </td>
</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 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 , 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. 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>.
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>.
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/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/BasicHttpParams.html"
class="external-link" rel="nofollow">BasicHttpParams</a>. For instance <tt>httpClient.soTimeout=5000</tt>
will set the <tt>SO_TIMEOUT</tt> to 5 seconds. Look on the setter methods of the
following parameter beans for a complete reference: <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/auth/params/AuthParamBean.html"
class="external-link" rel="nofollow">AuthParamBean</a>, <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/params/ClientParamBean.html"
class="external-link" rel="nofollow">ClientParamBean</a>, <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/conn/params/ConnConnectionParamBean.html"
class="external-link" rel="nofollow">ConnConnectionParamBean</a>, <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/conn/params/ConnRouteParamBean.html"
class="external-link" rel="nofollow">ConnRouteParamBean</a>, <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/cookie/params/CookieSpecParamBean.html"
class="external-link" rel="nofollow">CookieSpecParamBean</a>, <a href="http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/HttpConnectionParamBean.html"
class="external-link" rel="nofollow">HttpConnectionParamBean</a> and <a href="http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/HttpProtocolParamBean.html"
class="external-link" rel="nofollow">HttpProtocolParamBean</a> </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'> 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>
<tr>
<td class='confluenceTd'> <tt>maxTotalConnections</tt> </td>
<td class='confluenceTd'> <tt>200</tt> </td>
<td class='confluenceTd'> The maximum number of connections. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>connectionsPerRoute</tt> </td>
<td class='confluenceTd'> <tt>20</tt> </td>
<td class='confluenceTd'> The maximum number of connections per route. </td>
</tr>
</tbody></table>
</div>
</div>

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

<h4><a name="HTTP4-SettingBasicAuthenticationandProxy"></a>Setting Basic
Authentication and Proxy</h4>
<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>username</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Username for authentication. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>password</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Password for authentication. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>domain</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The domain name for authentication. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>host</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The host name 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>proxyUsername</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Username for proxy authentication </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyPassword</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Password for proxy authentication </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyDomain</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The proxy domain name </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>proxyNtHost</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The proxy Nt host name </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="HTTP4-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>x509HostnameVerifier</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.7:</b> To use a custom <tt>org.apache.http.conn.ssl.X509HostnameVerifier</tt>
</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="HTTP4-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. </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="HTTP4-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="HTTP4-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
/>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="HTTP4-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="HTTP4-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="HTTP4-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<br/>
<b>NOTE</b> 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="HTTP4-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">"http4:<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">"http4://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>HttpConstants.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(HttpConstants.HTTP_URI, constant(<span class="code-quote">"http:<span
class="code-comment">//newhost"</span>))
</span>	    .to(<span class="code-quote">"http4:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div>
<p>In the sample above Camel will call the <a href="http://newhost" class="external-link"
rel="nofollow">http://newhost</a> despite the endpoint is configured with <a href="http4://oldhost"
class="external-link" rel="nofollow">http4://oldhost</a>.<br/>
Where Constants is the class, <tt>org.apache.camel.component.http4.Constants</tt>.</p>

<h3><a name="HTTP4-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>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">"http4:<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">"http4:<span class="code-comment">//oldhost"</span>);</span>
</pre>
</div></div>


<h3><a name="HTTP4-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 HTTP4 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(org.apache.camel.component.http4.HttpMethods.POST))
	    .to(<span class="code-quote">"http4:<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">"http4://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="HTTP4-UsingclienttineoutSOTIMEOUT"></a>Using client tineout
- SO_TIMEOUT</h3>

<p>See the <a href="https://svn.apache.org/repos/asf/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpSOTimeoutTest.java"
class="external-link" rel="nofollow">HttpSOTimeoutTest</a> unit test.</p>


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

<p>The HTTP4 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">"http4:<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="HTTP4-UsingproxysettingsoutsideofURI"></a>Using proxy settings
outside of URI</h4>

<p>To avoid the System properties conflicts, 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="HTTP4-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="HTTP4-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">"http4:<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="HTTP4-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">"http4:<span class="code-comment">//www.google.com/search?q=Camel"</span>,
<span class="code-keyword">null</span>);</span>
</pre>
</div></div>

<h4><a name="HTTP4-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">"http4:<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="HTTP4-GettingtheResponseCode"></a>Getting the Response Code</h4>

<p>You can get the HTTP response code from the HTTP4 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">"http4:<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="HTTP4-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="HTTP4-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="HTTP4-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="HTTP4-SettingupSSLforHTTPClient"></a>Setting up SSL for HTTP
Client</h4>
<p>Basically camel-http4 component is built on the top of <a href="http://hc.apache.org/"
class="external-link" rel="nofollow">Apache HTTP client</a>. Please refer to <a
href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e537" class="external-link"
rel="nofollow">SSL/TLS customization</a> for details or have a look into the <tt>org.apache.camel.component.http4.HttpsServerTestSupport</tt>
unit test base class.<br/>
You can also implement a custom <tt>org.apache.camel.component.http4.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">
KeyStore keystore = ...;
KeyStore truststore = ...;

SchemeRegistry registry = <span class="code-keyword">new</span> SchemeRegistry();
registry.register(<span class="code-keyword">new</span> Scheme(<span class="code-quote">"https"</span>,
443, <span class="code-keyword">new</span> SSLSocketFactory(keystore, <span
class="code-quote">"mypassword"</span>, truststore)));
</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">"http4"</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">"https4:<span class="code-comment">//myhostname.com:443/myURL?httpClientConfigurer=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>
    </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/HTTP4">View Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/HTTP4?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message