|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Sylvian, We are happy to announce you as the winner of WSO2 samples competition held during ApacheCon US 2005. And you will be the fortunate winner of a brand new nano pod for your valuable contribution to Axis2. We hope to see your contributions in the future as well. Chinthaka Sylvain Wallez wrote: > Hi there! > > Stimulated by the hope of winning a shiny Nano, I started hacking a > sample integrating Rhino and Axis2. I know Rhino quite well as > Cocoon is using it extensively, but I'm a total newbie when it > comes to Axis2. So I wrote the sample, but failed to run a test to > see if it actually works. > > So please find attached the patch against modules/samples with what > I've been up to, in the hope that it can be useful and that you > guys can spend a bit of time to check if it works. > > Thanks, Sylvain > > PS: I started to use Axiom for what could become a StAX-based > Cocoon. Nice stuff, although I found little things that I'm > missing. Working around for now, but expect some patches in the > near future :-) > > > ---------------------------------------------------------------------- > > > Index: maven.xml > =================================================================== > --- maven.xml (revision 355804) +++ maven.xml (working copy) @@ > -258,6 +258,26 @@ </goal> > > <!-- > ================================================================ > --> + <!--- Rhino Service Sample --> + <!-- > ================================================================ > --> + + <goal name="rhino"> + <mkdir > dir="target/samples"/> + <ant:copy > file="./src/sample/rhino/rhino-sample.js" > tofile="target/samples/rhino/sample/rhino/rhino-sample.js"/> + > <ant:copy file="target/classes/sample/rhino/RhinoReceiver.class" + > tofile="target/samples/rhino/sample/rhino/RhinoReceiver.class"/> + > <ant:copy file="${maven.repo.local}/rhino/jars/js-1.6R2.jar" > tofile="target/samples/rhino/lib/js-1.6R2.jar"/> + <ant:copy > file="${maven.repo.local}/xmlbeans/jars/xbean-2.0.0.jar" > tofile="target/samples/rhino/lib/xbean-2.0.0.jar"/> + <jar > destfile="${samples.dir}/rhinoService.aar"> + <fileset > dir="target/samples/rhino"> + </fileset> + > <fileset dir="src/sample/rhino/"> + <include > name="META-INF/**"/> + </fileset> + </jar> + > </goal> + + <!-- > ================================================================ > --> <!--- Security Sample --> <!-- > ================================================================ > --> <goal name="securitySample"> Index: project.xml > =================================================================== > --- project.xml (revision 355804) +++ project.xml (working copy) > @@ -211,6 +211,14 @@ <module>true</module> </properties> > </dependency> + <dependency> + > <groupId>rhino</groupId> + <artifactId>js</artifactId> + > <version>${rhino.version}</version> + <properties> + > <module>true</module> + </properties> + > </dependency> </dependencies> > > <!-- build information for the project --> Index: > src/sample/rhino/META-INF/services.xml > =================================================================== > --- src/sample/rhino/META-INF/services.xml (revision 0) +++ > src/sample/rhino/META-INF/services.xml (revision 0) @@ -0,0 +1,12 > @@ +<service name="RhinoService"> + <description> + This > is Rhino service , what this really does is take the incoming SOAP > message + and hand that over to a Rhino function which > processes the message and return a SOAP + message back to > MessageReceiver + </description> + <parameter > name="_javascript_Source" > locked="false">sample/rhino/rhino-sample.js</parameter> + + > <operation name="hello"> + <messageReceiver > class="sample.rhino.RhinoReceiver"/> + </operation> +</service> > \ No newline at end of file > > Property changes on: src/sample/rhino/META-INF/services.xml > ___________________________________________________________________ > Name: svn:keywords + Id Name: svn:eol-style + native > > Index: src/sample/rhino/RhinoReceiver.java > =================================================================== > --- src/sample/rhino/RhinoReceiver.java (revision 0) +++ > src/sample/rhino/RhinoReceiver.java (revision 0) @@ -0,0 +1,160 @@ > +/* + * Copyright 2004,2005 The Apache Software Foundation. + * + * > Licensed 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. + */ + +package > sample.rhino; + +import java.io.ByteArrayInputStream; +import > java.io.IOException; +import java.io.InputStream; +import > java.io.InputStreamReader; +import java.io.StringWriter; + +import > javax.xml.stream.XMLInputFactory; +import > javax.xml.stream.XMLStreamException; +import > javax.xml.stream.XMLStreamReader; + +import > org.apache.axis2.AxisFault; +import > org.apache.axis2.context.MessageContext; +import > org.apache.axis2.description.AxisOperation; +import > org.apache.axis2.description.AxisService; +import > org.apache.axis2.description.Parameter; +import > org.apache.axis2.engine.MessageReceiver; +import > org.apache.axis2.i18n.Messages; +import > org.apache.axis2.om.OMAbstractFactory; +import > org.apache.axis2.om.OMElement; +import > org.apache.axis2.om.OMFactory; +import > org.apache.axis2.om.OMNamespace; +import > org.apache.axis2.om.impl.llom.builder.StAXOMBuilder; +import > org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver; > +import org.apache.axis2.soap.SOAPEnvelope; +import > org.apache.axis2.soap.SOAPFactory; +import > org.mozilla._javascript_.Context; +import > org.mozilla._javascript_.Function; +import > org.mozilla._javascript_.Script; +import > org.mozilla._javascript_.Scriptable; +import > org.mozilla._javascript_.ScriptableObject; + +public class > RhinoReceiver extends AbstractInOutSyncMessageReceiver implements > MessageReceiver { + + public void > invokeBusinessLogic(MessageContext inMessage, MessageContext > outMessage) throws AxisFault { + try { + > AxisService service = > inMessage.getOperationContext().getServiceContext().getAxisService(); > + Parameter jsSource = > service.getParameter("_javascript_Source"); + if (jsSource > == null) { + throw new AxisFault( + > Messages.getMessage("paramIsNotSpecified", "_javascript_Source")); + > } + + // The operation should be a JS function + > AxisOperation op = > inMessage.getOperationContext().getAxisOperation(); + if > (op == null) { + throw new > AxisFault(Messages.getMessage("notFound", "Operation")); + > } + String funcName = op.getName().getLocalPart(); + + > Scriptable script = buildScript( + > service.getClassLoader(), + > jsSource.getValue().toString()); + + + OMElement body = > (OMElement)inMessage.getEnvelope().getBody(); + + // > Serialize the body of the request + StringWriter writer > = new StringWriter(); + body.build(); + > body.serialize(writer); + String bodyText = > writer.toString(); + + + Object jsFunc = > ScriptableObject.getProperty(script, funcName); + if > (jsFunc == Scriptable.NOT_FOUND) { + throw new > AxisFault("Function \"_javascript_:" + funcName + "()\" not found"); > + } + + if (!(jsFunc instanceof Function)) { > + throw new AxisFault("_javascript_:" + funcName + " > is not a function"); + } + + Object result; + > Context ctx = Context.enter(); + try { + > result = ((Function)jsFunc).call(ctx, script.getParentScope(), > null, new Object[]{bodyText}); + } finally { + > Context.exit(); + } + + if (result == null) { > + throw new AxisFault("No result from _javascript_ > function"); + } + + SOAPFactory fac = null; + > if(inMessage.isSOAP11()){ + fac = > OMAbstractFactory.getSOAP11Factory(); + }else{ + > fac = OMAbstractFactory.getSOAP12Factory(); + } + > SOAPEnvelope envelope = fac.getDefaultEnvelope(); + > OMNamespace ns = + > fac.createOMNamespace("http://soapenc/", "res"); + > OMElement responseElement = + > fac.createOMElement(funcName + "Response", ns); + String > outMessageString = result.toString(); + // > System.out.println("outMessageString = " + outMessageString); + > // responseElement.setText(outMessageString); + > responseElement.addChild(getpayLoad(outMessageString)); + > envelope.getBody().addChild(responseElement); + > outMessage.setEnvelope(envelope); + + } catch (Exception e) > { + throw new AxisFault(e); + } + } + + > private OMElement getpayLoad(String str) throws XMLStreamException > { + XMLStreamReader xmlReader = + > XMLInputFactory.newInstance().createXMLStreamReader( + > new ByteArrayInputStream(str.getBytes())); + OMFactory fac = > OMAbstractFactory.getOMFactory(); + + StAXOMBuilder > staxOMBuilder = + new StAXOMBuilder(fac, xmlReader); + > return staxOMBuilder.getDocumentElement(); + } + + public > static Scriptable buildScript(ClassLoader classLoader, String > location) throws IOException { + InputStream stream = > classLoader.getResourceAsStream(location); + if (stream == > null) { + throw new AxisFault("Resource " + location + " > doesn't exist."); + } + Scriptable scope; + > Context ctx = Context.enter(); + try { + Script > script = ctx.compileReader( + new > InputStreamReader(stream), // in + location, // > sourceName + 0, // lineNo + null // > securityDomain + ); + + scope = > ctx.initStandardObjects(); + + script.exec(ctx, scope); > + } finally { + Context.exit(); + } + > return scope; + } +} > > Property changes on: src/sample/rhino/RhinoReceiver.java > ___________________________________________________________________ > Name: svn:keywords + Id Name: svn:eol-style + native > > Index: src/sample/rhino/rhino-sample.js > =================================================================== > --- src/sample/rhino/rhino-sample.js (revision 0) +++ > src/sample/rhino/rhino-sample.js (revision 0) @@ -0,0 +1,5 @@ + + > +function hello() { + return "<hello>world</hello>"; +} \ No > newline at end of file > > Property changes on: src/sample/rhino/rhino-sample.js > ___________________________________________________________________ > Name: svn:keywords + Id Name: svn:eol-style + native > > Index: src/sample/rhino/RhinoTestClient.java > =================================================================== > --- src/sample/rhino/RhinoTestClient.java (revision 0) +++ > src/sample/rhino/RhinoTestClient.java (revision 0) @@ -0,0 +1,56 @@ > +package sample.rhino; + +import java.io.StringWriter; + +import > javax.xml.stream.XMLOutputFactory; + +import > org.apache.axis2.Constants; +import > org.apache.axis2.addressing.EndpointReference; +import > org.apache.axis2.client.Call; +import > org.apache.axis2.client.Options; +import > org.apache.axis2.om.OMAbstractFactory; +import > org.apache.axis2.om.OMElement; +import > org.apache.axis2.om.OMFactory; +import > org.apache.axis2.om.OMNamespace; + + +public class RhinoTestClient > { + + private static EndpointReference targetEPR = new > EndpointReference("http://localhost:8080/axis2/services/rhinoService"); > + + public static OMElement getEchoOMElement() { + > OMFactory fac = OMAbstractFactory.getOMFactory(); + > OMNamespace omNs = fac.createOMNamespace( + > "http://example1.org/example1", "example1"); + OMElement > method = fac.createOMElement("echo", omNs); + OMElement > value = fac.createOMElement("Text", omNs); + > value.addChild(fac.createText(value, "Axis2 Echo String ")); + > method.addChild(value); + + return method; + } + + > public static void main(String[] args) throws Exception { + > OMElement payload = getEchoOMElement(); + Call call = new > Call(); + + Options options = new Options(); + > call.setClientOptions(options); + options.setTo(targetEPR); > + > options.setListenerTransportProtocol(Constants.TRANSPORT_HTTP); + + > //Blocking invocation + OMElement result = > call.invokeBlocking("hello", + payload); + + > StringWriter writer = new StringWriter(); + > result.serialize(XMLOutputFactory.newInstance() + > .createXMLStreamWriter(writer)); + writer.flush(); + + > System.out.println(writer.toString()); + + } + +} + > > Property changes on: src/sample/rhino/RhinoTestClient.java > ___________________________________________________________________ > Name: svn:keywords + Id Name: svn:eol-style + native > -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (MingW32) iD8DBQFDoOA6jON2uBzUhh8RAinLAJ469rtY2mRm59Zl6p67ZUjYPEqkNwCgrlY3 qzdZZ3hDjmGH/dRVKrFu0k8= =SN5K -----END PGP SIGNATURE----- |
