1 22 package org.jboss.jaxr.juddi.transport; 23 24 import java.io.StringReader ; 25 import java.io.StringWriter ; 26 import java.net.URL ; 27 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.soap.MessageFactory ; 30 import javax.xml.soap.Name ; 31 import javax.xml.soap.SOAPBody ; 32 import javax.xml.soap.SOAPBodyElement ; 33 import javax.xml.soap.SOAPConnection ; 34 import javax.xml.soap.SOAPConnectionFactory ; 35 import javax.xml.soap.SOAPElement ; 36 import javax.xml.soap.SOAPException ; 37 import javax.xml.soap.SOAPFactory ; 38 import javax.xml.soap.SOAPFault ; 39 import javax.xml.soap.SOAPMessage ; 40 import javax.xml.soap.SOAPPart ; 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 ; 50 import org.w3c.dom.Element ; 51 import org.w3c.dom.NamedNodeMap ; 52 import org.w3c.dom.Node ; 53 import org.w3c.dom.NodeList ; 54 import org.xml.sax.InputSource ; 55 56 61 public class SaajTransport implements Transport 62 { 63 private static Log log = LogFactory.getLog(SaajTransport.class); 65 66 69 public Element send(Element request, URL endpointURL) 70 throws RegistryException 71 { 72 String requestMessage = XMLUtils.toString(request); 73 log.debug("Request message:" + requestMessage); 74 75 Element response = null; 76 try 77 { 78 SOAPMessage message = this.createSOAPMessage(request); 79 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 81 SOAPConnection connection = soapConnectionFactory.createConnection(); 82 SOAPMessage soapResponse = connection.call(message, endpointURL); 83 84 SOAPBody soapBody = soapResponse.getSOAPBody(); 85 boolean hasFault = soapBody.hasFault(); 86 if(hasFault) 87 { 88 SOAPFault soapFault = soapBody.getFault(); 89 String faultStr = soapFault.getFaultCode() + "::" + soapFault.getFaultString(); 90 throw new RegistryException(faultStr); 91 } 92 response = getFirstChildElement(soapBody); 93 } catch (Exception 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 106 public String send(String request, URL endpointURL) 107 throws RegistryException 108 { 109 Element reqEl = getElement(request); 110 Element respEl = this.send(reqEl, endpointURL); 111 StringWriter sw = new StringWriter (); 112 113 DOMWriter dw = new DOMWriter(sw); 114 dw.print(respEl); 115 return sw.toString(); 116 } 118 119 121 private SOAPMessage createSOAPMessage(Element elem) throws Exception 122 { 123 String prefix = "uddi"; 124 MessageFactory msgFactory = MessageFactory.newInstance(); 125 SOAPFactory factory = SOAPFactory.newInstance(); 126 127 SOAPMessage message = msgFactory.createMessage(); 128 message.getSOAPHeader().detachNode(); 129 SOAPPart soapPart = message.getSOAPPart(); 130 SOAPBody soapBody = soapPart.getEnvelope().getBody(); 131 String uddins = IRegistry.UDDI_V2_NAMESPACE; 133 Name bodyName = factory.createName(elem.getNodeName(),prefix,uddins); 134 SOAPBodyElement 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 bodyElement, NamedNodeMap nnm, 142 SOAPFactory factory) throws SOAPException 143 { 144 int len = nnm != null ? nnm.getLength() : 0; 145 for (int i = 0; i < len; i++) 146 { 147 Node n = nnm.item(i); 148 String nodename = n.getNodeName(); 149 String nodevalue = n.getNodeValue(); 150 if("xmlns".equals(nodename)) 151 continue; 152 if("xml:lang".equals(nodename)) 154 { 155 Name 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 bodyElement, NodeList nlist, 165 SOAPFactory factory) 166 throws SOAPException 167 { 168 String prefix = "uddi"; 169 String 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 node = nlist.item(i); 175 short nodeType = node != null ? node.getNodeType() : -100; 176 if (Node.ELEMENT_NODE == nodeType) 177 { 178 Element el = (Element )node; 179 Name name = factory.createName(el.getNodeName(), prefix,uddins); 180 SOAPElement 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 197 private static Element getElement(String xmlFrag) 198 { 199 Document doc = null; 200 Element reqElement = null; 201 try 202 { 203 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 204 doc = factory.newDocumentBuilder().parse(new InputSource (new StringReader (xmlFrag))); 205 reqElement = doc.getDocumentElement(); 206 } catch (Exception e) 207 { 208 log.error("Exception:",e); 209 } 210 211 return reqElement; 212 } 213 214 220 private Element getFirstChildElement(Element el) 221 { 222 return getFirstChildElement(el, null); 223 } 224 225 228 private Element getFirstChildElement(Element el, String tagName) 229 { 230 Element childEl = null; 231 NodeList 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 node = nlist.item(i); 236 if (node.getNodeType() == Node.ELEMENT_NODE) 237 { 238 if (tagName == null || tagName.equals(node.getLocalName())) 239 childEl = (Element ) node; 240 } 241 } 242 String responseObtained = XMLUtils.toString(childEl); 243 log.debug("Response obtained="+responseObtained); 244 return childEl; 245 } 246 } 247 | Popular Tags |