This project has retired. For details please refer to its Attic page.
SaajTransport xref
View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source.
3    * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * Transport based on SAAJ
54   *
55   * @author Anil Saldhana (anil@apache.org)
56   * @author Richard Opalka (richard.opalka@redhat.com)
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              //Make the SAAJ Call now
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         //Create the outer body element
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             //Special case:  xml:lang
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 }