001: package org.objectweb.celtix.systest.provider;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.InputStream;
006:
007: import javax.xml.namespace.QName;
008: import javax.xml.soap.MessageFactory;
009: import javax.xml.transform.Transformer;
010: import javax.xml.transform.TransformerFactory;
011: import javax.xml.transform.dom.DOMResult;
012: import javax.xml.transform.sax.SAXSource;
013: import javax.xml.ws.Provider;
014: import javax.xml.ws.Service;
015: import javax.xml.ws.ServiceMode;
016: import javax.xml.ws.WebServiceProvider;
017:
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Node;
020: import org.w3c.dom.bootstrap.DOMImplementationRegistry;
021: import org.w3c.dom.ls.DOMImplementationLS;
022: import org.w3c.dom.ls.LSOutput;
023: import org.w3c.dom.ls.LSSerializer;
024:
025: import org.xml.sax.InputSource;
026:
027: //The following wsdl file is used.
028: //wsdlLocation = "C:/CeltixSVN/trunk/celtix-testutils/src/main/resources/wsdl/hello_world_rpc_lit.wsdl"
029: @WebServiceProvider(portName="SoapPortRPCLit5",serviceName="SOAPServiceRPCLit5",targetNamespace="http://objectweb.org/hello_world_rpclit",wsdlLocation="/wsdl/hello_world_rpc_lit.wsdl")
030: @ServiceMode(value=Service.Mode.PAYLOAD)
031: public class HWSAXSourcePayloadProvider implements Provider<SAXSource> {
032:
033: private static QName sayHi = new QName(
034: "http://objectweb.org/hello_world_rpclit", "sayHi");
035: private static QName greetMe = new QName(
036: "http://objectweb.org/hello_world_rpclit", "greetMe");
037: private MessageFactory factory;
038: private InputSource sayHiInputSource;
039: private InputSource greetMeInputSource;
040:
041: public HWSAXSourcePayloadProvider() {
042:
043: try {
044: factory = MessageFactory.newInstance();
045: InputStream is = getClass().getResourceAsStream(
046: "resources/sayHiRpcLiteralResp.xml");
047: Document sayHiDocument = factory.createMessage(null, is)
048: .getSOAPBody().extractContentAsDocument();
049: sayHiInputSource = new InputSource(
050: getSOAPBodyStream(sayHiDocument));
051:
052: InputStream is2 = getClass().getResourceAsStream(
053: "resources/GreetMeRpcLiteralResp.xml");
054: Document greetMeDocument = factory.createMessage(null, is2)
055: .getSOAPBody().extractContentAsDocument();
056: greetMeInputSource = new InputSource(
057: getSOAPBodyStream(greetMeDocument));
058:
059: } catch (Exception ex) {
060: ex.printStackTrace();
061: }
062: }
063:
064: public SAXSource invoke(SAXSource request) {
065: SAXSource response = new SAXSource();
066: try {
067:
068: DOMResult domResult = new DOMResult();
069: Transformer transformer = TransformerFactory.newInstance()
070: .newTransformer();
071: transformer.transform(request, domResult);
072: Node n = domResult.getNode().getFirstChild();
073:
074: while (n.getNodeType() != Node.ELEMENT_NODE) {
075: n = n.getNextSibling();
076: }
077: if (n.getLocalName().equals(sayHi.getLocalPart())) {
078: response.setInputSource(sayHiInputSource);
079:
080: } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
081: response.setInputSource(greetMeInputSource);
082: }
083: } catch (Exception ex) {
084: ex.printStackTrace();
085: }
086: return response;
087: }
088:
089: private InputStream getSOAPBodyStream(Document doc)
090: throws Exception {
091: System
092: .setProperty(DOMImplementationRegistry.PROPERTY,
093: "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
094: DOMImplementationRegistry registry = DOMImplementationRegistry
095: .newInstance();
096: DOMImplementationLS impl = (DOMImplementationLS) registry
097: .getDOMImplementation("LS");
098: LSOutput output = impl.createLSOutput();
099: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
100: output.setByteStream(byteArrayOutputStream);
101: LSSerializer writer = impl.createLSSerializer();
102: writer.write(doc, output);
103: byte[] buf = byteArrayOutputStream.toByteArray();
104: return new ByteArrayInputStream(buf);
105: }
106:
107: }
|