cocoon-docs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Cocoon Wiki] Update of "WebServiceServer" by SebastienArbogast
Date Sun, 05 Jun 2005 18:46:55 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Cocoon Wiki" for change notification.

The following page has been changed by SebastienArbogast:
http://wiki.apache.org/cocoon/WebServiceServer

------------------------------------------------------------------------------
  
  With that sample, you can just deploy your application, and when your testing tool asks
for a URL where it can find the WSDL description you can just enter http://localhost:8080/cocoon/webservice
(or whatever you customized above).
  
+ == Second approach (recommended) : using Axis API's ==
+ 
+ The code is self-documented so... Here is the flowscript code :
+ {{{
+ importPackage(Packages.java.io);
+ importClass(Packages.java.lang.System);
+ importPackage(Packages.org.apache.axis);
+ importPackage(Packages.org.apache.axis.message);
+ 
+ /**
+  * This function is for the webservice, it is called, when a SOAP-Envelope is 
+  * received, gets the Parameters out of it and then dispatches the called method to 
+  * javaclasses in the package jan.
+  * Then it finally generates a SOAP-response and serialises it back to the client.
+  */
+ function webservice(){
+ 	//getting the envelope out of the request (can be done only once)
+ 	var requestMessage = new java.io.ByteArrayOutputStream();
+ 	cocoon.processPipelineTo("requestMessage", null, requestMessage);
+ 	
+ 	// First we create a message from the request message we just retrieved
+ 	var message = new org.apache.axis.Message(requestMessage.toString());
+ 	// Then as we could have a message with attachment, we get the SOAP part of the message
+ 	var soapPart = message.getSOAPPart();
+ 	// We retrieve the envelope in the SOAP part
+ 	var envelope = soapPart.getEnvelope();
+ 	// Then the body of the envelope
+ 	var body = envelope.getBody();
+ 	// By convention for this sample we have only one child element under body
+ 	var it = body.getChildElements();
+ 	// The messageContent variable is very important because it's the one that contains the
internal content
+ 	// of the SOAP body, the one that gives us all the parameters of the request 
+ 	var messageContent = it.next();
+ 	// ...and the method name corresponds to its local name
+     var soapMethod = messageContent.getElementName().getLocalName();
+     
+     if(soapMethod == "echo"){
+         //extracting the parameter
+         var soapParameter = messageContent.getValue();
+         // process the method (echo in this case)
+         // creating the answer envelope
+ 		var answerEnvelope = new SOAPEnvelope();
+ 		// creating the answer content (according to the WSDL description) 
+ 		var answerContent = new SOAPBodyElement(new PrefixedQName("cocoonWS","result","cws"));
+ 		// inserting the echo
+ 		answerContent.addTextNode(soapParameter);
+ 		answerEnvelope.addBodyElement(answerContent);
+         // send the answer back to the client 
+         cocoon.sendPage("soapAnswer", {"soapData": answerEnvelope.toString().getBytes("UTF-8")});
+         return;
+ 	} 
+ 	/* here you can add more methods */
+ 	else { 
+ 	    //setting the answer if no method matched
+ 	    var answerEnvelope = new SOAPEnvelope();
+ 	    var answer = "The method you have called is not understood by this server. " +
+ 					"Sorry!";      
+ 		var fault = new AxisFault(answer);   
+ 		var soapFault = new SOAPFault(fault);
+ 		answerEnvelope.addBodyElement(soapFault);
+ 		cocoon.sendPage("soapAnswer", {"soapData":answerEnvelope.toString().getBytes("UTF-8")});
+ 	    return;
+     }
+ }
+ }}}
+ 
+ And here is the corresponding sitemap snippet :
+ {{{
+         <!-- Web Service pipeline -->
+         <map:pipeline>
+             
+             <!-- start the webservice - call the flow-function -->
+             <map:match pattern="*.sws">
+                 <map:call function="webservice"/>
+             </map:match>
+             
+             <!-- getting the request message -->
+             <map:match pattern="requestMessage">
+                 <map:generate type="stream"/>
+                 <map:serialize type="xmlnope"/>
+             </map:match> 
+             
+             <!-- sending the response to the client-->
+             <map:match pattern="soapAnswer">
+             	<map:generate src="module:flow-attr:soapData"/>
+                 <map:serialize type="xml"/>
+             </map:match>     
+             
+         </map:pipeline>
+ }}}
+ 
+ Of course as you will have noticed, this code only exposes the echo webservice but you can
simply adapt it.
+ 
+ The only thing we still miss now to use the full power of Axis, is to be able to build a
plain axis MessageContext object. Maybe [http://cocoon.apache.org/2.1/apidocs/org/apache/cocoon/components/axis/SoapServerImpl.html
this class] would help us to do it, if someone could explain how we can use it.
+ 
+ [SebastienArbogast]
+ 
  ----
  
  :) Ok, thats my first wikipage. If its all crap, just delete it ;)

Mime
View raw message