This project has retired. For details please refer to its
Attic page.
SaajTransport xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.ws.scout.transport;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.ws.scout.registry.RegistryException;
27 import org.apache.ws.scout.transport.Transport;
28 import org.apache.ws.scout.util.XMLUtils;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.w3c.dom.NamedNodeMap;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34 import org.xml.sax.InputSource;
35
36 import javax.xml.parsers.DocumentBuilderFactory;
37 import javax.xml.soap.MessageFactory;
38 import javax.xml.soap.Name;
39 import javax.xml.soap.SOAPBody;
40 import javax.xml.soap.SOAPBodyElement;
41 import javax.xml.soap.SOAPConnection;
42 import javax.xml.soap.SOAPConnectionFactory;
43 import javax.xml.soap.SOAPElement;
44 import javax.xml.soap.SOAPException;
45 import javax.xml.soap.SOAPFactory;
46 import javax.xml.soap.SOAPFault;
47 import javax.xml.soap.SOAPMessage;
48 import javax.xml.soap.SOAPPart;
49 import java.io.StringReader;
50 import java.net.URI;
51
52
53
54
55
56
57
58 public class SaajTransport implements Transport {
59
60 public static final String UDDI_V2_NAMESPACE = "urn:uddi-org:api_v2";
61
62 private static Log log = LogFactory.getLog(SaajTransport.class);
63
64 public Element send(Element request, URI endpointURL) throws TransportException {
65 if (log.isDebugEnabled()) {
66 String requestMessage = XMLUtils.convertNodeToXMLString(request);
67 log.debug("Request message: %s\n%s" + endpointURL + ":" + requestMessage);
68 }
69
70 Element response = null;
71 try {
72 SOAPMessage message = this.createSOAPMessage(request);
73
74 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
75 SOAPConnection connection = soapConnectionFactory.createConnection();
76 SOAPMessage soapResponse = connection.call(message, endpointURL.toURL());
77
78 SOAPBody soapBody = soapResponse.getSOAPBody();
79 boolean hasFault = soapBody.hasFault();
80 if (hasFault) {
81 SOAPFault soapFault = soapBody.getFault();
82 String faultStr = soapFault.getFaultCode() + "::" + soapFault.getFaultString();
83 throw new RegistryException(faultStr);
84 }
85 response = getFirstChildElement(soapBody);
86 } catch (Exception ex) {
87 log.error("Exception::" + ex.getMessage(), ex);
88 throw new TransportException(ex);
89 }
90 if (log.isDebugEnabled()) {
91 String responseMessage = XMLUtils.convertNodeToXMLString(response);
92 log.debug("Response message: %s" + responseMessage);
93 }
94
95 return response;
96 }
97
98 public String send(String request, URI endpointURL) throws TransportException {
99 Element reqEl = getElement(request);
100 Element respEl = this.send(reqEl, endpointURL);
101 return XMLUtils.convertNodeToXMLString(respEl);
102 }
103
104 private SOAPMessage createSOAPMessage(Element elem) throws Exception {
105 String prefix = "";
106 MessageFactory msgFactory = MessageFactory.newInstance();
107 SOAPFactory factory = SOAPFactory.newInstance();
108
109 SOAPMessage message = msgFactory.createMessage();
110 message.getSOAPHeader().detachNode();
111 SOAPPart soapPart = message.getSOAPPart();
112 SOAPBody soapBody = soapPart.getEnvelope().getBody();
113
114 Name bodyName = factory.createName(elem.getNodeName(), prefix, UDDI_V2_NAMESPACE);
115 SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName);
116 bodyElement.addNamespaceDeclaration(prefix, UDDI_V2_NAMESPACE);
117 appendAttributes(bodyElement, elem.getAttributes(), factory);
118 appendElements(bodyElement, elem.getChildNodes(), factory);
119 return message;
120 }
121
122 private void appendAttributes(SOAPElement bodyElement, NamedNodeMap nnm, SOAPFactory factory) throws SOAPException {
123 int len = nnm != null ? nnm.getLength() : 0;
124 for (int i = 0; i < len; i++) {
125 Node n = nnm.item(i);
126 String nodename = n.getNodeName();
127 String nodevalue = n.getNodeValue();
128 if ("xmlns".equals(nodename))
129 continue;
130 if (nodename.startsWith("xmlns:"))
131 continue;
132
133 if ("xml:lang".equals(nodename)) {
134 Name xmlLang = factory.createName("lang", "xml", "");
135 bodyElement.addAttribute(xmlLang, nodevalue);
136 } else
137 bodyElement.addAttribute(factory.createName(nodename), nodevalue);
138 }
139 }
140
141 private void appendElements(SOAPElement bodyElement, NodeList nlist, SOAPFactory factory) throws SOAPException {
142 String prefix = "";
143 int len = nlist != null ? nlist.getLength() : 0;
144 for (int i = 0; i < len; i++) {
145 Node node = nlist.item(i);
146 short nodeType = node != null ? node.getNodeType() : -100;
147 if (Node.ELEMENT_NODE == nodeType) {
148 Element el = (Element) node;
149 Name name = factory.createName(el.getNodeName(), prefix, UDDI_V2_NAMESPACE);
150 SOAPElement attachedEl = bodyElement.addChildElement(name);
151 appendAttributes(attachedEl, el.getAttributes(), factory);
152 appendElements(attachedEl, el.getChildNodes(), factory);
153 } else if (nodeType == Node.TEXT_NODE) {
154 bodyElement.addTextNode(node.getNodeValue());
155 }
156 }
157 }
158
159 private static Element getElement(String xmlFrag) {
160 Document doc = null;
161 Element reqElement = null;
162 try {
163 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
164 doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlFrag)));
165 reqElement = doc.getDocumentElement();
166 } catch (Exception ex) {
167 log.error("Exception:" + ex.getMessage(), ex);
168 }
169
170 return reqElement;
171 }
172
173 private Element getFirstChildElement(Element el) {
174 return getFirstChildElement(el, null);
175 }
176
177 private Element getFirstChildElement(Element el, String tagName) {
178 Element childEl = null;
179 NodeList nlist = el != null ? el.getChildNodes() : null;
180 int len = nlist != null ? nlist.getLength() : 0;
181 for (int i = 0; childEl == null && i < len; i++) {
182 Node node = nlist.item(i);
183 if (node.getNodeType() == Node.ELEMENT_NODE) {
184 if (tagName == null || tagName.equals(node.getLocalName()))
185 childEl = (Element) node;
186 }
187 }
188 if (log.isDebugEnabled()) {
189 String responseObtained = XMLUtils.convertNodeToXMLString(childEl);
190 log.debug("Response obtained: %s" + responseObtained);
191 }
192 return childEl;
193 }
194 }