01: package org.objectweb.celtix.systest.provider;
02:
03: import java.io.InputStream;
04:
05: import javax.xml.namespace.QName;
06: import javax.xml.soap.MessageFactory;
07: import javax.xml.soap.SOAPBody;
08: import javax.xml.soap.SOAPMessage;
09: import javax.xml.transform.dom.DOMSource;
10: import javax.xml.ws.Provider;
11: import javax.xml.ws.WebServiceProvider;
12:
13: import org.w3c.dom.Document;
14: import org.w3c.dom.Node;
15:
16: //The following wsdl file is used.
17: //wsdlLocation = "C:/CeltixSVN/trunk/celtix-testutils/src/main/resources/wsdl/hello_world_rpc_lit.wsdl"
18: @WebServiceProvider(portName="SoapPortRPCLit2",serviceName="SOAPServiceRPCLit2",targetNamespace="http://objectweb.org/hello_world_rpclit",wsdlLocation="/wsdl/hello_world_rpc_lit.wsdl")
19: public class HWDOMSourcePayloadProvider implements Provider<DOMSource> {
20:
21: private static QName sayHi = new QName(
22: "http://objectweb.org/hello_world_rpclit", "sayHi");
23: private static QName greetMe = new QName(
24: "http://objectweb.org/hello_world_rpclit", "greetMe");
25: private SOAPMessage sayHiResponse;
26: private SOAPMessage greetMeResponse;
27: private MessageFactory factory;
28:
29: public HWDOMSourcePayloadProvider() {
30:
31: try {
32: factory = MessageFactory.newInstance();
33: InputStream is = getClass().getResourceAsStream(
34: "resources/sayHiRpcLiteralResp.xml");
35: sayHiResponse = factory.createMessage(null, is);
36: is.close();
37: is = getClass().getResourceAsStream(
38: "resources/GreetMeRpcLiteralResp.xml");
39: greetMeResponse = factory.createMessage(null, is);
40: is.close();
41: } catch (Exception ex) {
42: ex.printStackTrace();
43: }
44: }
45:
46: public DOMSource invoke(DOMSource request) {
47: DOMSource response = new DOMSource();
48: try {
49: SOAPMessage msg = factory.createMessage();
50: SOAPBody body = msg.getSOAPBody();
51: body.addDocument((Document) request.getNode());
52:
53: Node n = getElementChildNode(body);
54: if (n.getLocalName().equals(sayHi.getLocalPart())) {
55: response.setNode(sayHiResponse.getSOAPBody()
56: .extractContentAsDocument());
57: } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
58: response.setNode(greetMeResponse.getSOAPBody()
59: .extractContentAsDocument());
60: }
61: } catch (Exception ex) {
62: ex.printStackTrace();
63: }
64: return response;
65: }
66:
67: private Node getElementChildNode(SOAPBody body) {
68: Node n = body.getFirstChild();
69:
70: while (n.getNodeType() != Node.ELEMENT_NODE) {
71: n = n.getNextSibling();
72: }
73:
74: return n;
75: }
76: }
|