blob: d267c65a8cac7d8c92423864e05c9cb508b7185d [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<link type="text/css" rel="stylesheet" href="/resources/site.css">
<script src='/resources/space.js'></script>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta name="keywords" content="business integration, EAI, SOA, Service Oriented Architecture, web services, SOAP, JBI, JMS, WSDL, XML, EDI, Electronic Data Interchange, standards support, integration standards, application integration, middleware, software, solutions, services, CXF, open source">
<meta name="description" content="Apache CXF, Services Framework - JavaScript">
<link type="text/css" rel="stylesheet" href="/resources/highlighter/styles/shCoreCXF.css">
<link type="text/css" rel="stylesheet" href="/resources/highlighter/styles/shThemeCXF.css">
<script src='/resources/highlighter/scripts/shCore.js'></script>
<script src='/resources/highlighter/scripts/shBrushBash.js'></script>
<script src='/resources/highlighter/scripts/shBrushJava.js'></script>
<script>
SyntaxHighlighter.defaults['toolbar'] = false;
SyntaxHighlighter.all();
</script>
<title>
Apache CXF -- JavaScript
</title>
</head>
<body onload="init()">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td id="cell-0-0" colspan="2">&nbsp;</td>
<td id="cell-0-1">&nbsp;</td>
<td id="cell-0-2" colspan="2">&nbsp;</td>
</tr>
<tr>
<td id="cell-1-0">&nbsp;</td>
<td id="cell-1-1">&nbsp;</td>
<td id="cell-1-2">
<!-- Banner -->
<div class="banner" id="banner"><div><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td align="left" colspan="1" nowrap>
<a shape="rect" href="http://cxf.apache.org/" title="Apache CXF"><span style="font-weight: bold; font-size: 170%; color: white">Apache CXF</span></a>
</td><td align="right" colspan="1" nowrap>
<a shape="rect" href="http://www.apache.org/" title="The Apache Sofware Foundation"><img border="0" alt="ASF Logo" src="http://cxf.apache.org/images/asf-logo.png"></a>
</td></tr></table></div></div>
<!-- Banner -->
<div id="top-menu">
<table border="0" cellpadding="1" cellspacing="0" width="100%">
<tr>
<td>
<div align="left">
<!-- Breadcrumbs -->
<a href="index.html">Index</a>&nbsp;&gt;&nbsp;<a href="dynamic-languages.html">Dynamic Languages</a>&nbsp;&gt;&nbsp;<a href="javascript.html">JavaScript</a>
<!-- Breadcrumbs -->
</div>
</td>
<td>
<div align="right">
<!-- Quicklinks -->
<div id="quicklinks"><p><a shape="rect" href="http://cxf.apache.org/download.html">Download</a> | <a shape="rect" href="http://cxf.apache.org/docs/index.html">Documentation</a></p></div>
<!-- Quicklinks -->
</div>
</td>
</tr>
</table>
</div>
</td>
<td id="cell-1-3">&nbsp;</td>
<td id="cell-1-4">&nbsp;</td>
</tr>
<tr>
<td id="cell-2-0" colspan="2">&nbsp;</td>
<td id="cell-2-1">
<table>
<tr valign="top">
<td height="100%">
<div id="wrapper-menu-page-right">
<div id="wrapper-menu-page-top">
<div id="wrapper-menu-page-bottom">
<div id="menu-page">
<!-- NavigationBar -->
<div id="navigation"><ul class="alternate"><li><a shape="rect" href="overview.html">Overview</a></li><li><a shape="rect" href="how-tos.html">How-Tos</a></li><li><a shape="rect" href="frontends.html">Frontends</a></li><li><a shape="rect" href="databindings.html">DataBindings</a></li><li><a shape="rect" href="transports.html">Transports</a></li><li><a shape="rect" href="configuration.html">Configuration</a></li><li><a shape="rect" href="debugging-and-logging.html">Debugging and Logging</a></li><li><a shape="rect" href="tools.html">Tools</a></li><li><a shape="rect" href="restful-services.html">RESTful Services</a></li><li><a shape="rect" href="wsdl-bindings.html">WSDL Bindings</a></li><li><a shape="rect" href="service-routing.html">Service Routing</a></li><li><a shape="rect" href="dynamic-languages.html">Dynamic Languages</a></li><li><a shape="rect" href="ws-support.html">WS-* Support</a></li><li><a shape="rect" href="advanced-integration.html">Advanced Integration</a></li><li><a shape="rect" href="deployment.html">Deployment</a></li><li><a shape="rect" href="schemas-and-namespaces.html">Use of Schemas and Namespaces</a></li></ul><hr><ul class="alternate"><li><p>Search</p></li></ul><form enctype="application/x-www-form-urlencoded" method="get" id="cse-search-box" action="http://www.google.com/cse">
<div>
<input type="hidden" name="cx" value="002890367768291051730:o99qiwa09y4">
<input type="hidden" name="ie" value="UTF-8">
<input type="text" name="q" size="21">
<input type="submit" name="sa" value="Search">
</div>
</form>
<script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&amp;lang=en"></script><hr><ul class="alternate"><li><a shape="rect" href="http://cxf.apache.org/javadoc/latest/">API 3.2.x (Javadoc)</a></li><li><a shape="rect" href="http://cxf.apache.org/javadoc/latest-3.1.x/">API 3.1.x (Javadoc)</a></li><li><a shape="rect" href="http://cxf.apache.org/">CXF Website</a></li></ul><p>&#160;</p><p><a shape="rect" class="external-link" href="http://www.apache.org/events/current-event.html"><span class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image confluence-external-resource" src="http://www.apache.org/events/current-event-125x125.png" data-image-src="http://www.apache.org/events/current-event-125x125.png"></span></a></p></div>
<!-- NavigationBar -->
</div>
</div>
</div>
</div>
</td>
<td height="100%">
<!-- Content -->
<div class="wiki-content">
<div id="ConfluenceContent"><h1 id="JavaScript-UsingDynamicLanguagestoImplementServices">Using Dynamic Languages to Implement Services</h1>
<h2 id="JavaScript-Overview">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 <code>Provider</code> implementations that handle their requests and responses (either SOAP messages or SOAP payloads) as DOM documents.</p>
<h2 id="JavaScript-ImplementingaServiceinJavaScript">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 id="JavaScript-DefiningtheMetadata">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 <code>WebServiceProvider</code> as a JAX-WS metadata variable.</p>
<p>Properties of the variable are expected to specify the same metadata that the JAX-WS <code>WebServiceProvider</code> annotation specifies, including:</p>
<ul><li><code>wsdlLocation</code> specifies a URL for the WSDL defining the service.</li><li><code>serviceName</code> specifies the name of the service.</li><li><code>portName</code> specifies the service's port/interface name.</li><li><code>targetNamespace</code> specifies the target namespace of the service.</li></ul>
<p>The Javascript <code>WebServiceProvider</code> can also specify the following optional properties:</p>
<ul><li><code>ServiceMode</code> indicates whether the specified service handles SOAP payload documents or full SOAP message documents. This property mimics the JAX-WS <code>ServiceMode</code> annotation. The default value is <code>PAYLOAD</code>.</li><li><code>BindingMode</code> indicates the service binding ID URL. The default is the SOAP 1.1/HTTP binding.</li><li><code>EndpointAddress</code> indicates the URL consumer applications use to communicate with this service. The property is optional but has no default.</li></ul>
<p><a shape="rect" href="javascript.html">Example 1</a> shows a metadata description for a JavaScript service implementation.</p>
<p><span class="confluence-anchor-link" id="JavaScript-ex1"></span></p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Example 1:JavaScript Metadata</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">
var WebServiceProvider1 = {
'wsdlLocation': 'file:./wsdl/hello_world.wsdl',
'serviceName': 'SOAPService1',
'portName': 'SoapPort1',
'targetNamespace': 'http://apache.org/hello_world_soap_http',
};
</pre>
</div></div>
<h3 id="JavaScript-ImplementingtheServiceLogic">Implementing the Service Logic</h3>
<p>You implement the service's logic using the required <code>invoke</code> property of the <code>WebServiceProvider</code> variable. This variable is a function that accepts one input argument, a <code>javax.xml.transform.dom.DOMSource</code> node, and returns a document of the same type. The <code>invoke</code> function can manipulate either the input or output documents using the regular Java <code>DOMSource</code> class interface just as a Java application would.</p>
<p><a shape="rect" href="javascript.html">Example 2</a> shows an <code>invoke</code> property for a simple JavaScript service implementation.</p>
<p><span class="confluence-anchor-link" id="JavaScript-ex2"></span></p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Example 2:JavaScript Service Implementation</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">
WebServiceProvider.invoke = function(document) {
var ns4 = "http://apache.org/hello_world_soap_http/types";
var list = document.getElementsByTagNameNS(ns4, "requestType");
var name = list.item(0).getFirstChild().getNodeValue();
var newDoc = document.getImplementation().createDocument(ns4, "ns4:greetMeResponse", null);
var el = newDoc.createElementNS(ns4, "ns4:responseType");
var txt = newDoc.createTextNode("Hi " + name);
el.insertBefore(txt, null);
newDoc.getDocumentElement().insertBefore(el, null);
return newDoc;
}
</pre>
</div></div>
<h2 id="JavaScript-ImplementingaServiceinECMAScriptforXML(E4X)">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 <code>WebServiceProvider</code> variable in JavaScript. You also implement the service's logic in the <code>WebServiceProvider</code> variable's <code>invoke</code> 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 shape="rect" href="javascript.html">Example 3</a> shows an <code>invoke</code> property for a simple E4X service implementation.</p>
<p><span class="confluence-anchor-link" id="JavaScript-ex3"></span></p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Example 3:E4X Service Implementation</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">
var SOAP_ENV = new Namespace('SOAP-ENV',
'http://schemas.xmlsoap.org/soap/envelope/');
var xs = new Namespace('xs', 'http://www.w3.org/2001/XMLSchema');
var xsi = new Namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
var ns = new Namespace('ns', 'http://apache.org/hello_world_soap_http/types');
WebServiceProvider1.invoke = function(req) {
default xml namespace = ns;
var name = (req..requestType)[0];
default xml namespace = SOAP_ENV;
var 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;
return resp;
}
</pre>
</div></div>
<h2 id="JavaScript-DeployingScriptedServices">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="confluence-information-macro confluence-information-macro-note"><p class="title">Note</p><span class="aui-icon aui-icon-small aui-iconfont-warning confluence-information-macro-icon"></span><div class="confluence-information-macro-body">
<p>Script based services can only work with SOAP messages. So, while they are multi-transport, they can only use the SOAP binding.</p></div></div>
<p>You deploy services into the container using the following command:</p>
<div class="panel" style="border-width: 1px;"><div class="panelContent">
<p><code>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> ... ]</code></p>
</div></div>
<p>The <code>org.apache.cxf.js.rhino.ServerApp</code> class, shorted to <code>ServerApp</code> below, takes one or more Javascript files, suffixed with a <code>.js</code>, or E4X files, suffixed with a <code>.jsx</code>, and loads them into the CXF runtime. If <code>ServerApp</code> locates JAX-WS metadata in the files it creates and registers a JAX-WS <code>Provider&lt;DOMSource&gt;</code> object for each service. The <code>Provider&lt;DOMSource&gt;</code> object delegates the processing of requests to the implementation stored in the associated file. <code>ServerApp</code> 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><code>ServerApp</code> has three optional arguments:</p>
<div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>Argument</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> <code>-a <em>addressURL</em></code> </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> Specifies the address at which <code>ServerApp</code> publishes the service endpoint implementation found in the script file following the URL. </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> <code>-b <em>baseAddressURL</em></code> </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> Specifies the base address used by <code>ServerApp</code> 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. </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> <code>-v</code> </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> Specifies that {<code>ServerApp</code> is to run in verbose mode.</p></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 shape="rect" href="javascript.html">Example 4</a>, your service would be deployed at <code><a shape="rect" href="http://cxf.apache.org/goodness">http://cxf.apache.org/goodness</a></code>.</p>
<p><span class="confluence-anchor-link" id="JavaScript-ex4"></span></p><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><code>java org.apache.cxf.js.rhino.ServerApp -a <a shape="rect" href="http://cxf.apache.org/goodness">http://cxf.apache.org/goodness</a> hello_world.jsx</code></p>
</div></div>
<p>To deploy a number of services using a common base URL you could use the command shown in <a shape="rect" href="javascript.html">Example 5</a>. If the service defined by <code>hello_world.jsx</code> had port name of <code>helloWorld</code>, <code>ServerApp</code> would publish it at <code><a shape="rect" href="http://cxf.apache.org/helloWorld">http://cxf.apache.org/helloWorld</a></code>. If the service defined by <code>goodbye_moon.js</code> had a port name of <code>blue</code>, <code>ServerApp</code> would publish at <code><a shape="rect" href="http://cxf.apache.org/blue">http://cxf.apache.org/blue</a></code>.</p>
<p><span class="confluence-anchor-link" id="JavaScript-ex5"></span></p><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><code>java org.apache.cxf.js.rhino.ServerApp -b <a shape="rect" href="http://cxf.apache.org">http://cxf.apache.org</a> hello_world.jsx goodbye_moon.js</code></p>
</div></div>
<p>You can also combine the arguments as shown in <a shape="rect" href="javascript.html">Example 6</a> and your service would be deployed at <code><a shape="rect" href="http://cxf.apache.org/goodness">http://cxf.apache.org/goodness</a></code>. <code>ServerApp</code> would publish three service endpoints:</p>
<ul><li>The service defined by <code>hello_world.jsx</code> at <code><a shape="rect" href="http://cxf.apache.org/helloWorld">http://cxf.apache.org/helloWorld</a></code>.</li><li>The service defined by <code>goodbye_moon.js</code> at <code><a shape="rect" href="http://cxf.apache.org/blue">http://cxf.apache.org/blue</a></code>.</li><li>The service defined by <code>chocolate.jsx</code> at <code><a shape="rect" href="http://cxf.apache.org/goodness">http://cxf.apache.org/goodness</a></code>.</li></ul>
<p><span class="confluence-anchor-link" id="JavaScript-ex6"></span></p><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><code>java org.apache.cxf.js.rhino.ServerApp -b <a shape="rect" href="http://cxf.apache.org">http://cxf.apache.org</a> hello_world.jsx goodbye_moon.js -a <a shape="rect" href="http://cxf.apache.org/goodness">http://cxf.apache.org/goodness</a> chocolate.jsx</code></p>
</div></div></div>
</div>
<!-- Content -->
</td>
</tr>
</table>
</td>
<td id="cell-2-2" colspan="2">&nbsp;</td>
</tr>
<tr>
<td id="cell-3-0">&nbsp;</td>
<td id="cell-3-1">&nbsp;</td>
<td id="cell-3-2">
<div id="footer">
<!-- Footer -->
<div id="site-footer">
<a href="http://cxf.apache.org/privacy-policy.html">Privacy Policy</a> -
(<a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=28428">edit page</a>)
(<a href="https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=28428&amp;showComments=true&amp;showCommentArea=true#addcomment">add comment</a>)<br>
Apache CXF, CXF, Apache, the Apache feather logo are trademarks of The Apache Software Foundation.<br>
All other marks mentioned may be trademarks or registered trademarks of their respective owners.
</div>
<!-- Footer -->
</div>
</td>
<td id="cell-3-3">&nbsp;</td>
<td id="cell-3-4">&nbsp;</td>
</tr>
<tr>
<td id="cell-4-0" colspan="2">&nbsp;</td>
<td id="cell-4-1">&nbsp;</td>
<td id="cell-4-2" colspan="2">&nbsp;</td>
</tr>
</table>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-4458903-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>