camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > SERVLET
Date Thu, 07 Feb 2013 14:01: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/SERVLET">SERVLET</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</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" > <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">From
*Camel 2.11* onwards Camel will detect this duplicate and fail to start the application. You
can control to ignore this duplicate by setting the servlet init-parameter ignoreDuplicateServletName
to true as follows: <br> <br>{code:xml} <br>  &lt;servlet&gt; <br>
   &lt;servlet-name&gt;CamelServlet&lt;/servlet-name&gt; <br>    &lt;display-name&gt;Camel
Http Transport Servlet&lt;/display-name&gt; <br>    &lt;servlet-class&gt;org.apache.camel.component.servlet.CamelHttpTransportServlet&lt;/servlet-class&gt;
<br>    &lt;init-param&gt; <br>      &lt;param-name&gt;ignoreDuplicateServletName&lt;/param-name&gt;
<br>      &lt;param-value&gt;true&lt;/param-value&gt; <br>   
&lt;/init-param&gt; <br>  &lt;/servlet&gt; <br>{code} <br>
<br>But its *strongly advised* to use unique servlet-name for each Camel application
to avoid this duplication clash, as well any unforeseen side-effects. <br> <br>
<br></td></!
 tr>
            <tr><td class="diff-unchanged" >h3. Sample <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="SERVLET-ServletComponent"></a>Servlet Component</h2>

<p>The <b>servlet:</b> component provides HTTP based <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">endpoints</a> for consuming HTTP requests that arrive at a HTTP
endpoint that is bound to a published Servlet.</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-servlet<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">&lt;\!-\- use the same version as your Camel core version
\--&gt;</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>Stream</b><br
/>Servlet is stream based, which means the input it receives is submitted to Camel as a
stream. That means you will only be able to read the content of the stream <b>once</b>.
 If you find a situation where the message body appears to be empty or you need to access
the data multiple times (eg: doing multicasting, or redelivery error handling) you should
use <a href="/confluence/display/CAMEL/Stream+caching" title="Stream caching">Stream
caching</a> or convert the message body to a <tt>String</tt> which is safe
to be read multiple times.</td></tr></table></div>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
servlet:<span class="code-comment">//relative_path[?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="SERVLET-Options"></a>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>httpBindingRef</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Reference to an <tt>org.apache.camel.component.http.HttpBinding</tt>
in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
A <tt>HttpBinding</tt> implementation can be used to customize how to write a
response. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>matchOnUriPrefix</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> 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>servletName</tt> </td>
<td class='confluenceTd'> <tt>CamelServlet</tt> </td>
<td class='confluenceTd'> Specifies the servlet name that the servlet endpoint will
bind to. This name should match the name you define in <tt>web.xml</tt> file.
</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="SERVLET-MessageHeaders"></a>Message Headers</h3>
<p>Camel will apply the same Message Headers as the <a href="/confluence/display/CAMEL/HTTP"
title="HTTP">HTTP</a> component.</p>

<p>Camel will also populate <b>all</b> <tt>request.parameter</tt>
and <tt>request.headers</tt>. For example, if a client request has the URL, <tt><a
href="http://myserver/myserver?orderid=123" class="external-link" rel="nofollow">http://myserver/myserver?orderid=123</a></tt>,
the exchange will contain a header named <tt>orderid</tt> with the value 123.
</p>

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

<p>You can consume only from endpoints generated by the Servlet component. Therefore,
it should be used only as input into your Camel routes. 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="SERVLET-PuttingCamelJARsintheappserverbootclasspath"></a>Putting
Camel JARs in the app server boot classpath</h3>
<p>If you put the Camel JARs such as <tt>camel-core</tt>, <tt>camel-servlet</tt>,
etc. in the boot classpath of your application server (eg usually in its lib directory), then
mind that the servlet mapping list is now shared between multiple deployed Camel application
in the app server.</p>

<p>Mind that putting Camel JARs in the boot classpath of the application server is generally
not best practice!</p>

<p>So in those situations you <b>must</b> define a custom and unique servlet
name in each of your Camel application, eg in the <tt>web.xml</tt> define:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;servlet&gt;</span>
  <span class="code-tag">&lt;servlet-name&gt;</span>MySerlvet<span
class="code-tag">&lt;/servlet-name&gt;</span>
  <span class="code-tag">&lt;servlet-class&gt;</span>org.apache.camel.component.servlet.CamelHttpTransportServlet<span
class="code-tag">&lt;/servlet-class&gt;</span>
  <span class="code-tag">&lt;load-on-startup&gt;</span>1<span class="code-tag">&lt;/load-on-startup&gt;</span>
<span class="code-tag">&lt;/servlet&gt;</span>

<span class="code-tag">&lt;servlet-mapping&gt;</span>
  <span class="code-tag">&lt;servlet-name&gt;</span>MyServlet<span
class="code-tag">&lt;/servlet-name&gt;</span>
  <span class="code-tag">&lt;url-pattern&gt;</span>/*<span class="code-tag">&lt;/url-pattern&gt;</span>
<span class="code-tag">&lt;/servlet-mapping&gt;</span>
</pre>
</div></div>

<p>And in your Camel endpoints then include the servlet name as well</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">"servlet://foo?servletName=MyServlet"</span>/&gt;</span>
  ...
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>


<p>From <b>Camel 2.11</b> onwards Camel will detect this duplicate and fail
to start the application. You can control to ignore this duplicate by setting the servlet
init-parameter ignoreDuplicateServletName to true as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;servlet&gt;</span>
    <span class="code-tag">&lt;servlet-name&gt;</span>CamelServlet<span
class="code-tag">&lt;/servlet-name&gt;</span>
    <span class="code-tag">&lt;display-name&gt;</span>Camel Http Transport
Servlet<span class="code-tag">&lt;/display-name&gt;</span>
    <span class="code-tag">&lt;servlet-class&gt;</span>org.apache.camel.component.servlet.CamelHttpTransportServlet<span
class="code-tag">&lt;/servlet-class&gt;</span>
    <span class="code-tag">&lt;init-param&gt;</span>
      <span class="code-tag">&lt;param-name&gt;</span>ignoreDuplicateServletName<span
class="code-tag">&lt;/param-name&gt;</span>
      <span class="code-tag">&lt;param-value&gt;</span>true<span class="code-tag">&lt;/param-value&gt;</span>
    <span class="code-tag">&lt;/init-param&gt;</span>
  <span class="code-tag">&lt;/servlet&gt;</span>
</pre>
</div></div>

<p>But its <b>strongly advised</b> to use unique servlet-name for each Camel
application to avoid this duplication clash, as well any unforeseen side-effects.</p>


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

<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>From Camel 2.7 onwards
it's easier to use <a href="/confluence/display/CAMEL/SERVLET" title="SERVLET">Servlet</a>
in Spring web applications. See <a href="/confluence/display/CAMEL/Servlet+Tomcat+Example"
title="Servlet Tomcat Example">Servlet Tomcat Example</a> for details.</td></tr></table></div>

<p>In this sample, we define a route that exposes a HTTP service at <tt><a
href="http://localhost:8080/camel/services/hello" class="external-link" rel="nofollow">http://localhost:8080/camel/services/hello</a></tt>.<br/>
First, you need to publish the <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java"
class="external-link" rel="nofollow">CamelHttpTransportServlet</a> through the normal
Web Container, or OSGi Service.<br/>
Use the <tt>Web.xml</tt> file to publish the <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java"
class="external-link" rel="nofollow">CamelHttpTransportServlet</a> as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;web-app&gt;</span>

  <span class="code-tag">&lt;servlet&gt;</span>
    <span class="code-tag">&lt;servlet-name&gt;</span>CamelServlet<span
class="code-tag">&lt;/servlet-name&gt;</span>
    <span class="code-tag">&lt;display-name&gt;</span>Camel Http Transport
Servlet<span class="code-tag">&lt;/display-name&gt;</span>
    <span class="code-tag">&lt;servlet-class&gt;</span>org.apache.camel.component.servlet.CamelHttpTransportServlet<span
class="code-tag">&lt;/servlet-class&gt;</span>
  <span class="code-tag">&lt;/servlet&gt;</span>

  <span class="code-tag">&lt;servlet-mapping&gt;</span>
    <span class="code-tag">&lt;servlet-name&gt;</span>CamelServlet<span
class="code-tag">&lt;/servlet-name&gt;</span>
    <span class="code-tag">&lt;url-pattern&gt;</span>/services/*<span
class="code-tag">&lt;/url-pattern&gt;</span>
  <span class="code-tag">&lt;/servlet-mapping&gt;</span>

<span class="code-tag">&lt;/web-app&gt;</span>
</pre>
</div></div>

<p>Then you can define your route as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"servlet:<span class="code-comment">///hello?matchOnUriPrefix=<span
class="code-keyword">true</span>"</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 {
        <span class="code-object">String</span> contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE,
<span class="code-object">String</span>.class);
        <span class="code-object">String</span> path = exchange.getIn().getHeader(Exchange.HTTP_PATH,
<span class="code-object">String</span>.class);
        assertEquals(<span class="code-quote">"Get a wrong content type"</span>,
CONTENT_TYPE, contentType);
        <span class="code-comment">// <span class="code-keyword">assert</span>
camel http header
</span>        <span class="code-object">String</span> charsetEncoding =
exchange.getIn().getHeader(Exchange.HTTP_CHARACTER_ENCODING, <span class="code-object">String</span>.class);
        assertEquals(<span class="code-quote">"Get a wrong charset name from the message
heaer"</span>, <span class="code-quote">"UTF-8"</span>, charsetEncoding);
        <span class="code-comment">// <span class="code-keyword">assert</span>
exchange charset
</span>        assertEquals(<span class="code-quote">"Get a wrong charset naem
from the exchange property"</span>, <span class="code-quote">"UTF-8"</span>,
exchange.getProperty(Exchange.CHARSET_NAME));
        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, contentType + <span class="code-quote">";
charset=UTF-8"</span>);
        exchange.getOut().setHeader(<span class="code-quote">"PATH"</span>, path);
        exchange.getOut().setBody(<span class="code-quote">"&lt;b&gt;Hello World&lt;/b&gt;"</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>Specify the relative
path for camel-servlet endpoint</b><br />Since we are binding the Http transport
with a published servlet, and we don't know the servlet's application context path, the <tt>camel-servlet</tt>
endpoint uses the relative path to specify the endpoint's URL. A client can access the <tt>camel-servlet</tt>
endpoint through the servlet publish address: <tt>("http://localhost:8080/camel/services")
+ RELATIVE_PATH("/hello")</tt>.</td></tr></table></div>

<h4><a name="SERVLET-SamplewhenusingSpring3.x"></a>Sample when using Spring
3.x</h4>

<p>See <a href="/confluence/display/CAMEL/Servlet+Tomcat+Example" title="Servlet
Tomcat Example">Servlet Tomcat Example</a></p>

<h4><a name="SERVLET-SamplewhenusingSpring2.x"></a>Sample when using Spring
2.x</h4>

<p>When using the Servlet component in a Camel/Spring application it's often required
to load the Spring ApplicationContext <em>after</em> the Servlet component has
started. This can be accomplished by using Spring's <tt>ContextLoaderServlet</tt>
instead of <tt>ContextLoaderListener</tt>. In that case you'll need to start <tt>ContextLoaderServlet</tt>
after <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java"
class="external-link" rel="nofollow">CamelHttpTransportServlet</a> like this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"> 
<span class="code-tag">&lt;web-app&gt;</span>
  <span class="code-tag">&lt;servlet&gt;</span> 
        <span class="code-tag">&lt;servlet-name&gt;</span>CamelServlet<span
class="code-tag">&lt;/servlet-name&gt;</span> 
        <span class="code-tag">&lt;servlet-class&gt;</span> 
            org.apache.camel.component.servlet.CamelHttpTransportServlet 
        <span class="code-tag">&lt;/servlet-class&gt;</span> 
        <span class="code-tag">&lt;load-on-startup&gt;</span>1<span
class="code-tag">&lt;/load-on-startup&gt;</span> 
  <span class="code-tag">&lt;/servlet&gt;</span> 
  <span class="code-tag">&lt;servlet&gt;</span> 
        <span class="code-tag">&lt;servlet-name&gt;</span>SpringApplicationContext<span
class="code-tag">&lt;/servlet-name&gt;</span> 
        <span class="code-tag">&lt;servlet-class&gt;</span> 
            org.springframework.web.context.ContextLoaderServlet 
        <span class="code-tag">&lt;/servlet-class&gt;</span> 
        <span class="code-tag">&lt;load-on-startup&gt;</span>2<span
class="code-tag">&lt;/load-on-startup&gt;</span> 
  <span class="code-tag">&lt;/servlet&gt;</span> 
<span class="code-tag">&lt;web-app&gt;</span>
</pre>
</div></div>

<h4><a name="SERVLET-SamplewhenusingOSGi"></a>Sample when using OSGi</h4>

<p>From <b>Camel 2.6.0</b>, you can publish the <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java"
class="external-link" rel="nofollow">CamelHttpTransportServlet</a> as an OSGi service
with help of SpringDM like this.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="code-keyword">xmlns:osgi</span>=<span class="code-quote">"http://www.springframework.org/schema/osgi"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/osgi  http://www.springframework.org/schema/osgi/spring-osgi.xsd"&gt;
    
    <span class="code-tag">&lt;bean id=<span class="code-quote">"camelServlet"</span>
class=<span class="code-quote">"org.apache.camel.component.servlet.CamelHttpTransportServlet"</span>&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>
    
    &lt;!-- 
    	Enlist it in OSGi service registry 
    	This will cause two things:
    	1) As the pax web whiteboard extender is running the CamelServlet will
    	   be registered with the OSGi HTTP Service
    	2) It will trigger the HttpRegistry in other bundles so the servlet is
    	   made known there too
    --&gt;
    <span class="code-tag">&lt;osgi:service ref=<span class="code-quote">"camelServlet"</span>&gt;</span>
    	<span class="code-tag">&lt;osgi:interfaces&gt;</span>
    		<span class="code-tag">&lt;value&gt;</span>javax.servlet.Servlet<span
class="code-tag">&lt;/value&gt;</span>
    		<span class="code-tag">&lt;value&gt;</span>org.apache.camel.component.http.CamelServlet<span
class="code-tag">&lt;/value&gt;</span>
    	<span class="code-tag">&lt;/osgi:interfaces&gt;</span>
        <span class="code-tag">&lt;osgi:service-properties&gt;</span>
            <span class="code-tag">&lt;entry key=<span class="code-quote">"alias"</span>
value=<span class="code-quote">"/camel/services"</span> /&gt;</span>
            <span class="code-tag">&lt;entry key=<span class="code-quote">"matchOnUriPrefix"</span>
value=<span class="code-quote">"true"</span> /&gt;</span>
            <span class="code-tag">&lt;entry key=<span class="code-quote">"servlet-name"</span>
value=<span class="code-quote">"CamelServlet"</span>/&gt;</span>
        <span class="code-tag">&lt;/osgi:service-properties&gt;</span>
    <span class="code-tag">&lt;/osgi:service&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>Then use this service in your camel route like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="code-keyword">xmlns:camel</span>=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
       <span class="code-keyword">xmlns:osgi</span>=<span class="code-quote">"http://www.springframework.org/schema/osgi"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/osgi  http://www.springframework.org/schema/osgi/spring-osgi.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"&gt;

	<span class="code-tag">&lt;osgi:reference id=<span class="code-quote">"servletref"</span>
interface=<span class="code-quote">"org.apache.camel.component.http.CamelServlet"</span>&gt;</span>
  		<span class="code-tag">&lt;osgi:listener bind-method=<span class="code-quote">"register"</span>
unbind-method=<span class="code-quote">"unregister"</span>&gt;</span>
     		<span class="code-tag">&lt;ref bean=<span class="code-quote">"httpRegistry"</span>/&gt;</span>
  		<span class="code-tag">&lt;/osgi:listener&gt;</span>
	<span class="code-tag">&lt;/osgi:reference&gt;</span>
	
	<span class="code-tag">&lt;bean id=<span class="code-quote">"httpRegistry"</span>
class=<span class="code-quote">"org.apache.camel.component.servlet.DefaultHttpRegistry"</span>/&gt;</span>
        
	<span class="code-tag">&lt;bean id=<span class="code-quote">"servlet"</span>
class=<span class="code-quote">"org.apache.camel.component.servlet.ServletComponent"</span>&gt;</span>
    	<span class="code-tag">&lt;property name=<span class="code-quote">"httpRegistry"</span>
ref=<span class="code-quote">"httpRegistry"</span> /&gt;</span>
	<span class="code-tag">&lt;/bean&gt;</span>

	<span class="code-tag">&lt;bean id=<span class="code-quote">"servletProcessor"</span>
class=<span class="code-quote">"org.apache.camel.itest.osgi.servlet.ServletProcessor"</span>
/&gt;</span>

    <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!-- notice
how we can use the servlet scheme which is that osgi:reference above --&gt;</span></span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"servlet:///hello"</span>/&gt;</span>
            <span class="code-tag">&lt;process ref=<span class="code-quote">"servletProcessor"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
    <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<p>For versions prior to Camel 2.6 you can use an <tt>Activator</tt> to
publish the <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java"
class="external-link" rel="nofollow">CamelHttpTransportServlet</a> on the OSGi platform
</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">import</span> java.util.Dictionary;
<span class="code-keyword">import</span> java.util.Hashtable;

<span class="code-keyword">import</span> org.apache.camel.component.servlet.CamelHttpTransportServlet;
<span class="code-keyword">import</span> org.osgi.framework.BundleActivator;
<span class="code-keyword">import</span> org.osgi.framework.BundleContext;
<span class="code-keyword">import</span> org.osgi.framework.ServiceReference;
<span class="code-keyword">import</span> org.osgi.service.http.HttpContext;
<span class="code-keyword">import</span> org.osgi.service.http.HttpService;
<span class="code-keyword">import</span> org.slf4j.Logger;
<span class="code-keyword">import</span> org.slf4j.LoggerFactory;
<span class="code-keyword">import</span> org.springframework.osgi.context.BundleContextAware;

<span class="code-keyword">public</span> <span class="code-keyword">final</span>
class ServletActivator <span class="code-keyword">implements</span> BundleActivator,
BundleContextAware {
    <span class="code-keyword">private</span> <span class="code-keyword">static</span>
<span class="code-keyword">final</span> <span class="code-keyword">transient</span>
Logger LOG = LoggerFactory.getLogger(ServletActivator.class);
    <span class="code-keyword">private</span> <span class="code-keyword">static</span>
<span class="code-object">boolean</span> registerService;
    
    /**
     * HttpService reference.
     */
    <span class="code-keyword">private</span> ServiceReference httpServiceRef;
    
    /**
     * Called when the OSGi framework starts our bundle
     */
    <span class="code-keyword">public</span> void start(BundleContext bc) <span
class="code-keyword">throws</span> Exception {
        registerServlet(bc);
    }

    /**
     * Called when the OSGi framework stops our bundle
     */
    <span class="code-keyword">public</span> void stop(BundleContext bc) <span
class="code-keyword">throws</span> Exception {
        <span class="code-keyword">if</span> (httpServiceRef != <span class="code-keyword">null</span>)
{
            bc.ungetService(httpServiceRef);
            httpServiceRef = <span class="code-keyword">null</span>;
        }
    }
    
    <span class="code-keyword">protected</span> void registerServlet(BundleContext
bundleContext) <span class="code-keyword">throws</span> Exception {
        httpServiceRef = bundleContext.getServiceReference(HttpService.class.getName());
        
        <span class="code-keyword">if</span> (httpServiceRef != <span class="code-keyword">null</span>
&amp;&amp; !registerService) {
            LOG.info(<span class="code-quote">"Register the servlet service"</span>);
            <span class="code-keyword">final</span> HttpService httpService =
(HttpService)bundleContext.getService(httpServiceRef);
            <span class="code-keyword">if</span> (httpService != <span class="code-keyword">null</span>)
{
                <span class="code-comment">// create a <span class="code-keyword">default</span>
context to share between registrations
</span>                <span class="code-keyword">final</span> HttpContext
httpContext = httpService.createDefaultHttpContext();
                <span class="code-comment">// register the hello world servlet
</span>                <span class="code-keyword">final</span> Dictionary&lt;<span
class="code-object">String</span>, <span class="code-object">String</span>&gt;
initParams = <span class="code-keyword">new</span> Hashtable&lt;<span class="code-object">String</span>,
<span class="code-object">String</span>&gt;();
                initParams.put(<span class="code-quote">"matchOnUriPrefix"</span>,
<span class="code-quote">"<span class="code-keyword">false</span>"</span>);
                initParams.put(<span class="code-quote">"servlet-name"</span>,
<span class="code-quote">"CamelServlet"</span>);
                httpService.registerServlet(<span class="code-quote">"/camel/services"</span>,
<span class="code-comment">// alias
</span>                    <span class="code-keyword">new</span> CamelHttpTransportServlet(),
<span class="code-comment">// register servlet
</span>                    initParams, <span class="code-comment">// init params
</span>                    httpContext <span class="code-comment">// http context
</span>                );
                registerService = <span class="code-keyword">true</span>;
            }
        }
    }

    <span class="code-keyword">public</span> void setBundleContext(BundleContext
bc) {
        <span class="code-keyword">try</span> {
            registerServlet(bc);
        } <span class="code-keyword">catch</span> (Exception e) {
            LOG.error(<span class="code-quote">"Cannot register the servlet, the reason
is "</span> + e);
        }
    }

}
</pre>
</div></div>

<h3><a name="SERVLET-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/Servlet+Tomcat+Example" title="Servlet Tomcat
Example">Servlet Tomcat Example</a></li>
	<li><a href="/confluence/display/CAMEL/Servlet+Tomcat+No+Spring+Example" title="Servlet
Tomcat No Spring Example">Servlet Tomcat No Spring Example</a></li>
	<li><a href="/confluence/display/CAMEL/HTTP" title="HTTP">HTTP</a></li>
	<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/SERVLET">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=2327054&revisedVersion=20&originalVersion=19">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/SERVLET?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message