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 Fri, 18 Feb 2011 06:58: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/SERVLET">SERVLET</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~rkettelerij">Richard
Kettelerij</a>
    </h4>
        <br/>
                         <h4>Changes (6)</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" >You can only consume from endpoints
generated by the Servlet component. Therefore, it should only be used as input into your camel
routes. To issue HTTP requests against other HTTP endpoints, use the [HTTP Component|HTTP]
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{note:title=Specify
the relative path for camel-servlet endpoint} <br>Since we are binding the Http transport
with a published servlet, and we don&#39;t know the servlet&#39;s application context
path, the {{camel-servlet}} endpoint uses the relative path to specify the endpoint&#39;s
URL. A client can access the {{camel-servlet}} endpoint through the servlet publish address:
{{(&quot;http://localhost:8080/camel/services&quot;) + RELATIVE_PATH(&quot;/hello&quot;)}}.{note}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Sample <br>In this sample,
we define a route that exposes a HTTP service at {{http://localhost:8080/camel/services/hello}}.
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=web|lang=xml|url=camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml}
<br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">Use
an {{Activator}} to publish the [CamelHttpTransportServlet|http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java]
on the OSGi platform  <br>{snippet:id=activator|lang=java|url=camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/servlet/support/ServletActivator.java}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >Then you can define your route as
follows: <br>{snippet:id=route|lang=java|url=camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java}
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Sample when using Spring <br> <br>When using the Servlet component in a Camel/Spring
application it&#39;s often required to load the Spring ApplicationContext _after_ the
Servlet component has started. This can be accomplished by using Spring&#39;s {{ContextLoaderServlet}}
instead of {{ContextLoaderListener}}. In that case you&#39;ll need to start ContextLoaderServlet
after CamelHttpTransportServlet like this: <br> <br>{code}  <br>&lt;web-app&gt;
<br>&lt;servlet&gt;  <br>        &lt;servlet-name&gt;CamelServlet&lt;/servlet-name&gt;
 <br>        &lt;servlet-class&gt;  <br>            org.apache.camel.component.servlet.CamelHttpTransportServlet
 <br>        &lt;/servlet-class&gt;  <br>        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
 <br> &lt;/servlet&gt;  <br>&lt;servlet&gt;  <br>      
 &lt;servlet-name&gt;context&lt;/servlet-name&gt;  <br>        &lt;servlet-class&gt;
 <br>          org.springframework.web.context.ContextLoaderServlet  <br>    
   &lt;/servlet-class&gt;  <br>        &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
 <br>&lt;/servlet&gt;  <br>&lt;web-app&gt; <br>{code} <br>
<br>h4. Sample when using OSGi <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >From *Camel 2.6.0*, you can <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">also</span>
publish the [CamelHttpTransportServlet|http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java]
as an OSGi service with help of SpringDM like this. <br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=service|lang=xml|url=camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/servlet/ServletServiceContext.xml}
<br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=camelContext|lang=xml|url=camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/servlet/CamelServletWithServletServiceContext.xml}
<br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{note:title=Specify
the relative path for camel-servlet endpoint} <br>Since we are binding the Http transport
with a published servlet, and we don&#39;t know the servlet&#39;s application context
path, the {{camel-servlet}} endpoint uses the relative path to specify the endpoint&#39;s
URL. A client can access the {{camel-servlet}} endpoint through the servlet publish address:
{{(&quot;http://localhost:8080/camel/services&quot;) + RELATIVE_PATH(&quot;/hello&quot;)}}.
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Alternatively
you can use an {{Activator}} to publish the [CamelHttpTransportServlet|http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java]
on the OSGi platform  <br>{snippet:id=activator|lang=java|url=camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/servlet/support/ServletActivator.java}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br>-
[Http] <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="SERVLET-ServletComponent"></a>Servlet Component</h2>
<p><b>Available as of Camel 2.0</b></p>

<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 and this endpoint 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>

<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>null</tt> </td>
<td class='confluenceTd'> Specifies the servlet name that the servlet endpoint will
bind to. If there is no servlet name specified, the servlet endpoint will be bind to first
published Servlet </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 only consume from endpoints generated by the Servlet component. Therefore,
it should only be used 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>

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

<h3><a name="SERVLET-Sample"></a>Sample</h3>
<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>

<h4><a name="SERVLET-SamplewhenusingSpring"></a>Sample when using Spring</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 ContextLoaderServlet
after CamelHttpTransportServlet like this:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"> 
&lt;web-app&gt;
&lt;servlet&gt; 
        &lt;servlet-name&gt;CamelServlet&lt;/servlet-name&gt; 
        &lt;servlet-class&gt; 
            org.apache.camel.component.servlet.CamelHttpTransportServlet 
        &lt;/servlet-class&gt; 
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; 
 &lt;/servlet&gt; 
&lt;servlet&gt; 
        &lt;servlet-name&gt;context&lt;/servlet-name&gt; 
        &lt;servlet-class&gt; 
          org.springframework.web.context.ContextLoaderServlet 
        &lt;/servlet-class&gt; 
        &lt;load-on-startup&gt;2&lt;/load-on-startup&gt; 
&lt;/servlet&gt; 
&lt;web-app&gt;
</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">"osgiServlet"</span>
class=<span class="code-quote">"org.apache.camel.component.servlet.CamelHttpTransportServlet"</span>/&gt;</span>
     
    <span class="code-tag">&lt;osgi:service ref=<span class="code-quote">"osgiServlet"</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.servlet.CamelServletService<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">"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">"camelHttpTransportServlet"</span>
interface=<span class="code-quote">"org.apache.camel.component.servlet.CamelServletService"</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">"camelServletService"</span>
ref=<span class="code-quote">"camelHttpTransportServlet"</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;camel:route&gt;</span>
       <span class="code-tag">&lt;camel:from uri=<span class="code-quote">"servlet:///hello"</span>/&gt;</span>
       <span class="code-tag">&lt;camel:process ref=<span class="code-quote">"servletProcessor"</span>/&gt;</span>
     <span class="code-tag">&lt;/camel:route&gt;</span>
   <span class="code-tag">&lt;/camelContext&gt;</span>

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

<p>Alternatively 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> javax.servlet.Servlet;

<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">"Regist 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>                    (Servlet)<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">"Can't 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/HTTP" title="HTTP">HTTP</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=7&originalVersion=6">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