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.stream.StreamSource;
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: //The following wsdl file is used.
044: //wsdlLocation = "/trunk/testutils/src/main/resources/wsdl/hello_world_rpc_lit.wsdl"
045: @WebServiceProvider(portName="SoapPortProviderRPCLit7",serviceName="SOAPServiceProviderRPCLit",targetNamespace="http://apache.org/hello_world_rpclit",wsdlLocation="/wsdl/hello_world_rpc_lit.wsdl")
046: @ServiceMode(value=Service.Mode.PAYLOAD)
047: public class HWStreamSourcePayloadProvider implements
048: Provider<StreamSource> {
049:
050: private static QName sayHi = new QName(
051: "http://apache.org/hello_world_rpclit", "sayHi");
052: private static QName greetMe = new QName(
053: "http://apache.org/hello_world_rpclit", "greetMe");
054: private InputStream sayHiInputStream;
055: private InputStream greetMeInputStream;
056: private MessageFactory factory;
057:
058: public HWStreamSourcePayloadProvider() {
059:
060: try {
061: factory = MessageFactory.newInstance();
062: InputStream is = getClass().getResourceAsStream(
063: "resources/sayHiRpcLiteralResp.xml");
064: Document sayHiDocument = factory.createMessage(null, is)
065: .getSOAPBody().extractContentAsDocument();
066: sayHiInputStream = getSOAPBodyStream(sayHiDocument);
067:
068: InputStream is2 = getClass().getResourceAsStream(
069: "resources/GreetMeRpcLiteralResp.xml");
070: Document greetMeDocument = factory.createMessage(null, is2)
071: .getSOAPBody().extractContentAsDocument();
072: greetMeInputStream = getSOAPBodyStream(greetMeDocument);
073:
074: } catch (Exception ex) {
075: ex.printStackTrace();
076: }
077: }
078:
079: public StreamSource invoke(StreamSource request) {
080: StreamSource response = new StreamSource();
081: try {
082: DOMResult domResult = new DOMResult();
083: Transformer transformer = TransformerFactory.newInstance()
084: .newTransformer();
085: transformer.transform(request, domResult);
086: Node n = domResult.getNode().getFirstChild();
087:
088: while (n.getNodeType() != Node.ELEMENT_NODE) {
089: n = n.getNextSibling();
090: }
091: if (n.getLocalName().equals(sayHi.getLocalPart())) {
092: response.setInputStream(sayHiInputStream);
093: } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
094: response.setInputStream(greetMeInputStream);
095: }
096: } catch (Exception ex) {
097: ex.printStackTrace();
098: }
099: return response;
100: }
101:
102: private InputStream getSOAPBodyStream(Document doc)
103: throws Exception {
104: // Try to get the DOMImplementation from the doc before
105: // defaulting to the sun implementation class (which uses
106: // sun internal xerces classes not found in the ibm jdk).
107: DOMImplementationLS impl = null;
108: DOMImplementation docImpl = doc.getImplementation();
109: if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
110: impl = (DOMImplementationLS) docImpl
111: .getFeature("LS", "3.0");
112: } else {
113: System
114: .setProperty(DOMImplementationRegistry.PROPERTY,
115: "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
116: DOMImplementationRegistry registry = DOMImplementationRegistry
117: .newInstance();
118: impl = (DOMImplementationLS) registry
119: .getDOMImplementation("LS");
120: }
121: LSOutput output = impl.createLSOutput();
122: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
123: output.setByteStream(byteArrayOutputStream);
124: LSSerializer writer = impl.createLSSerializer();
125: writer.write(doc, output);
126: byte[] buf = byteArrayOutputStream.toByteArray();
127: return new ByteArrayInputStream(buf);
128: }
129:
130: }
|