cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache CXF Documentation > JavaScript
Date Wed, 16 Jan 2013 14:27:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CXF20DOC&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/CXF20DOC/JavaScript">JavaScript</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~coheigea@apache.org">Colm
O hEigeartaigh</a>
    </h4>
        <br/>
                         <h4>Changes (3)</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" >JavaScript, also known by its formal
name ECMAScript, is one of the many dynamic languages that are growing in prevalence in development
environments. It provides a quick and lightweight means of creating functionality that can
be run on a number of platforms. Another strength of JavaScript is that applications can be
quickly rewritten. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >CXF provides support for developing
services using JavaScript and ECMAScript for XML(E4X). The <span class="diff-changed-words">pattern<span
class="diff-added-chars"style="background-color: #dfd;">s</span></span> used
to develop these services are similar to JAX-WS {{Provider}} implementations that handle their
requests and responses (either SOAP messages or SOAP payloads) as DOM documents. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h2. Implementing a Service
in JavaScript <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >* The service defined by {{goodbye_moon.js}}
at {{http://cxf.apache.org/blue}}. <br>* The service defined by {{chocolate.jsx}} at
{{http://cxf.apache.org/goodness}}. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
<br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{anchor:ex6}{panel:title=<span
class="diff-added-chars"style="background-color: #dfd;">Example 6:</span>Combining</span>
the Command Line Arguments} <br></td></tr>
            <tr><td class="diff-unchanged" >{{java org.apache.cxf.js.rhino.ServerApp
-b http://cxf.apache.org hello_world.jsx goodbye_moon.js -a http://cxf.apache.org/goodness
chocolate.jsx}} <br>{panel} <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h1><a name="JavaScript-UsingDynamicLanguagestoImplementServices"></a>Using
Dynamic Languages to Implement Services</h1>


<h2><a name="JavaScript-Overview"></a>Overview</h2>

<p>JavaScript, also known by its formal name ECMAScript, is one of the many dynamic
languages that are growing in prevalence in development environments. It provides a quick
and lightweight means of creating functionality that can be run on a number of platforms.
Another strength of JavaScript is that applications can be quickly rewritten.</p>

<p>CXF provides support for developing services using JavaScript and ECMAScript for
XML(E4X). The patterns used to develop these services are similar to JAX-WS <tt>Provider</tt>
implementations that handle their requests and responses (either SOAP messages or SOAP payloads)
as DOM documents.</p>

<h2><a name="JavaScript-ImplementingaServiceinJavaScript"></a>Implementing
a Service in JavaScript</h2>

<p>Writing a service in JavaScript is a two step process:</p>
<ol>
	<li>Define the JAX-WS style metadata.</li>
	<li>Implement the services business logic.</li>
</ol>


<h3><a name="JavaScript-DefiningtheMetadata"></a>Defining the Metadata</h3>

<p>Normal Java providers typically use Java annotations to specify JAX-WS metadata.
Since JavaScript does not support annotations, you use ordinary JavaScript variables to specify
metadata for JavaScript implementations. CXF treats any Javascript variable in your code whose
name equals or begins with <tt>WebServiceProvider</tt> as a JAX-WS metadata variable.</p>

<p>Properties of the variable are expected to specify the same metadata that the JAX-WS
<tt>WebServiceProvider</tt> annotation specifies, including:</p>

<ul>
	<li><tt>wsdlLocation</tt> specifies a URL for the WSDL defining the service.</li>
	<li><tt>serviceName</tt> specifies the name of the service.</li>
	<li><tt>portName</tt> specifies the service's port/interface name.</li>
	<li><tt>targetNamespace</tt> specifies the target namespace of the service.</li>
</ul>


<p>The Javascript <tt>WebServiceProvider</tt> can also specify the following
optional properties:</p>
<ul>
	<li><tt>ServiceMode</tt> indicates whether the specified service handles
SOAP payload documents or full SOAP message documents. This property mimics the JAX-WS <tt>ServiceMode</tt>
annotation. The default value is <tt>PAYLOAD</tt>.</li>
	<li><tt>BindingMode</tt> indicates the service binding ID URL. The default
is the SOAP 1.1/HTTP binding.</li>
	<li><tt>EndpointAddress</tt> indicates the URL consumer applications use
to communicate with this service. The property is optional but has no default.</li>
</ul>


<p><a href="#JavaScript-ex1">Example 1</a> shows a metadata description
for a JavaScript service implementation.</p>

<p><a name="JavaScript-ex1"></a></p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Example 1:JavaScript Metadata</b></div><div
class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">var</span> WebServiceProvider1 = {
    'wsdlLocation': 'file:./wsdl/hello_world.wsdl',
    'serviceName': 'SOAPService1',
    'portName': 'SoapPort1',
    'targetNamespace': 'http:<span class="code-comment">//apache.org/hello_world_soap_http',
</span>};
</pre>
</div></div>

<h3><a name="JavaScript-ImplementingtheServiceLogic"></a>Implementing the
Service Logic</h3>

<p>You implement the service's logic using the required <tt>invoke</tt>
property of the <tt>WebServiceProvider</tt> variable. This variable is a function
that accepts one input argument, a <tt>javax.xml.transform.dom.DOMSource</tt>
node, and returns a document of the same type. The <tt>invoke</tt> function can
manipulate either the input or output documents using the regular Java <tt>DOMSource</tt>
class interface just as a Java application would.</p>

<p><a href="#JavaScript-ex2">Example 2</a> shows an <tt>invoke</tt>
property for a simple JavaScript service implementation.</p>

<p><a name="JavaScript-ex2"></a></p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Example 2:JavaScript Service Implementation</b></div><div
class="codeContent panelContent">
<pre class="code-java">
WebServiceProvider.invoke = function(document) {
    <span class="code-keyword">var</span> ns4 = <span class="code-quote">"http:<span
class="code-comment">//apache.org/hello_world_soap_http/types"</span>;
</span>    <span class="code-keyword">var</span> list = document.getElementsByTagNameNS(ns4,
<span class="code-quote">"requestType"</span>);
    <span class="code-keyword">var</span> name = list.item(0).getFirstChild().getNodeValue();
    <span class="code-keyword">var</span> newDoc = document.getImplementation().createDocument(ns4,
<span class="code-quote">"ns4:greetMeResponse"</span>, <span class="code-keyword">null</span>);
    <span class="code-keyword">var</span> el = newDoc.createElementNS(ns4, <span
class="code-quote">"ns4:responseType"</span>);
    <span class="code-keyword">var</span> txt = newDoc.createTextNode(<span
class="code-quote">"Hi "</span> + name);
    el.insertBefore(txt, <span class="code-keyword">null</span>);
    newDoc.getDocumentElement().insertBefore(el, <span class="code-keyword">null</span>);
    <span class="code-keyword">return</span> newDoc;
}
</pre>
</div></div>
<h2><a name="JavaScript-ImplementingaServiceinECMAScriptforXML%28E4X%29"></a>Implementing
a Service in ECMAScript for XML(E4X)</h2>

<p>Writing a CXF service using E4X is very similar to writing a service using JavaScript.
You define the JAX-WS metadata using the same <tt>WebServiceProvider</tt> variable
in JavaScript. You also implement the service's logic in the <tt>WebServiceProvider</tt>
variable's <tt>invoke</tt> property.</p>

<p>The only difference between the two approaches is the type of document the implementation
manipulates. When working with E4X, the implementation receives requests as an E4X XML document
and returns a document of the same type. These documents are manipulated using built-in E4X
XML features.</p>

<p><a href="#JavaScript-ex3">Example 3</a> shows an <tt>invoke</tt>
property for a simple E4X service implementation.</p>

<p><a name="JavaScript-ex3"></a></p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Example 3:E4X Service Implementation</b></div><div
class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">var</span> SOAP_ENV = <span class="code-keyword">new</span>
Namespace('SOAP-ENV',
                             'http:<span class="code-comment">//schemas.xmlsoap.org/soap/envelope/');
</span><span class="code-keyword">var</span> xs = <span class="code-keyword">new</span>
Namespace('xs', 'http:<span class="code-comment">//www.w3.org/2001/XMLSchema');
</span><span class="code-keyword">var</span> xsi = <span class="code-keyword">new</span>
Namespace('xsi', 'http:<span class="code-comment">//www.w3.org/2001/XMLSchema-instance');
</span><span class="code-keyword">var</span> ns = <span class="code-keyword">new</span>
Namespace('ns', 'http:<span class="code-comment">//apache.org/hello_world_soap_http/types');
</span>
WebServiceProvider1.invoke = function(req) {
    <span class="code-keyword">default</span> xml namespace = ns;
    <span class="code-keyword">var</span> name = (req..requestType)[0];
    <span class="code-keyword">default</span> xml namespace = SOAP_ENV;
    <span class="code-keyword">var</span> resp = &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV={SOAP_ENV}
xmlns:xs={xs} xmlns:xsi={xsi}/&gt;;
    resp.Body = &lt;Body/&gt;;
    resp.Body.ns::greetMeResponse = &lt;ns:greetMeResponse xmlns:ns={ns}/&gt;;
    resp.Body.ns::greetMeResponse.ns::responseType = 'Hi ' + name;
    <span class="code-keyword">return</span> resp;
}
</pre>
</div></div>
<h2><a name="JavaScript-DeployingScriptedServices"></a>Deploying Scripted
Services</h2>

<p>CXF provides a lightweight container that allows you to deploy your Javascript and
E4X services and take advantage of CXF's pluggable transport infrastructure.</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>Note</b><br
/>Script based services can only work with SOAP messages. So, while they are multi-transport,
they can only use the SOAP binding.</td></tr></table></div>
<p>You deploy services into the container using the following command:</p>
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<p><tt>java org.apache.cxf.js.rhino.ServerApp [ -a <em>addressURL</em>
] [ -b <em>baseAddressURL</em> ] file.js [ <em>file2.js</em> <em>file3.jsx</em>
... ]</tt></p>
</div></div>
<p>The <tt>org.apache.cxf.js.rhino.ServerApp</tt> class, shorted to <tt>ServerApp</tt>
below, takes one or more Javascript files, suffixed with a <tt>.js</tt>, or E4X
files, suffixed with a <tt>.jsx</tt>, and loads them into the CXF runtime. If
<tt>ServerApp</tt> locates JAX-WS metadata in the files it creates and registers
a JAX-WS <tt>Provider&lt;DOMSource&gt;</tt> object for each service. The
<tt>Provider&lt;DOMSource&gt;</tt> object delegates the processing of
requests to the implementation stored in the associated file. <tt>ServerApp</tt>
can also take the name of a directory containing Javascript and E4X files. It will load all
of the scripts that contain JAX-WS metadata, load them, and publish a service endpoint for
each one.</p>

<p><tt>ServerApp</tt> has three optional arguments:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Argument</th>
<th class='confluenceTh'>Description</th>
</tr>
<tr>
<td class='confluenceTd'> <tt>-a <em>addressURL</em></tt> </td>
<td class='confluenceTd'> Specifies the address at which <tt>ServerApp</tt>
publishes the service endpoint implementation found in the script file following the URL.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>-b <em>baseAddressURL</em></tt>
</td>
<td class='confluenceTd'> Specifies the base address used by <tt>ServerApp</tt>
when publishing the service endpoints defined by the script files. The full address for the
service endpoints is formed by appending the service's port name to the base address. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>-v</tt> </td>
<td class='confluenceTd'> Specifies that {<tt>ServerApp</tt> is to run in
verbose mode.</td>
</tr>
</tbody></table>
</div>


<p>The optional arguments take precedence over any addressing information provided in
{{EndpointAddress}}properties that appear in the JAX-WS metadata.</p>

<p>For example, if you deployed a JavaScript service using the command shown in <a
href="#JavaScript-ex4">Example 4</a>, your service would be deployed at <tt><a
href="http://cxf.apache.org/goodness" class="external-link" rel="nofollow">http://cxf.apache.org/goodness</a></tt>.</p>

<p><a name="JavaScript-ex4"></a><div class="panel" style="border-width:
1px;"><div class="panelHeader" style="border-bottom-width: 1px;"><b>Example
4:Deploying a Service at a Specified Address</b></div><div class="panelContent">
<p><tt>java org.apache.cxf.js.rhino.ServerApp -a <a href="http://cxf.apache.org/goodness"
class="external-link" rel="nofollow">http://cxf.apache.org/goodness</a> hello_world.jsx</tt></p>
</div></div></p>

<p>To deploy a number of services using a common base URL you could use the command
shown in <a href="#JavaScript-ex5">Example 5</a>. If the service defined by <tt>hello_world.jsx</tt>
had port name of <tt>helloWorld</tt>, <tt>ServerApp</tt> would publish
it at <tt><a href="http://cxf.apache.org/helloWorld" class="external-link" rel="nofollow">http://cxf.apache.org/helloWorld</a></tt>.
If the service defined by <tt>goodbye_moon.js</tt> had a port name of <tt>blue</tt>,
<tt>ServerApp</tt> would publish at <tt><a href="http://cxf.apache.org/blue"
class="external-link" rel="nofollow">http://cxf.apache.org/blue</a></tt>.</p>

<p><a name="JavaScript-ex5"></a><div class="panel" style="border-width:
1px;"><div class="panelHeader" style="border-bottom-width: 1px;"><b>Example
5:Deploying a Group of Services to a Base Address</b></div><div class="panelContent">
<p><tt>java org.apache.cxf.js.rhino.ServerApp -b <a href="http://cxf.apache.org"
class="external-link" rel="nofollow">http://cxf.apache.org</a> hello_world.jsx goodbye_moon.js</tt></p>
</div></div></p>

<p>You can also combine the arguments as shown in <a href="#JavaScript-ex6">Example
6</a> and your service would be deployed at <tt><a href="http://cxf.apache.org/goodness"
class="external-link" rel="nofollow">http://cxf.apache.org/goodness</a></tt>.
<tt>ServerApp</tt> would publish three service endpoints:</p>
<ul>
	<li>The service defined by <tt>hello_world.jsx</tt> at <tt><a
href="http://cxf.apache.org/helloWorld" class="external-link" rel="nofollow">http://cxf.apache.org/helloWorld</a></tt>.</li>
	<li>The service defined by <tt>goodbye_moon.js</tt> at <tt><a
href="http://cxf.apache.org/blue" class="external-link" rel="nofollow">http://cxf.apache.org/blue</a></tt>.</li>
	<li>The service defined by <tt>chocolate.jsx</tt> at <tt><a href="http://cxf.apache.org/goodness"
class="external-link" rel="nofollow">http://cxf.apache.org/goodness</a></tt>.</li>
</ul>


<p><a name="JavaScript-ex6"></a><div class="panel" style="border-width:
1px;"><div class="panelHeader" style="border-bottom-width: 1px;"><b>Example
6:Combining the Command Line Arguments</b></div><div class="panelContent">
<p><tt>java org.apache.cxf.js.rhino.ServerApp -b <a href="http://cxf.apache.org"
class="external-link" rel="nofollow">http://cxf.apache.org</a> hello_world.jsx goodbye_moon.js
-a <a href="http://cxf.apache.org/goodness" class="external-link" rel="nofollow">http://cxf.apache.org/goodness</a>
chocolate.jsx</tt></p>
</div></div></p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JavaScript">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=28428&revisedVersion=8&originalVersion=7">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JavaScript?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message