camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Websocket
Date Thu, 24 May 2012 09:47: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/Websocket">Websocket</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~cmoulliard">charles
Moulliard</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" >This example is part of an unit test,
which you can find [here|https://svn.apache.org/repos/asf/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketRouteExampleTest.java].
As a client we use the [AHC] library which offers support for web socket as well.  <br>
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Setting up SSL for WebSocket Component <br> <br>h4. Using the JSSE Configuration
Utility <br> <br>As of Camel 2.10, the WebSocket component supports SSL/TLS configuration
through the [Camel JSSE Configuration Utility|CAMEL:Camel Configuration Utilities].&amp;nbsp;
This utility greatly decreases the amount of component specific code you need to write and
is configurable at the endpoint and component levels.&amp;nbsp; The following examples
demonstrate how to use the utility with the Cometd component. <br> <br>h5. Programmatic
configuration of the component <br> <br>{code} <br>KeyStoreParameters ksp
= new KeyStoreParameters(); <br>ksp.setResource(&quot;/users/home/server/keystore.jks&quot;);
<br>ksp.setPassword(&quot;keystorePassword&quot;); <br> <br>KeyManagersParameters
kmp = new KeyManagersParameters(); <br>kmp.setKeyStore(ksp); <br>kmp.setKeyPassword(&quot;keyPassword&quot;);
<br> <br>TrustManagersParameters tmp = new TrustManagersParameters(); <br>tmp.setKeyStore(ksp);
<br> <br>SSLContextParameters scp = new SSLContextParameters(); <br>scp.setKeyManagers(kmp);
<br>scp.setTrustManagers(tmp); <br> <br>CometdComponent commetdComponent
= getContext().getComponent(&quot;cometds&quot;, CometdComponent.class); <br>commetdComponent.setSslContextParameters(scp);
<br>{code} <br> <br>h5. Spring DSL based configuration of endpoint <br>
<br>{code:xml} <br>... <br>  &lt;camel:sslContextParameters <br>
     id=&quot;sslContextParameters&quot;&gt; <br>    &lt;camel:keyManagers
<br>        keyPassword=&quot;keyPassword&quot;&gt; <br>      &lt;camel:keyStore
<br>          resource=&quot;/users/home/server/keystore.jks&quot; <br>
         password=&quot;keystorePassword&quot;/&gt; <br>    &lt;/camel:keyManagers&gt;
<br>    &lt;camel:trustManagers&gt; <br>      &lt;camel:keyStore <br>
         resource=&quot;/users/home/server/keystore.jks&quot; <br>         
password=&quot;keystorePassword&quot;/&gt; <br>    &lt;/camel:keyManagers&gt;
<br>  &lt;/camel:sslContextParameters&gt;... <br>... <br>  &lt;to
uri=&quot;websocket://127.0.0.1:8443/test?sslContextParameters=#sslContextParameters&quot;/&gt;...
<br>{code} <br> <br>h5. Java DSL based configuration of endpoint <br>
<br>{code:java} <br>... <br>    protected RouteBuilder createRouteBuilder()
throws Exception { <br>        return new RouteBuilder() { <br>            public
void configure() { <br> <br>                String uri = &quot;websocket://127.0.0.1:8443/test?sslContextParametersRef=#sslContextParameters&quot;;
<br> <br>                from(uri) <br>                     .log(&quot;&gt;&gt;&gt;
Message received from WebSocket Client : ${body}&quot;) <br>                   
 .to(&quot;mock:client&quot;) <br>                     .loop(10) <br>
                        .setBody().constant(&quot;&gt;&gt; Welcome on board!&quot;)
<br>                         .to(uri); <br>... <br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br>-
[AHC] <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Websocket-WebsocketComponent"></a>Websocket Component</h2>
<p><b>Available as of Camel 2.10</b></p>

<p>The <b>websocket</b> component provides websocket <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">endpoints</a> for communicating with clients using websocket. The
component uses Eclipse Jetty Server which implements the <a href="http://tools.ietf.org/html/rfc6455"
class="external-link" rel="nofollow">IETF</a> specification (drafts and RFC 6455).
It supports the protocols ws:// and wss://. To use wss:// protocol, the SSLContextParameters
must be defined.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Version currently
supported</b><br />As Camel 2.10 uses Jetty 7.5.4.v20111024, only the D00 to <a
href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-13" class="external-link"
rel="nofollow">D13</a> IETF implementations are available.</td></tr></table></div>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
websocket:<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="Websocket-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'> sslContextParameters </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Reference to a <tt>org.apache.camel.util.jsse.SSLContextParameters</tt>
in the <a href="http://camel.apache.org/registry.html" class="external-link" rel="nofollow">Registry</a>.&nbsp;
This reference overrides any configured SSLContextParameters at the component level.&nbsp;
See <a href="/confluence/display/CAMEL/Camel+Configuration+Utilities" title="Camel Configuration
Utilities">Using the JSSE Configuration Utility</a>. <br class="atl-forced-newline"
/> </td>
</tr>
</tbody></table>
</div>
</div>

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

<p>Here is some examples on How to pass the parameters</p>

<p>To use ssl and websocket secure &#45;-&gt; websocket://localhost:8443?/test?sslContextParametersRef=#sslContextParameters</p>

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

<p>The websocket component uses 2 headers to indicate to either send messages back to
a single/current client, or to all clients.</p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Key</th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>WebsocketConstants.SEND_TO_ALL</tt> </td>
<td class='confluenceTd'> Sends the message to all clients which are currently connected.
You can use the <tt>sendToAll</tt> option on the endpoint instead of using this
header. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>WebsocketConstants.CONNECTION_KEY</tt> </td>
<td class='confluenceTd'> Sends the message to the client with the given connection
key. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Websocket-ComponentOptions"></a>Component Options</h3>

<p>The <tt>WebsocketComponent</tt> must be configured prior to use, to setup
host, to act as a websocket server. </p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> Host </td>
<td class='confluenceTd'> 0.0.0.0 </td>
<td class='confluenceTd'> The hostname. </td>
</tr>
<tr>
<td class='confluenceTd'> Port </td>
<td class='confluenceTd'> 9292 </td>
<td class='confluenceTd'> The port number. </td>
</tr>
<tr>
<td class='confluenceTd'> StaticResources </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Path for static resources such as index.html files etc. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Websocket-EndpointOptions"></a>Endpoint Options</h3>

<p>The <tt>WebsocketEndpoint</tt> can be configured prior to use</p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> sendToAll </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> To send to all websocket subscribers. Can be used to configure
on endpoint level, instead of having to use the <tt>WebsocketConstants.SEND_TO_ALL</tt>
header on the message. </td>
</tr>
</tbody></table>
</div>
</div>

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

<p>In this example we let Camel exposes a websocket server which clients can communicate
with. The websocket server uses the default host and port, which would be <tt>0.0.0.0:9292</tt>.<br/>
The example will send back an echo of the input. To send back a message, we need to send the
transformed message to the same endpoint <tt>"websocket://echo"</tt>. This is
needed<br/>
because by default the messaging is InOnly. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// expose a echo websocket client,
that sends back an echo
</span>from(<span class="code-quote">"websocket:<span class="code-comment">//echo"</span>)
</span>    .log(<span class="code-quote">"&gt;&gt;&gt; Message received
from WebSocket Client : ${body}"</span>)
    .transform().simple(<span class="code-quote">"${body}${body}"</span>)
    <span class="code-comment">// send back to the client, by sending the message to
the same endpoint
</span>    <span class="code-comment">// <span class="code-keyword">this</span>
is needed as by <span class="code-keyword">default</span> messages is InOnly
</span>    <span class="code-comment">// and we will by <span class="code-keyword">default</span>
send back to the current client using the provided connection key
</span>    .to(<span class="code-quote">"websocket:<span class="code-comment">//echo"</span>);</span>
</pre>
</div></div>

<p>This example is part of an unit test, which you can find <a href="https://svn.apache.org/repos/asf/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketRouteExampleTest.java"
class="external-link" rel="nofollow">here</a>. As a client we use the <a href="/confluence/display/CAMEL/AHC"
title="AHC">AHC</a> library which offers support for web socket as well. </p>

<h3><a name="Websocket-SettingupSSLforWebSocketComponent"></a>Setting up
SSL for WebSocket Component</h3>

<h4><a name="Websocket-UsingtheJSSEConfigurationUtility"></a>Using the JSSE
Configuration Utility</h4>

<p>As of Camel 2.10, the WebSocket component supports SSL/TLS configuration through
the <a href="/confluence/display/CAMEL/Camel+Configuration+Utilities" title="Camel Configuration
Utilities">Camel JSSE Configuration Utility</a>.&nbsp; This utility greatly decreases
the amount of component specific code you need to write and is configurable at the endpoint
and component levels.&nbsp; The following examples demonstrate how to use the utility
with the Cometd component.</p>

<h5><a name="Websocket-Programmaticconfigurationofthecomponent"></a>Programmatic
configuration of the component</h5>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
KeyStoreParameters ksp = <span class="code-keyword">new</span> KeyStoreParameters();
ksp.setResource(<span class="code-quote">"/users/home/server/keystore.jks"</span>);
ksp.setPassword(<span class="code-quote">"keystorePassword"</span>);

KeyManagersParameters kmp = <span class="code-keyword">new</span> KeyManagersParameters();
kmp.setKeyStore(ksp);
kmp.setKeyPassword(<span class="code-quote">"keyPassword"</span>);

TrustManagersParameters tmp = <span class="code-keyword">new</span> TrustManagersParameters();
tmp.setKeyStore(ksp);

SSLContextParameters scp = <span class="code-keyword">new</span> SSLContextParameters();
scp.setKeyManagers(kmp);
scp.setTrustManagers(tmp);

CometdComponent commetdComponent = getContext().getComponent(<span class="code-quote">"cometds"</span>,
CometdComponent.class);
commetdComponent.setSslContextParameters(scp);
</pre>
</div></div>

<h5><a name="Websocket-SpringDSLbasedconfigurationofendpoint"></a>Spring
DSL based configuration of endpoint</h5>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
...
  &lt;camel:sslContextParameters
      id=<span class="code-quote">"sslContextParameters"</span>&gt;
    &lt;camel:keyManagers
        keyPassword=<span class="code-quote">"keyPassword"</span>&gt;
      &lt;camel:keyStore
          resource=<span class="code-quote">"/users/home/server/keystore.jks"</span>
          password=<span class="code-quote">"keystorePassword"</span>/&gt;
    <span class="code-tag">&lt;/camel:keyManagers&gt;</span>
    <span class="code-tag">&lt;camel:trustManagers&gt;</span>
      &lt;camel:keyStore
          resource=<span class="code-quote">"/users/home/server/keystore.jks"</span>
          password=<span class="code-quote">"keystorePassword"</span>/&gt;
    <span class="code-tag">&lt;/camel:keyManagers&gt;</span>
  <span class="code-tag">&lt;/camel:sslContextParameters&gt;</span>...
...
  <span class="code-tag">&lt;to uri=<span class="code-quote">"websocket://127.0.0.1:8443/test?sslContextParameters=#sslContextParameters"</span>/&gt;</span>...
</pre>
</div></div>

<h5><a name="Websocket-JavaDSLbasedconfigurationofendpoint"></a>Java DSL
based configuration of endpoint</h5>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
...
    <span class="code-keyword">protected</span> RouteBuilder createRouteBuilder()
<span class="code-keyword">throws</span> Exception {
        <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
            <span class="code-keyword">public</span> void configure() {
                
                <span class="code-object">String</span> uri = <span class="code-quote">"websocket:<span
class="code-comment">//127.0.0.1:8443/test?sslContextParametersRef=#sslContextParameters"</span>;
</span>                
                from(uri)
                     .log(<span class="code-quote">"&gt;&gt;&gt; Message
received from WebSocket Client : ${body}"</span>)
                     .to(<span class="code-quote">"mock:client"</span>)
                     .loop(10)
                         .setBody().constant(<span class="code-quote">"&gt;&gt;
Welcome on board!"</span>)
                         .to(uri);
...
</pre>
</div></div>

<h3><a name="Websocket-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/AHC" title="AHC">AHC</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/Websocket">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=27841331&revisedVersion=7&originalVersion=6">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Websocket?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message