Using Dynamic Languages to Implement Services
Overview
JavascriptJavaScript, 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.
CXF provides support for developing services using JavaScript and ECMAScript for XML(E4X). The pattern 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.
Implementing a Service in
...
JavaScript
Writing a service in Javascript is JavaScriptis a two step process:
- Definethe Define the JAX-WS style metadata.
- Implementthe Implement the services business logic.
Defining the Metadata
Normal Java providers typically use Java annotations to specify JAX-WS metadata. Since Javascript JavaScript does not support annotations, you use ordinary Javascript JavaScript variables to specify metadata for Javascript JavaScript implementations. CXF treats any Javascript variable in your code whose name equals or begins with WebServiceProvider
as a JAX-WS metadata variable.
...
Example 1 shows a metadata description for a Javascript JavaScript service implementation.
Anchor | ||||
---|---|---|---|---|
|
Code Block | ||
---|---|---|
| ||
var WebServiceProvider1 = { 'wsdlLocation': 'file:./wsdl/hello_world.wsdl', 'serviceName': 'SOAPService1', 'portName': 'SoapPort1', 'targetNamespace': 'http://apache.org/hello_world_soap_http', }; |
Implementing the Service Logic
You implement the service's logic using the required invoke
property of the WebServiceProvider
variable. This variable is a function that accepts one input argument, a javax.xml.transform.dom.DOMSource
node, and returns a document of the same type. The invoke
function can manipulate either the input or output documents using the regular Java DOMSource
class interface just as a Java application would.
Example 2 shows an invoke
property for a simple Javascript JavaScript service implementation.
Anchor | ||||
---|---|---|---|---|
|
Code Block | ||
---|---|---|
| ||
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; } |
Implementing a Service in ECMAScript for XML(E4X)
Writing a CXF service using E4X is very similar to writing a service using JavascriptJavaScript. You define the JAX-WS metadata using the same WebServiceProvider
variable in JavascriptJavaScript. You also implement the service's logic in the WebServiceProvider
variable's invoke
property.
...
Code Block | ||
---|---|---|
| ||
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 = <SOAP-ENV:Envelope xmlns:SOAP-ENV={SOAP_ENV} xmlns:xs={xs} xmlns:xsi={xsi}/>; resp.Body = <Body/>; resp.Body.ns::greetMeResponse = <ns:greetMeResponse xmlns:ns={ns}/>; resp.Body.ns::greetMeResponse.ns::responseType = 'Hi ' + name; return resp; } |
Deploying Scripted Services
CXF provides a lightweight container that allows you to deploy your Javascript and E4X services and take advantage of CXF's pluggable transport infrastructure.
...
For example, if you deployed a Javascript JavaScript service using the command shown in Example 4, your service would be deployed at http://cxf.apache.org/goodness
.
...