KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jaxr > juddi > transport > SaajTransport


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * 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.jboss.jaxr.juddi.transport;
23
24 import java.io.StringReader JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.net.URL JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.soap.MessageFactory JavaDoc;
30 import javax.xml.soap.Name JavaDoc;
31 import javax.xml.soap.SOAPBody JavaDoc;
32 import javax.xml.soap.SOAPBodyElement JavaDoc;
33 import javax.xml.soap.SOAPConnection JavaDoc;
34 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
35 import javax.xml.soap.SOAPElement JavaDoc;
36 import javax.xml.soap.SOAPException JavaDoc;
37 import javax.xml.soap.SOAPFactory JavaDoc;
38 import javax.xml.soap.SOAPFault JavaDoc;
39 import javax.xml.soap.SOAPMessage JavaDoc;
40 import javax.xml.soap.SOAPPart JavaDoc;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.apache.juddi.IRegistry;
45 import org.apache.juddi.error.RegistryException;
46 import org.apache.juddi.proxy.Transport;
47 import org.apache.juddi.util.xml.XMLUtils;
48 import org.jboss.util.xml.DOMWriter;
49 import org.w3c.dom.Document JavaDoc;
50 import org.w3c.dom.Element JavaDoc;
51 import org.w3c.dom.NamedNodeMap JavaDoc;
52 import org.w3c.dom.Node JavaDoc;
53 import org.w3c.dom.NodeList JavaDoc;
54 import org.xml.sax.InputSource JavaDoc;
55
56 /**
57  * Transport based on SAAJ
58  *
59  * @author Anil Saldhana (anil@apache.org)
60  */

61 public class SaajTransport implements Transport
62 {
63     // private reference to the jUDDI logger
64
private static Log log = LogFactory.getLog(SaajTransport.class);
65     
66     /**
67      * @see Transport#send(org.w3c.dom.Element, java.net.URL)
68      */

69     public Element JavaDoc send(Element JavaDoc request, URL JavaDoc endpointURL)
70             throws RegistryException
71     {
72        String JavaDoc requestMessage = XMLUtils.toString(request);
73         log.debug("Request message:" + requestMessage);
74
75         Element JavaDoc response = null;
76         try
77         {
78            SOAPMessage JavaDoc message = this.createSOAPMessage(request);
79            //Make the SAAJ Call now
80
SOAPConnectionFactory JavaDoc soapConnectionFactory = SOAPConnectionFactory.newInstance();
81            SOAPConnection JavaDoc connection = soapConnectionFactory.createConnection();
82            SOAPMessage JavaDoc soapResponse = connection.call(message, endpointURL);
83            
84            SOAPBody JavaDoc soapBody = soapResponse.getSOAPBody();
85            boolean hasFault = soapBody.hasFault();
86            if(hasFault)
87            {
88               SOAPFault JavaDoc soapFault = soapBody.getFault();
89               String JavaDoc faultStr = soapFault.getFaultCode() + "::" + soapFault.getFaultString();
90               throw new RegistryException(faultStr);
91            }
92            response = getFirstChildElement(soapBody);
93         } catch (Exception JavaDoc ex)
94         {
95             log.error("Exception::",ex);
96             throw new RegistryException(ex);
97         }
98
99         log.debug("Response message:" + XMLUtils.getText(response));
100         return response;
101     }
102
103     /**
104      * @see Transport#send(java.lang.String, java.net.URL)
105      */

106     public String JavaDoc send(String JavaDoc request, URL JavaDoc endpointURL)
107             throws RegistryException
108     {
109         Element JavaDoc reqEl = getElement(request);
110         Element JavaDoc respEl = this.send(reqEl, endpointURL);
111         StringWriter JavaDoc sw = new StringWriter JavaDoc();
112
113         DOMWriter dw = new DOMWriter(sw);
114         dw.print(respEl);
115         return sw.toString();
116         //return XMLUtils.toString(respEl);
117
}
118     
119     // PRIVATE METHODS
120

121     private SOAPMessage JavaDoc createSOAPMessage(Element JavaDoc elem) throws Exception JavaDoc
122     {
123        String JavaDoc prefix = "uddi";
124        MessageFactory JavaDoc msgFactory = MessageFactory.newInstance();
125        SOAPFactory JavaDoc factory = SOAPFactory.newInstance();
126        
127        SOAPMessage JavaDoc message = msgFactory.createMessage();
128        message.getSOAPHeader().detachNode();
129        SOAPPart JavaDoc soapPart = message.getSOAPPart();
130        SOAPBody JavaDoc soapBody = soapPart.getEnvelope().getBody();
131        //Create the outer body element
132
String JavaDoc uddins = IRegistry.UDDI_V2_NAMESPACE;
133        Name JavaDoc bodyName = factory.createName(elem.getNodeName(),prefix,uddins);
134        SOAPBodyElement JavaDoc bodyElement = soapBody.addBodyElement(bodyName);
135        bodyElement.addNamespaceDeclaration(prefix,uddins);
136        appendAttributes(bodyElement, elem.getAttributes(), factory);
137        appendElements(bodyElement,elem.getChildNodes(), factory);
138        return message;
139     }
140     
141     private void appendAttributes(SOAPElement JavaDoc bodyElement, NamedNodeMap JavaDoc nnm,
142           SOAPFactory JavaDoc factory) throws SOAPException JavaDoc
143     {
144        int len = nnm != null ? nnm.getLength() : 0;
145        for (int i = 0; i < len; i++)
146        {
147            Node JavaDoc n = nnm.item(i);
148            String JavaDoc nodename = n.getNodeName();
149            String JavaDoc nodevalue = n.getNodeValue();
150            if("xmlns".equals(nodename))
151               continue;
152            //Special case: xml:lang
153
if("xml:lang".equals(nodename))
154            {
155               Name JavaDoc xmlLang = factory.createName("lang","xml",
156                     "http://www.w3.org/TR/REC-xml/");
157               bodyElement.addAttribute(xmlLang, nodevalue);
158            }
159            else
160                bodyElement.addAttribute(factory.createName(nodename), nodevalue);
161        }
162     }
163     
164     private void appendElements(SOAPElement JavaDoc bodyElement, NodeList JavaDoc nlist,
165           SOAPFactory JavaDoc factory)
166     throws SOAPException JavaDoc
167     {
168        String JavaDoc prefix = "uddi";
169        String JavaDoc uddins = IRegistry.UDDI_V2_NAMESPACE;
170        int len = nlist != null ? nlist.getLength() : 0;
171
172        for (int i = 0; i < len; i++)
173        {
174            Node JavaDoc node = nlist.item(i);
175            short nodeType = node != null ? node.getNodeType() : -100;
176            if (Node.ELEMENT_NODE == nodeType)
177            {
178               Element JavaDoc el = (Element JavaDoc)node;
179               Name JavaDoc name = factory.createName(el.getNodeName(), prefix,uddins);
180               SOAPElement JavaDoc attachedEl = bodyElement.addChildElement(name);
181               appendAttributes(attachedEl, el.getAttributes(), factory);
182               appendElements(attachedEl, el.getChildNodes(), factory);
183            } else if (nodeType == Node.TEXT_NODE)
184            {
185                bodyElement.addTextNode(node.getNodeValue());
186            }
187        }
188     }
189     
190
191     /**
192      * Get an Element given a string
193      *
194      * @param xmlFrag
195      * @return
196      */

197     private static Element JavaDoc getElement(String JavaDoc xmlFrag)
198     {
199         Document JavaDoc doc = null;
200         Element JavaDoc reqElement = null;
201         try
202         {
203             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
204             doc = factory.newDocumentBuilder().parse(new InputSource JavaDoc(new StringReader JavaDoc(xmlFrag)));
205             reqElement = doc.getDocumentElement();
206         } catch (Exception JavaDoc e)
207         {
208             log.error("Exception:",e);
209         }
210
211         return reqElement;
212     }
213
214     /**
215      * Return the first child element
216      *
217      * @param el
218      * @return
219      */

220     private Element JavaDoc getFirstChildElement(Element JavaDoc el)
221     {
222         return getFirstChildElement(el, null);
223     }
224
225     /**
226      * Return the first child element for a givenname
227      */

228     private Element JavaDoc getFirstChildElement(Element JavaDoc el, String JavaDoc tagName)
229     {
230         Element JavaDoc childEl = null;
231         NodeList JavaDoc nlist = el != null ? el.getChildNodes() : null;
232         int len = nlist != null ? nlist.getLength() : 0;
233         for (int i = 0; childEl == null && i < len; i++)
234         {
235             Node JavaDoc node = nlist.item(i);
236             if (node.getNodeType() == Node.ELEMENT_NODE)
237             {
238                 if (tagName == null || tagName.equals(node.getLocalName()))
239                     childEl = (Element JavaDoc) node;
240             }
241         }
242         String JavaDoc responseObtained = XMLUtils.toString(childEl);
243         log.debug("Response obtained="+responseObtained);
244         return childEl;
245     }
246 }
247
Popular Tags