1 7 package org.jboss.jaxr.juddi.axis; 8 9 15 16 import org.jboss.axis.AxisFault; 17 import org.jboss.axis.Message; 18 import org.jboss.axis.client.Call; 19 import org.jboss.axis.client.Service; 20 import org.jboss.axis.message.SOAPBodyElementAxisImpl; 21 import org.jboss.axis.utils.XMLUtils; 22 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.juddi.error.RegistryException; 27 import org.apache.juddi.proxy.Transport; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.xml.sax.SAXException ; 31 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import javax.xml.transform.TransformerFactory ; 36 import javax.xml.transform.Transformer ; 37 import javax.xml.transform.Result ; 38 import javax.xml.transform.dom.DOMSource ; 39 import javax.xml.transform.stream.StreamResult ; 40 import java.io.ByteArrayInputStream ; 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 import java.io.ByteArrayOutputStream ; 45 import java.net.URL ; 46 import java.util.Vector ; 47 import java.util.regex.Matcher ; 48 import java.util.regex.Pattern ; 49 50 56 public class JBossJuddiAxisTransport implements Transport 57 { 58 private static Log log = LogFactory.getLog(JBossJuddiAxisTransport.class); 60 61 public Element send(Element request, URL endpointURL) 62 throws RegistryException 63 { 64 return this.send(request, endpointURL, false, null, 0); 65 } 66 67 public Element send(Element request, URL endpointURL, boolean proxySet, String proxyHost, int proxyPort) 68 throws RegistryException 69 { 70 if (proxySet) 72 { 73 System.setProperty("http.proxySet", String.valueOf(proxySet)); 74 System.setProperty("http.proxyHost", proxyHost); 75 System.setProperty("http.proxyPort", String.valueOf(proxyPort)); 76 } 77 78 Service service = null; 79 Call call = null; 80 Element response = null; 81 82 log.debug("\nRequest message:\n" + XMLUtils.ElementToString(request)); 83 84 try 85 { 86 service = new Service(); 87 call = (Call)service.createCall(); 88 call.setTargetEndpointAddress(endpointURL); 89 90 String requestString = XMLUtils.ElementToString(request); 91 92 requestString = updateNS(requestString); 93 SOAPBodyElementAxisImpl body = 94 new SOAPBodyElementAxisImpl(new ByteArrayInputStream (requestString.getBytes("UTF-8"))); 95 Object [] soapBodies = new Object []{body}; 96 97 SOAPBodyElementAxisImpl res = null; 98 Vector result = null; 99 Object r = call.invoke(soapBodies); 100 if(r instanceof Vector ) 101 result = (Vector )r; 102 else 103 if( r instanceof SOAPBodyElementAxisImpl) 104 res = (SOAPBodyElementAxisImpl) r; 105 107 108 if(result!=null) res= (SOAPBodyElementAxisImpl)result.elementAt(0); 110 String retstr = res.getAsString(); 111 retstr = removeNS(retstr); 112 retstr = updateNS(retstr); 113 SOAPBodyElementAxisImpl updatedbody = 115 new SOAPBodyElementAxisImpl(new ByteArrayInputStream (retstr.getBytes("UTF-8"))); 116 response = updatedbody.getAsDOM(); 117 119 } 123 catch (AxisFault fault) 124 { 125 126 fault.printStackTrace(); 127 128 try 129 { 130 Message msg = call.getResponseMessage(); 131 response = msg.getSOAPEnvelope().getFirstBody().getAsDOM(); 132 } 133 catch (Exception ex) 134 { 135 throw new RegistryException(ex); 136 } 137 } 138 catch (Exception ex) 139 { 140 ex.printStackTrace(); 141 throw new RegistryException(ex); 142 } 143 144 log.debug("\nResponse message:\n" + XMLUtils.ElementToString(response)); 145 146 147 return response; 148 } 149 150 public String send(String request, URL endpointURL) 151 throws RegistryException 152 { 153 Element response = null; 154 try 155 { 156 response = this.send(parse(request), endpointURL); 157 } 158 catch (Exception ex) 159 { 160 ex.printStackTrace(); 161 throw new RegistryException(ex); 162 } 163 164 log.debug("\nResponse message:\n" + response); 165 166 return toString(response); 167 } 168 169 175 private String updateNS(String str) 176 { 177 str = replaceElements(str); 178 str = replaceJustElements(str); 179 180 return str; 181 } 182 183 189 private String replaceJustElements(String str) 190 { 191 Pattern pIn = Pattern.compile("<[a-zA-Z]*>"); 192 Matcher matcher = pIn.matcher(str); 193 194 StringBuffer buf = new StringBuffer (); 196 boolean found = false; 197 while ((found = matcher.find())) 198 { 199 String replaceStr = matcher.group(); 201 replaceStr = replaceStr.substring(0, replaceStr.length() - 1); 203 replaceStr = replaceStr + " xmlns=\"urn:uddi-org:api_v2\"> "; 205 206 matcher.appendReplacement(buf, replaceStr); 208 } 209 matcher.appendTail(buf); 210 211 return buf.toString(); 212 213 } 214 215 221 private String replaceElements(String str) 222 { 223 Pattern pIn = Pattern.compile("<[a-zA-Z]* "); 224 Matcher matcher = pIn.matcher(str); 225 226 StringBuffer buf = new StringBuffer (); 228 boolean found = false; 229 while ((found = matcher.find())) 230 { 231 String replaceStr = matcher.group(); 233 234 replaceStr = replaceStr + " xmlns=\"urn:uddi-org:api_v2\" "; 236 237 matcher.appendReplacement(buf, replaceStr); 239 } 240 matcher.appendTail(buf); 241 242 return buf.toString(); 243 244 } 245 246 253 private String removeNS(String str) 254 { 255 String ns = "xmlns=\"urn:uddi-org:api_v2\""; 256 String ns1 = "xmlns:xmlns=\"http://www.w3.org/2000/xmlns/\""; 257 str = str.replaceAll(ns, " "); 258 str = str.replaceAll(ns1, " "); 259 return str; 260 } 261 262 263 265 private Element parse(String xmlString) throws IOException 266 { 267 try 268 { 269 return parse(new ByteArrayInputStream (xmlString.getBytes())); 270 } 271 catch (IOException e) 272 { 273 log.error("Cannot parse: " + xmlString); 274 throw e; 275 } 276 } 277 278 DocumentBuilder builder = getDocumentBuilder(); 279 280 282 private DocumentBuilder getDocumentBuilder() 283 { 284 if (builder == null) 285 { 286 try 287 { 288 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 289 factory.setValidating(false); 290 factory.setNamespaceAware(true); 291 builder = factory.newDocumentBuilder(); 292 } 293 catch (ParserConfigurationException e) 294 { 295 log.error(e); 296 } 297 } 298 return builder; 299 } 300 301 303 private Element parse(InputStream xmlStream) throws IOException 304 { 305 try 306 { 307 Document doc = builder.parse(xmlStream); 308 Element root = doc.getDocumentElement(); 309 return root; 310 } 311 catch (SAXException e) 312 { 313 throw new IOException (e.toString()); 314 } 315 } 316 317 private void writeXML(Element element,OutputStream stream) 318 { 319 try { 320 TransformerFactory xformerFactory = TransformerFactory.newInstance(); 321 Transformer xformer = xformerFactory.newTransformer(); 322 Result output = new StreamResult (stream); 323 DOMSource source = new DOMSource (element); 324 325 xformer.transform(source,output); 327 } 328 catch(Exception ex) { 329 ex.printStackTrace(); 330 } 331 } 332 333 private String toString(Element element) 334 { 335 ByteArrayOutputStream stream = new ByteArrayOutputStream (); 336 writeXML(element,stream); 337 338 return stream.toString(); 339 } 340 } 341 | Popular Tags |