001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.systest.provider;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.soap.MessageFactory;
026: import javax.xml.transform.Transformer;
027: import javax.xml.transform.TransformerFactory;
028: import javax.xml.transform.dom.DOMResult;
029: import javax.xml.transform.sax.SAXSource;
030: import javax.xml.ws.Provider;
031: import javax.xml.ws.Service;
032: import javax.xml.ws.ServiceMode;
033: import javax.xml.ws.WebServiceProvider;
034:
035: import org.w3c.dom.DOMImplementation;
036: import org.w3c.dom.Document;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.bootstrap.DOMImplementationRegistry;
039: import org.w3c.dom.ls.DOMImplementationLS;
040: import org.w3c.dom.ls.LSOutput;
041: import org.w3c.dom.ls.LSSerializer;
042:
043: import org.xml.sax.InputSource;
044:
045: //The following wsdl file is used.
046: //wsdlLocation = "/trunk/testutils/src/main/resources/wsdl/hello_world_rpc_lit.wsdl"
047: @WebServiceProvider(portName="SoapPortProviderRPCLit6",serviceName="SOAPServiceProviderRPCLit",targetNamespace="http://apache.org/hello_world_rpclit",wsdlLocation="/wsdl/hello_world_rpc_lit.wsdl")
048: @ServiceMode(value=Service.Mode.PAYLOAD)
049: public class HWSAXSourcePayloadProvider implements Provider<SAXSource> {
050:
051: private static QName sayHi = new QName(
052: "http://apache.org/hello_world_rpclit", "sayHi");
053: private static QName greetMe = new QName(
054: "http://apache.org/hello_world_rpclit", "greetMe");
055: private MessageFactory factory;
056: private InputSource sayHiInputSource;
057: private InputSource greetMeInputSource;
058:
059: public HWSAXSourcePayloadProvider() {
060:
061: try {
062: factory = MessageFactory.newInstance();
063: InputStream is = getClass().getResourceAsStream(
064: "resources/sayHiRpcLiteralResp.xml");
065: Document sayHiDocument = factory.createMessage(null, is)
066: .getSOAPBody().extractContentAsDocument();
067: sayHiInputSource = new InputSource(
068: getSOAPBodyStream(sayHiDocument));
069:
070: InputStream is2 = getClass().getResourceAsStream(
071: "resources/GreetMeRpcLiteralResp.xml");
072: Document greetMeDocument = factory.createMessage(null, is2)
073: .getSOAPBody().extractContentAsDocument();
074: greetMeInputSource = new InputSource(
075: getSOAPBodyStream(greetMeDocument));
076:
077: } catch (Exception ex) {
078: ex.printStackTrace();
079: }
080: }
081:
082: public SAXSource invoke(SAXSource request) {
083: SAXSource response = new SAXSource();
084: try {
085:
086: DOMResult domResult = new DOMResult();
087: Transformer transformer = TransformerFactory.newInstance()
088: .newTransformer();
089: transformer.transform(request, domResult);
090: Node n = domResult.getNode().getFirstChild();
091:
092: while (n.getNodeType() != Node.ELEMENT_NODE) {
093: n = n.getNextSibling();
094: }
095: if (n.getLocalName().equals(sayHi.getLocalPart())) {
096: response.setInputSource(sayHiInputSource);
097:
098: } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
099: response.setInputSource(greetMeInputSource);
100: }
101: } catch (Exception ex) {
102: ex.printStackTrace();
103: }
104: return response;
105: }
106:
107: private InputStream getSOAPBodyStream(Document doc)
108: throws Exception {
109: // Try to get the DOMImplementation from the doc before
110: // defaulting to the sun implementation class (which uses
111: // sun internal xerces classes not found in the ibm jdk).
112: DOMImplementationLS impl = null;
113: DOMImplementation docImpl = doc.getImplementation();
114: if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
115: impl = (DOMImplementationLS) docImpl
116: .getFeature("LS", "3.0");
117: } else {
118: System
119: .setProperty(DOMImplementationRegistry.PROPERTY,
120: "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
121: DOMImplementationRegistry registry = DOMImplementationRegistry
122: .newInstance();
123: impl = (DOMImplementationLS) registry
124: .getDOMImplementation("LS");
125: }
126: LSOutput output = impl.createLSOutput();
127: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
128: output.setByteStream(byteArrayOutputStream);
129: LSSerializer writer = impl.createLSSerializer();
130: writer.write(doc, output);
131: byte[] buf = byteArrayOutputStream.toByteArray();
132: return new ByteArrayInputStream(buf);
133: }
134:
135: }
|