| 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 | 4 | public class SaajTransport implements Transport { |
| 59 | |
|
| 60 | |
public static final String UDDI_V2_NAMESPACE = "urn:uddi-org:api_v2"; |
| 61 | |
|
| 62 | 2 | private static Log log = LogFactory.getLog(SaajTransport.class); |
| 63 | |
|
| 64 | |
public Element send(Element request, URI endpointURL) throws TransportException { |
| 65 | 0 | if (log.isDebugEnabled()) { |
| 66 | 0 | String requestMessage = XMLUtils.convertNodeToXMLString(request); |
| 67 | 0 | log.debug("Request message: %s\n%s" + endpointURL + ":" + requestMessage); |
| 68 | |
} |
| 69 | |
|
| 70 | 0 | Element response = null; |
| 71 | |
try { |
| 72 | 0 | SOAPMessage message = this.createSOAPMessage(request); |
| 73 | |
|
| 74 | 0 | SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); |
| 75 | 0 | SOAPConnection connection = soapConnectionFactory.createConnection(); |
| 76 | 0 | SOAPMessage soapResponse = connection.call(message, endpointURL.toURL()); |
| 77 | |
|
| 78 | 0 | SOAPBody soapBody = soapResponse.getSOAPBody(); |
| 79 | 0 | boolean hasFault = soapBody.hasFault(); |
| 80 | 0 | if (hasFault) { |
| 81 | 0 | SOAPFault soapFault = soapBody.getFault(); |
| 82 | 0 | String faultStr = soapFault.getFaultCode() + "::" + soapFault.getFaultString(); |
| 83 | 0 | throw new RegistryException(faultStr); |
| 84 | |
} |
| 85 | 0 | response = getFirstChildElement(soapBody); |
| 86 | 0 | } catch (Exception ex) { |
| 87 | 0 | log.error("Exception::" + ex.getMessage(), ex); |
| 88 | 0 | throw new TransportException(ex); |
| 89 | 0 | } |
| 90 | 0 | if (log.isDebugEnabled()) { |
| 91 | 0 | String responseMessage = XMLUtils.convertNodeToXMLString(response); |
| 92 | 0 | log.debug("Response message: %s" + responseMessage); |
| 93 | |
} |
| 94 | |
|
| 95 | 0 | return response; |
| 96 | |
} |
| 97 | |
|
| 98 | |
public String send(String request, URI endpointURL) throws TransportException { |
| 99 | 0 | Element reqEl = getElement(request); |
| 100 | 0 | Element respEl = this.send(reqEl, endpointURL); |
| 101 | 0 | return XMLUtils.convertNodeToXMLString(respEl); |
| 102 | |
} |
| 103 | |
|
| 104 | |
private SOAPMessage createSOAPMessage(Element elem) throws Exception { |
| 105 | 0 | String prefix = ""; |
| 106 | 0 | MessageFactory msgFactory = MessageFactory.newInstance(); |
| 107 | 0 | SOAPFactory factory = SOAPFactory.newInstance(); |
| 108 | |
|
| 109 | 0 | SOAPMessage message = msgFactory.createMessage(); |
| 110 | 0 | message.getSOAPHeader().detachNode(); |
| 111 | 0 | SOAPPart soapPart = message.getSOAPPart(); |
| 112 | 0 | SOAPBody soapBody = soapPart.getEnvelope().getBody(); |
| 113 | |
|
| 114 | 0 | Name bodyName = factory.createName(elem.getNodeName(), prefix, UDDI_V2_NAMESPACE); |
| 115 | 0 | SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName); |
| 116 | 0 | bodyElement.addNamespaceDeclaration(prefix, UDDI_V2_NAMESPACE); |
| 117 | 0 | appendAttributes(bodyElement, elem.getAttributes(), factory); |
| 118 | 0 | appendElements(bodyElement, elem.getChildNodes(), factory); |
| 119 | 0 | return message; |
| 120 | |
} |
| 121 | |
|
| 122 | |
private void appendAttributes(SOAPElement bodyElement, NamedNodeMap nnm, SOAPFactory factory) throws SOAPException { |
| 123 | 0 | int len = nnm != null ? nnm.getLength() : 0; |
| 124 | 0 | for (int i = 0; i < len; i++) { |
| 125 | 0 | Node n = nnm.item(i); |
| 126 | 0 | String nodename = n.getNodeName(); |
| 127 | 0 | String nodevalue = n.getNodeValue(); |
| 128 | 0 | if ("xmlns".equals(nodename)) |
| 129 | 0 | continue; |
| 130 | 0 | if (nodename.startsWith("xmlns:")) |
| 131 | 0 | continue; |
| 132 | |
|
| 133 | 0 | if ("xml:lang".equals(nodename)) { |
| 134 | 0 | Name xmlLang = factory.createName("lang", "xml", ""); |
| 135 | 0 | bodyElement.addAttribute(xmlLang, nodevalue); |
| 136 | 0 | } else |
| 137 | 0 | bodyElement.addAttribute(factory.createName(nodename), nodevalue); |
| 138 | |
} |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
private void appendElements(SOAPElement bodyElement, NodeList nlist, SOAPFactory factory) throws SOAPException { |
| 142 | 0 | String prefix = ""; |
| 143 | 0 | int len = nlist != null ? nlist.getLength() : 0; |
| 144 | 0 | for (int i = 0; i < len; i++) { |
| 145 | 0 | Node node = nlist.item(i); |
| 146 | 0 | short nodeType = node != null ? node.getNodeType() : -100; |
| 147 | 0 | if (Node.ELEMENT_NODE == nodeType) { |
| 148 | 0 | Element el = (Element) node; |
| 149 | 0 | Name name = factory.createName(el.getNodeName(), prefix, UDDI_V2_NAMESPACE); |
| 150 | 0 | SOAPElement attachedEl = bodyElement.addChildElement(name); |
| 151 | 0 | appendAttributes(attachedEl, el.getAttributes(), factory); |
| 152 | 0 | appendElements(attachedEl, el.getChildNodes(), factory); |
| 153 | 0 | } else if (nodeType == Node.TEXT_NODE) { |
| 154 | 0 | bodyElement.addTextNode(node.getNodeValue()); |
| 155 | |
} |
| 156 | |
} |
| 157 | 0 | } |
| 158 | |
|
| 159 | |
private static Element getElement(String xmlFrag) { |
| 160 | 0 | Document doc = null; |
| 161 | 0 | Element reqElement = null; |
| 162 | |
try { |
| 163 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 164 | 0 | doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlFrag))); |
| 165 | 0 | reqElement = doc.getDocumentElement(); |
| 166 | 0 | } catch (Exception ex) { |
| 167 | 0 | log.error("Exception:" + ex.getMessage(), ex); |
| 168 | 0 | } |
| 169 | |
|
| 170 | 0 | return reqElement; |
| 171 | |
} |
| 172 | |
|
| 173 | |
private Element getFirstChildElement(Element el) { |
| 174 | 0 | return getFirstChildElement(el, null); |
| 175 | |
} |
| 176 | |
|
| 177 | |
private Element getFirstChildElement(Element el, String tagName) { |
| 178 | 0 | Element childEl = null; |
| 179 | 0 | NodeList nlist = el != null ? el.getChildNodes() : null; |
| 180 | 0 | int len = nlist != null ? nlist.getLength() : 0; |
| 181 | 0 | for (int i = 0; childEl == null && i < len; i++) { |
| 182 | 0 | Node node = nlist.item(i); |
| 183 | 0 | if (node.getNodeType() == Node.ELEMENT_NODE) { |
| 184 | 0 | if (tagName == null || tagName.equals(node.getLocalName())) |
| 185 | 0 | childEl = (Element) node; |
| 186 | |
} |
| 187 | |
} |
| 188 | 0 | if (log.isDebugEnabled()) { |
| 189 | 0 | String responseObtained = XMLUtils.convertNodeToXMLString(childEl); |
| 190 | 0 | log.debug("Response obtained: %s" + responseObtained); |
| 191 | |
} |
| 192 | 0 | return childEl; |
| 193 | |
} |
| 194 | |
} |