1 8 9 package org.uddi4j.transport; 10 11 import org.uddi4j.UDDIElement; 12 import java.net.URL ; 13 import java.net.MalformedURLException ; 14 import java.util.Vector ; 15 import java.io.PrintStream ; 16 import java.io.FileOutputStream ; 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.text.SimpleDateFormat ; 20 import java.util.Date ; 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.uddi4j.UDDIException; 27 import com.hp.soap.client.SoapClient; 28 import com.hp.soap.client.ClientMessage; 29 import com.hp.soap.client.SoapBody; 30 import com.hp.soap.client.SoapException; 31 import com.hp.soap.xml.XMLUtil; 32 33 34 59 public class HPSOAPTransport implements Transport { 60 boolean debug = true; 61 boolean useProxy = false; 62 URL httpProxy = null; 63 URL httpsProxy = null; 64 PrintStream print = System.out; 65 public SimpleDateFormat LOG_DATE_FORMAT = 66 new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 67 68 69 public HPSOAPTransport() { 70 File file = null; 71 String proxyHostString = System.getProperty("http.proxyHost"); 72 String proxyPortString = System.getProperty("http.proxyPort"); 73 String secureProxyHostString = System.getProperty("https.proxyHost"); 74 String secureProxyPortString = System.getProperty("https.proxyPort"); 75 76 try { 77 if ((proxyPortString != null) && (proxyHostString != null)) { 78 httpProxy = new URL ("http", proxyHostString, 79 Integer.parseInt(proxyPortString), ""); 80 useProxy = true; 81 } else if ((secureProxyPortString != null) 82 && (secureProxyHostString != null)) { 83 httpsProxy = new URL ("https", secureProxyHostString, 84 Integer.parseInt(secureProxyPortString), ""); 85 useProxy = true; 86 } 87 } catch(MalformedURLException mue) { 88 System.out.println("Invalid URL for proxy"); 89 mue.printStackTrace(); 90 useProxy = false; 91 } 92 93 try { 94 String isDebug = System.getProperty("org.uddi4j.logEnabled"); 95 if ((isDebug != null) && (isDebug.equalsIgnoreCase("true"))) { 96 debug = true; 97 } else { 98 debug = false; 99 } 100 String fileName = System.getProperty("hpsoap.logFileName"); 103 String dirName = System.getProperty("hpsoap.logDirectory"); 104 if ((fileName != null) && !(fileName.trim().equals (""))) { 105 if ((dirName != null) && !(dirName.trim().equals (""))) { 106 File dir = new File (dirName); 107 if (dir.exists()) { 108 fileName = dirName + File.separator + fileName; 109 } 110 } 111 print = new PrintStream (new FileOutputStream (fileName, true), true); 112 System.out.println ("Log file set to " + fileName + "\n"); 113 } else { System.out.println ("Logging set to command window\n"); 115 } 116 } catch (Exception e) { 117 e.printStackTrace(); 118 print = System.out; 119 } 120 } 121 122 123 132 public Element send(UDDIElement el, URL url) throws TransportException { 133 Element base = null; 134 135 try { 136 DocumentBuilder docBuilder = 137 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 138 139 base = docBuilder.newDocument().createElement("SOAP:Body"); 140 } catch (Exception e) { 141 e.printStackTrace(); 142 print.println("FATAL error : bailing out"); 143 System.exit(-1); 144 } 145 el.saveToXML(base); 146 return send((Element)base, url); 147 } 148 149 150 160 public Element send(Element el, URL url) throws TransportException { 161 Element base = null; 162 Node node = null; 163 SoapClient client = null; 164 ClientMessage response = null; 165 String trace = System.getProperty("org.uddi4j.logEnabled"); 166 167 try { 168 if (useProxy) { 169 if (url.getProtocol().equalsIgnoreCase("HTTP")) { 170 client = new SoapClient(url, httpProxy); 171 } else { 172 client = new SoapClient(url, httpsProxy); 173 } 174 } else { 175 client = new SoapClient(url); 176 } 177 if ((trace != null) && (trace.equalsIgnoreCase("true"))) { 178 print.println("[" + LOG_DATE_FORMAT.format(new Date ()) + "]\n"); 179 client.setLogStream(print); 180 } else { 181 client.setLogStream(null); 182 } 183 ClientMessage request = new ClientMessage(); 184 185 request.getEnvelope().removeAttribute("SOAP:encodingStyle"); 189 SoapBody body = new SoapBody(el); 190 191 request.setBody(body); 192 response = client.sendRequest(request); 193 if (response != null) { 194 if (response.getBody() != null) { 195 node = 196 ((Node )(response.getBody().getElement()).getFirstChild()); 197 } else { 198 print.println("INFO:The SOAP Body of the response obtained from Server is null"); 199 } 200 base = (Element)node; 201 } else { 202 print.println("INFO:The SOAP Response from Server is null"); 203 } 204 } catch (SoapException e) { 205 String faultString = e.getMessage(); 206 Element ele = null; 207 208 try { 209 ele = XMLUtil.stringToElement(faultString); 210 } catch (Exception exe) { 211 throw new TransportException(exe); 212 } 213 return ele; 214 } catch (IOException ioe) { 215 throw new TransportException(ioe); 216 } 217 return base; 218 } 219 } 220 | Popular Tags |