camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Jetty
Date Mon, 19 Apr 2010 17:28:04 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1520/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/Jetty">Jetty</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~dvaleri">David
Valeri</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="Jetty-JettyComponent"></a>Jetty Component</h2>

<p><b>Supports non blocking</b> <b><a href="/confluence/display/CAMEL/Request+Reply"
title="Request Reply">Request Reply</a></b> <b>producer in Camel 2.1
onwards</b></p>

<p>The <b>jetty</b> component provides HTTP-based <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">endpoints</a> for consuming HTTP requests. That is, the Jetty component
behaves as a simple Web server.</p>

<p>In <b>Camel 2.1</b> the <b>jetty</b> component also provides
non blocking <a href="/confluence/display/CAMEL/Request+Reply" title="Request Reply">Request
Reply</a> for producing HTTP requests. That is it can also acts as HTTP client sending
to a remote HTTP server and use non blocking in this process. See more at <a href="/confluence/display/CAMEL/ToAsync"
title="ToAsync">ToAsync</a> and the <a href="/confluence/display/CAMEL/HTTP+Async+Example"
title="HTTP Async Example">HTTP Async Example</a>.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
jetty:http:<span class="code-comment">//hostname[:port][/resourceUri][?options]</span>
</pre>
</div></div>

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

<h3><a name="Jetty-Options"></a>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>sessionSupport</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Specifies whether to enable the session manager on the server
side of Jetty. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpClient.XXX</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.6.0/2.0:</b> Configuration of Jetty's
<a href="http://wiki.eclipse.org/Jetty/Tutorial/HttpClient" rel="nofollow">HttpClient</a>.
For example, setting <tt>httpClient.idleTimeout=30000</tt> sets the idle timeout
to 30 seconds. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>httpBindingRef</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.6.0/2.0:</b> Reference to an <tt>org.apache.camel.component.http.HttpBinding</tt>
in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
<tt>HttpBinding</tt> can be used to customize how a response should be written.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>matchOnUriPrefix</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.0:</b> Whether or not the <tt>CamelServlet</tt>
should try to find a target consumer by matching the URI prefix if no exact match is found.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>handlers</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.6.1/2.0:</b> Specifies a comma-delimited
set of <tt>org.mortbay.jetty.Handler</tt> instances in your <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a> (such as your Spring <tt>ApplicationContext</tt>).
These handlers are added to the Jetty servlet context (for example, to add security). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>chunked</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>From Camel 2.2:</b> If this option is false
Jetty servlet will disable the HTTP streaming and set the content-length header on the response
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>enableJmx</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>From Camel 2.3:</b> If this option is true,
Jetty JMX support will be enabled for this endpoint. See <a href="#Jetty-JettyJMXsupport">Jetty
JMX support</a> for more details. </td>
</tr>
</tbody></table>

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

<p>Camel uses the same message headers as the <a href="/confluence/display/CAMEL/HTTP"
title="HTTP">HTTP</a> component.<br/>
>From Camel 2.2, it also uses (Exchange.HTTP_CHUNKED,CamelHttpChunked) header to turn on
or turn off the chuched encoding on the camel-jetty consumer.</p>

<p>Camel also populates <b>all</b> request.parameter and request.headers.
For example, given a client request with the URL, <tt><a href="http://myserver/myserver?orderid=123"
rel="nofollow">http://myserver/myserver?orderid=123</a></tt>, the exchange
will contain a header named <tt>orderid</tt> with the value 123. This feature
was introduced in Camel 1.5.</p>

<p>From Camel 1.6.3 and Camel 2.2.0, you can get the request.parameter from the message
header not only from Get Method, but also other HTTP method.</p>

<h3><a name="Jetty-Usage"></a>Usage</h3>

<p>The Jetty component only supports consumer endpoints. Therefore a Jetty endpoint
URI should be used only as the <b>input</b> for a Camel route (in a <tt>from()</tt>
DSL call). To issue HTTP requests against other HTTP endpoints, use the <a href="/confluence/display/CAMEL/HTTP"
title="HTTP">HTTP Component</a></p>

<h3><a name="Jetty-Sample"></a>Sample</h3>

<p>In this sample we define a route that exposes a HTTP service at <tt><a href="http://localhost:8080/myapp/myservice"
rel="nofollow">http://localhost:8080/myapp/myservice</a></tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"jetty:http:<span class="code-comment">//localhost:9080/myapp/myservice"</span>).process(<span
class="code-keyword">new</span> MyBookService());</span>
</pre>
</div></div>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Usage of localhost</b><br
/><p>When you specify <tt>localhost</tt> in a URL, Camel exposes the
endpoint only on the local TCP/IP network interface, so it cannot be accessed from outside
the machine it operates on.</p>

<p>If you need to expose a Jetty endpoint on a specific network interface, the numerical
IP address of this interface should be used as the host. If you need to expose a Jetty endpoint
on all network interfaces, the <tt>0.0.0.0</tt> address should be used.</p></td></tr></table></div>

<p>Our business logic is implemented in the <tt>MyBookService</tt> class,
which accesses the HTTP request contents and then returns a response.<br/>
<b>Note:</b> The <tt>assert</tt> call appears in this example, because
the code is part of an unit test.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MyBookService
<span class="code-keyword">implements</span> Processor {
    <span class="code-keyword">public</span> void process(Exchange exchange) <span
class="code-keyword">throws</span> Exception {
        <span class="code-comment">// just get the body as a string
</span>        <span class="code-object">String</span> body = exchange.getIn().getBody(<span
class="code-object">String</span>.class);

        <span class="code-comment">// we have access to the HttpServletRequest here
and we can grab it <span class="code-keyword">if</span> we need it
</span>        HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
        assertNotNull(req);

        <span class="code-comment">// <span class="code-keyword">for</span>
unit testing
</span>        assertEquals(<span class="code-quote">"bookid=123"</span>,
body);

        <span class="code-comment">// send a html response
</span>        exchange.getOut().setBody(<span class="code-quote">"&lt;html&gt;&lt;body&gt;Book
123 is Camel in Action&lt;/body&gt;&lt;/html&gt;"</span>);
    }
}
</pre>
</div></div>

<p>The following sample shows a content-based route that routes all requests containing
the URI parameter, <tt>one</tt>, to the endpoint, <tt>mock:one</tt>,
and all others to <tt>mock:other</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"jetty:"</span> + serverUri)
    .choice()
    .when().simple(<span class="code-quote">"in.header.one"</span>).to(<span
class="code-quote">"mock:one"</span>)
    .otherwise()
    .to(<span class="code-quote">"mock:other"</span>);
</pre>
</div></div>
<p>So if a client sends the HTTP request, <tt><a href="http://serverUri?one=hello"
rel="nofollow">http://serverUri?one=hello</a></tt>, the Jetty component will
copy the HTTP request parameter, <tt>one</tt> to the exchange's <tt>in.header</tt>.
We can then use the <tt>simple</tt> language to route exchanges that contain this
header to a specific endpoint and all others to another. If we used a language more powerful
than <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a>--<del>such
as <a href="/confluence/display/CAMEL/EL" title="EL">EL</a> or <a href="/confluence/display/CAMEL/OGNL"
title="OGNL">OGNL</a></del>--we could also test for the parameter value and
do routing based on the header value as well.</p>

<h3><a name="Jetty-SessionSupport"></a>Session Support</h3>

<p>The session support option, <tt>sessionSupport</tt>, can be used to enable
a <tt>HttpSession</tt> object and access the session object while processing the
exchange. For example, the following route enables sessions:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span><span class="code-tag">&lt;from
uri=<span class="code-quote">"jetty:http://0.0.0.0/myapp/myservice/?sessionSupport=true"</span>/&gt;</span><span
class="code-tag">&lt;processRef ref=<span class="code-quote">"myCode"</span>/&gt;</span><span
class="code-tag">&lt;route&gt;</span>
</pre>
</div></div>

<p>The <tt>myCode</tt> <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> can be instantiated by a Spring <tt>bean</tt>
element:</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">"myCode"</span>class=<span
class="code-quote">"com.mycompany.MyCodeProcessor"</span>/&gt;</span>
</pre>
</div></div>

<p>Where the processor implementation can access the <tt>HttpSession</tt>
as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
publicvoid process(Exchange exchange)throwsException { HttpSession session = ((HttpExchange)exchange).getRequest().getSession();
... }
</pre>
</div></div>
<h3><a name="Jetty-SSLSupport%28HTTPS%29"></a>SSL Support (HTTPS)</h3>

<p>Jetty provides SSL support out of the box. To enable Jetty to run in SSL mode, simply
format the URI with the <tt>https://</tt> prefix---for example:</p>

<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">"jetty:https://0.0.0.0/myapp/myservice/"</span>/&gt;</span>
</pre>
</div></div>

<p>Jetty also needs to know where to load your keystore from and what passwords to use
in order to load the correct SSL certificate. Set the following JVM System Properties:</p>

<p><b>until Camel 2.2</b></p>
<ul>
	<li><tt>jetty.ssl.keystore</tt> specifies the location of the Java keystore
file, which contains the Jetty server's own X.509 certificate in a <em>key entry</em>.
A key entry stores the X.509 certificate (effectively, the <em>public key</em>)
and also its associated private key.</li>
	<li><tt>jetty.ssl.password</tt> the store password, which is required to
access the keystore file (this is the same password that is supplied to the <tt>keystore</tt>
command's <tt>&#45;storepass</tt> option).</li>
	<li><tt>jetty.ssl.keypassword</tt> the key password, which is used to access
the certificate's key entry in the keystore (this is the same password that is supplied to
the <tt>keystore</tt> command's <tt>&#45;keypass</tt> option).</li>
</ul>


<p><b>from Camel 2.3 onwards</b></p>
<ul>
	<li><tt>org.eclipse.jetty.ssl.keystore</tt> specifies the location of the
Java keystore file, which contains the Jetty server's own X.509 certificate in a <em>key
entry</em>. A key entry stores the X.509 certificate (effectively, the <em>public
key</em>) and also its associated private key.</li>
	<li><tt>org.eclipse.jetty.ssl.password</tt> the store password, which is
required to access the keystore file (this is the same password that is supplied to the <tt>keystore</tt>
command's <tt>&#45;storepass</tt> option).</li>
	<li><tt>org.eclipse.jetty.ssl.keypassword</tt> the key password, which
is used to access the certificate's key entry in the keystore (this is the same password that
is supplied to the <tt>keystore</tt> command's <tt>&#45;keypass</tt>
option).</li>
</ul>


<p>For details of how to configure SSL on a Jetty endpoint, read the following documentation
at the Jetty Site: <span style='cursor:pointer;width:auto' class='clickable' title='link'
onclick='document.location.href="/confluence/pages/createpage.action?spaceKey=CAMEL&title=link&linkCreation=true&fromPageId=64903"'
onkeypress='document.location.href="/confluence/pages/createpage.action?spaceKey=CAMEL&title=link&linkCreation=true&fromPageId=64903"'><a
href="http://docs.codehaus.org/display/JETTY/How+to+configure+SSL" rel="nofollow">http://docs.codehaus.org/display/JETTY/How+to+configure+SSL</a></span></p>

<p>Some SSL properties aren't exposed directly by Camel, however Camel does expose the
underlying SslSocketConnector, which will allow you to set properties like needClientAuth
for mutual authentication requiring a client certificate or wantClientAuth for mutual authentication
where a client doesn't need a certificate but can have one. There's a slight difference between
Camel 1.6.x and 2.x:</p>

<p><b>Camel 1.x</b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;bean id=<span class="code-quote">"jetty"</span>class=<span class="code-quote">"org.apache.camel.component.jetty.JettyHttpComponent"</span>&gt;
&lt;property name=<span class="code-quote">"sslSocketConnector"</span>&gt;
&lt;bean class=<span class="code-quote">"org.mortbay.jetty.security.SslSocketConnector"</span>&gt;
&lt;property name=<span class="code-quote">"password"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"keyPassword"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"keystore"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"wantClientAuth"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"truststore"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;/bean&gt; &lt;/property&gt;
&lt;/bean&gt;
</pre>
</div></div>

<p><b>until Camel 2.2</b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;bean id=<span class="code-quote">"jetty"</span>class=<span class="code-quote">"org.apache.camel.component.jetty.JettyHttpComponent"</span>&gt;
&lt;property name=<span class="code-quote">"sslSocketConnectors"</span>&gt;
&lt;map&gt; &lt;entry key=<span class="code-quote">"8043"</span>&gt;
&lt;bean class=<span class="code-quote">"org.mortbay.jetty.security.SslSocketConnector"</span>&gt;
&lt;property name=<span class="code-quote">"password"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"keyPassword"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"keystore"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"needClientAuth"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"truststore"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;/bean&gt; &lt;/entry&gt;
&lt;/map&gt; &lt;/property&gt; &lt;/bean&gt;
</pre>
</div></div>


<p><b>from Camel 2.3 onwards</b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;bean id=<span class="code-quote">"jetty"</span>class=<span class="code-quote">"org.apache.camel.component.jetty.JettyHttpComponent"</span>&gt;
&lt;property name=<span class="code-quote">"sslSocketConnectors"</span>&gt;
&lt;map&gt; &lt;entry key=<span class="code-quote">"8043"</span>&gt;
&lt;bean class=<span class="code-quote">"org.eclipse.jetty.server.ssl.SslSocketConnector"</span>&gt;
&lt;property name=<span class="code-quote">"password"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"keyPassword"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"keystore"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"needClientAuth"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;property name=<span class="code-quote">"truststore"</span>value=<span
class="code-quote">"..."</span>/&gt; &lt;/bean&gt; &lt;/entry&gt;
&lt;/map&gt; &lt;/property&gt; &lt;/bean&gt;
</pre>
</div></div>

<p>The value you use as keys in the above map is the port you configure Jetty to listen
on.</p>

<h3><a name="Jetty-DefaultbehaviorforreturningHTTPstatuscodes"></a>Default
behavior for returning HTTP status codes</h3>

<p>The default behavior of HTTP status codes is defined by the <tt>org.apache.camel.component.http.DefaultHttpBinding</tt>
class, which handles how a response is written and also sets the HTTP status code.</p>

<p>If the exchange was processed successfully, the 200 HTTP status code is returned.<br/>
If the exchange failed with an exception, the 500 HTTP status code is returned, and the stacktrace
is returned in the body. If you want to specify which HTTP status code to return, set the
code in the <tt>HttpProducer.HTTP_RESPONSE_CODE</tt> header of the OUT message.</p>

<h3><a name="Jetty-CustomizingHttpBinding"></a>Customizing HttpBinding</h3>

<p><b>Available as of Camel 1.5.1/2.0</b></p>

<p>By default, Camel uses the <tt>org.apache.camel.component.http.DefaultHttpBinding</tt>
to handle how a response is written. If you like, you can customize this behavior either by
implementing your own <tt>HttpBinding</tt> class or by extending <tt>DefaultHttpBinding</tt>
and overriding the appropriate methods.</p>

<p>The following example shows how to customize the <tt>DefaultHttpBinding</tt>
in order to change how exceptions are returned:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MyHttpBinding
<span class="code-keyword">extends</span> DefaultHttpBinding {

    @Override
    <span class="code-keyword">public</span> void doWriteExceptionResponse(Throwable
exception, HttpServletResponse response) <span class="code-keyword">throws</span>
IOException {
        <span class="code-comment">// we override the doWriteExceptionResponse as we
only want to alter the binding how exceptions is
</span>        <span class="code-comment">// written back to the client. 
</span>
        <span class="code-comment">// we just <span class="code-keyword">return</span>
HTTP 200 so the client thinks its okay
</span>        response.setStatus(200);
        <span class="code-comment">// and we <span class="code-keyword">return</span>
<span class="code-keyword">this</span> fixed text
</span>        response.getWriter().write(<span class="code-quote">"Something
went wrong but we dont care"</span>);
    }
}
</pre>
</div></div>

<p>We can then create an instance of our binding and register it in the Spring registry
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">"mybinding"</span>class=<span
class="code-quote">"com.mycompany.MyHttpBinding"</span>/&gt;</span>
</pre>
</div></div>

<p>And then we can reference this binding when we define the route:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span><span class="code-tag">&lt;from
uri=<span class="code-quote">"jetty:http://0.0.0.0:8080/myapp/myservice?httpBindingRef=mybinding"</span>/&gt;</span><span
class="code-tag">&lt;to uri=<span class="code-quote">"bean:doSomething"</span>/&gt;</span><span
class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="Jetty-Jettyhandlersandsecurityconfiguration"></a>Jetty handlers
and security configuration</h3>

<p><b>Available as of Camel 1.6.1/2.0:</b> You can configure a list of Jetty
handlers on the endpoint, which can be useful for enabling advanced Jetty security features.
These handlers are configured in Spring XML as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;-- Jetty Security handling --&gt;</span><span
class="code-tag">&lt;bean id=<span class="code-quote">"userRealm"</span>class=<span
class="code-quote">"org.mortbay.jetty.plus.jaas.JAASUserRealm"</span>&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"name"</span>value=<span
class="code-quote">"tracker-users"</span>/&gt;</span><span class="code-tag">&lt;property
name=<span class="code-quote">"loginModuleName"</span>value=<span class="code-quote">"ldaploginmodule"</span>/&gt;</span><span
class="code-tag">&lt;/bean&gt;</span><span class="code-tag">&lt;bean
id=<span class="code-quote">"constraint"</span>class=<span class="code-quote">"org.mortbay.jetty.security.Constraint"</span>&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"name"</span>value=<span
class="code-quote">"BASIC"</span>/&gt;</span><span class="code-tag">&lt;property
name=<span class="code-quote">"roles"</span>value=<span class="code-quote">"tracker-users"</span>/&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"authenticate"</span>value=<span
class="code-quote">"true"</span>/&gt;</span><span class="code-tag">&lt;/bean&gt;</span><span
class="code-tag">&lt;bean id=<span class="code-quote">"constraintMapping"</span>class=<span
class="code-quote">"org.mortbay.jetty.security.ConstraintMapping"</span>&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"constraint"</span>ref=<span
class="code-quote">"constraint"</span>/&gt;</span><span class="code-tag">&lt;property
name=<span class="code-quote">"pathSpec"</span>value=<span class="code-quote">"/*"</span>/&gt;</span><span
class="code-tag">&lt;/bean&gt;</span><span class="code-tag">&lt;bean
id=<span class="code-quote">"securityHandler"</span>class=<span class="code-quote">"org.mortbay.jetty.security.SecurityHandler"</span>&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"userRealm"</span>ref=<span
class="code-quote">"userRealm"</span>/&gt;</span><span class="code-tag">&lt;property
name=<span class="code-quote">"constraintMappings"</span>ref=<span class="code-quote">"constraintMapping"</span>/&gt;</span><span
class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p><b>And from Camel 2.3 onwards</b> you can configure a list of Jetty handlers
as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;-- Jetty Security handling --&gt;</span><span
class="code-tag">&lt;bean id=<span class="code-quote">"constraint"</span>class=<span
class="code-quote">"org.eclipse.jetty.http.security.Constraint"</span>&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"name"</span>value=<span
class="code-quote">"BASIC"</span>/&gt;</span><span class="code-tag">&lt;property
name=<span class="code-quote">"roles"</span>value=<span class="code-quote">"tracker-users"</span>/&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"authenticate"</span>value=<span
class="code-quote">"true"</span>/&gt;</span><span class="code-tag">&lt;/bean&gt;</span><span
class="code-tag">&lt;bean id=<span class="code-quote">"constraintMapping"</span>class=<span
class="code-quote">"org.eclipse.jetty.security.ConstraintMapping"</span>&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"constraint"</span>ref=<span
class="code-quote">"constraint"</span>/&gt;</span><span class="code-tag">&lt;property
name=<span class="code-quote">"pathSpec"</span>value=<span class="code-quote">"/*"</span>/&gt;</span><span
class="code-tag">&lt;/bean&gt;</span><span class="code-tag">&lt;bean
id=<span class="code-quote">"securityHandler"</span>class=<span class="code-quote">"org.eclipse.jetty.security.ConstraintSecurityHandler"</span>&gt;</span><span
class="code-tag">&lt;property name=<span class="code-quote">"authenticator"</span>&gt;</span><span
class="code-tag">&lt;bean class=<span class="code-quote">"org.eclipse.jetty.security.authentication.BasicAuthenticator"</span>/&gt;</span><span
class="code-tag">&lt;/property&gt;</span><span class="code-tag">&lt;property
name=<span class="code-quote">"constraintMappings"</span>&gt;</span><span
class="code-tag">&lt;list&gt;</span><span class="code-tag">&lt;ref
bean=<span class="code-quote">"constraintMapping"</span>/&gt;</span><span
class="code-tag">&lt;/list&gt;</span><span class="code-tag">&lt;/property&gt;</span><span
class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>You can then define the endpoint as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"jetty:http:<span class="code-comment">//0.0.0.0:9080/myservice?handlers=securityHandler"</span>)</span>
</pre>
</div></div>

<p>If you need more handlers, set the <tt>handlers</tt> option equal to
a comma-separated list of bean IDs.</p>

<h3><a name="Jetty-HowtoreturnacustomHTTP500replymessage"></a>How to return
a custom HTTP 500 reply message</h3>

<p>You may want to return a custom reply message when something goes wrong, instead
of the default reply message Camel <a href="/confluence/display/CAMEL/Jetty" title="Jetty">Jetty</a>
replies with.<br/>
You could use a custom <tt>HttpBinding</tt> to be in control of the message mapping,
but often it may be easier to use Camel's <a href="/confluence/display/CAMEL/Exception+Clause"
title="Exception Clause">Exception Clause</a> to construct the custom reply message.
For example as show here, where we return <tt>Dude something went wrong</tt> with
HTTP error code 500:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"jetty:<span class="code-comment">//http://localhost:8234/myserver"</span>)
</span>    <span class="code-comment">// use onException to <span class="code-keyword">catch</span>
all exceptions and <span class="code-keyword">return</span> a custom reply message
</span>    .onException(Exception.class)
        .handled(<span class="code-keyword">true</span>)
        <span class="code-comment">// create a custom failure response
</span>        .transform(constant(<span class="code-quote">"Dude something went
wrong"</span>))
        <span class="code-comment">// we must remember to set error code 500 as handled(<span
class="code-keyword">true</span>)
</span>        <span class="code-comment">// otherwise would let Camel thing its
a OK response (200)
</span>        .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500))
    .end()
    <span class="code-comment">// now just force an exception immediately
</span>    .throwException(<span class="code-keyword">new</span> IllegalArgumentException(<span
class="code-quote">"I cannot <span class="code-keyword">do</span> <span
class="code-keyword">this</span>"</span>));
</pre>
</div></div>

<h3><a name="Jetty-MultipartFormsupport"></a>Multi-part Form support</h3>

<p>From Camel 2.3.0, camel-jetty support to multipart form post out of box. The submitted
form-data are mapped into the message header. Camel-jetty creates an attachment for each uploaded
file. The file name is mapped to the name of the attachment. The content type is set as the
content type of the attachment file name. You can find the example here.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"jetty:<span class="code-comment">//http://localhost:9080/test"</span>).process(<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 {
        Message in = exchange.getIn();
        assertEquals(<span class="code-quote">"Get a wrong attachement size"</span>,
1, in.getAttachments().size());
        <span class="code-comment">// The file name is attachment id
</span>        DataHandler data = in.getAttachment(<span class="code-quote">"NOTICE.txt"</span>);
        assertNotNull(<span class="code-quote">"Should get the DataHandle NOTICE.txt"</span>,
data);
        assertEquals(<span class="code-quote">"Get a wrong content type"</span>,
<span class="code-quote">"text/plain"</span>, data.getContentType());
        <span class="code-comment">// The other form date can be get from the message
header
</span>        exchange.getOut().setBody(in.getHeader(<span class="code-quote">"comment"</span>));
    }

});
</pre>
</div></div>

<h3><a name="Jetty-JettyJMXsupport"></a>Jetty JMX support</h3>

<p>From Camel 2.3.0, camel-jetty supports the enabling of Jetty's JMX capabilities at
the component and endpoint level with the endpoint configuration taking priority.  Note that
JMX must be enabled within the Camel context in order to enable JMX support in this component
as the component provides Jetty with a reference to the MBeanServer registered with the context.
 Because the camel-jetty component caches and reuses Jetty resources for a given protocol/host/port
pairing, this configuration option will only be evaluated during the creation of the first
endpoint to use a protocol/host/port pairing.  For example, given two routes created from
the following XML fragments, JMX support would remain enabled for all endpoints listening
on "https://0.0.0.0".</p>

<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">"jetty:https://0.0.0.0/myapp/myservice1/?enableJmx=true"</span>/&gt;</span>
</pre>
</div></div>

<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">"jetty:https://0.0.0.0/myapp/myservice2/?enableJmx=false"</span>/&gt;</span>
</pre>
</div></div>

<p>The camel-jetty component also provides for direct configuration of the Jetty MBeanContainer.
 Jetty creates MBean names dynamically.  If you are running another instance of Jetty outside
of the camel-context and sharing the same MBeanServer between the instances, you can provide
both instances with a reference to the same MBeanContainer in order to avoid name collisions
when registering Jetty MBeans.</p>


<h3><a name="Jetty-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/HTTP" title="HTTP">HTTP</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/Jetty">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=64903&revisedVersion=33&originalVersion=32">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Jetty?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message