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.stream.StreamSource;
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: //The following wsdl file is used.
026: //wsdlLocation = "C:/CeltixSVN/trunk/celtix-testutils/src/main/resources/wsdl/hello_world_rpc_lit.wsdl"
027: @WebServiceProvider(portName="SoapPortRPCLit6",serviceName="SOAPServiceRPCLit6",targetNamespace="http://objectweb.org/hello_world_rpclit",wsdlLocation="/wsdl/hello_world_rpc_lit.wsdl")
028: @ServiceMode(value=Service.Mode.PAYLOAD)
029: public class HWStreamSourcePayloadProvider implements
030: Provider<StreamSource> {
031:
032: private static QName sayHi = new QName(
033: "http://objectweb.org/hello_world_rpclit", "sayHi");
034: private static QName greetMe = new QName(
035: "http://objectweb.org/hello_world_rpclit", "greetMe");
036: private InputStream sayHiInputStream;
037: private InputStream greetMeInputStream;
038: private MessageFactory factory;
039:
040: public HWStreamSourcePayloadProvider() {
041:
042: try {
043: factory = MessageFactory.newInstance();
044: InputStream is = getClass().getResourceAsStream(
045: "resources/sayHiRpcLiteralResp.xml");
046: Document sayHiDocument = factory.createMessage(null, is)
047: .getSOAPBody().extractContentAsDocument();
048: sayHiInputStream = getSOAPBodyStream(sayHiDocument);
049:
050: InputStream is2 = getClass().getResourceAsStream(
051: "resources/GreetMeRpcLiteralResp.xml");
052: Document greetMeDocument = factory.createMessage(null, is2)
053: .getSOAPBody().extractContentAsDocument();
054: greetMeInputStream = getSOAPBodyStream(greetMeDocument);
055:
056: } catch (Exception ex) {
057: ex.printStackTrace();
058: }
059: }
060:
061: public StreamSource invoke(StreamSource request) {
062: StreamSource response = new StreamSource();
063: try {
064: DOMResult domResult = new DOMResult();
065: Transformer transformer = TransformerFactory.newInstance()
066: .newTransformer();
067: transformer.transform(request, domResult);
068: Node n = domResult.getNode().getFirstChild();
069:
070: while (n.getNodeType() != Node.ELEMENT_NODE) {
071: n = n.getNextSibling();
072: }
073: if (n.getLocalName().equals(sayHi.getLocalPart())) {
074: response.setInputStream(sayHiInputStream);
075: } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
076: response.setInputStream(greetMeInputStream);
077: }
078: } catch (Exception ex) {
079: ex.printStackTrace();
080: }
081: return response;
082: }
083:
084: private InputStream getSOAPBodyStream(Document doc)
085: throws Exception {
086: System
087: .setProperty(DOMImplementationRegistry.PROPERTY,
088: "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
089: DOMImplementationRegistry registry = DOMImplementationRegistry
090: .newInstance();
091: DOMImplementationLS impl = (DOMImplementationLS) registry
092: .getDOMImplementation("LS");
093: LSOutput output = impl.createLSOutput();
094: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
095: output.setByteStream(byteArrayOutputStream);
096: LSSerializer writer = impl.createLSSerializer();
097: writer.write(doc, output);
098: byte[] buf = byteArrayOutputStream.toByteArray();
099: return new ByteArrayInputStream(buf);
100: }
101:
102: }
|