KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > uddi4j > transport > HPSOAPTransport


1 /*
2  * The source code contained herein is licensed under the IBM Public License
3  * Version 1.0, which has been approved by the Open Source Initiative.
4  * Copyright (C) 2001, Hewlett-Packard Company
5  * All Rights Reserved.
6  *
7  */

8
9 package org.uddi4j.transport;
10
11 import org.uddi4j.UDDIElement;
12 import java.net.URL JavaDoc;
13 import java.net.MalformedURLException JavaDoc;
14 import java.util.Vector JavaDoc;
15 import java.io.PrintStream JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Date JavaDoc;
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
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 /**
35  * Implementation for a SOAP transport to be used by UDDI4J.
36  * This uses HP SOAP as the underlying transport.
37  * The following system properties are used:
38  * <UL>
39  * <LI>org.uddi4j.TransportClassName = full class name of Transport
40  * implementation class. In this case, it is
41  * org.uddi4j.transport.HPSOAPTransport</LI>
42  * <LI>http.proxyHost = Hostname of http proxy</LI>
43  * <LI>https.proxyHost = Hostname of https proxy</LI>
44  * <LI>http.proxyPort = Portname of http proxy</LI>
45  * <LI>https.proxyPort = Portname of https proxy</LI>
46  * <LI>org.uddi4j.logEnabled = (true/false) Turns logging of messages on or
47  * off. This can be changed at the runtime, and will be reloaded.</LI>
48  * <LI>hpsoap.logFileName = name of the log file. In case this property is
49  * missing or there isnt write permission in the directory, log messages are sent
50  * to System.out. The file is created in case the file does not exist.</LI>
51  * <LI>hpsoap.logDirectory = directory containing the log file.
52  * In case this property is missing or a wrong directory is specified, the file
53  * is assumed to be in the current working directory. The directory is *NOT*
54  * created if it is not present.</LI>
55  * </UL>
56  *
57  * @author Ravi Trivedi (ravi_trivedi@hp.com)
58  */

59 public class HPSOAPTransport implements Transport {
60     boolean debug = true;
61     boolean useProxy = false;
62     URL JavaDoc httpProxy = null;
63     URL JavaDoc httpsProxy = null;
64     PrintStream JavaDoc print = System.out;
65     public SimpleDateFormat JavaDoc LOG_DATE_FORMAT =
66         new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
67
68
69     public HPSOAPTransport() {
70         File JavaDoc file = null;
71         String JavaDoc proxyHostString = System.getProperty("http.proxyHost");
72         String JavaDoc proxyPortString = System.getProperty("http.proxyPort");
73         String JavaDoc secureProxyHostString = System.getProperty("https.proxyHost");
74         String JavaDoc secureProxyPortString = System.getProperty("https.proxyPort");
75
76         try {
77             if ((proxyPortString != null) && (proxyHostString != null)) {
78                 httpProxy = new URL JavaDoc("http", proxyHostString,
79                     Integer.parseInt(proxyPortString), "");
80                 useProxy = true;
81             } else if ((secureProxyPortString != null)
82                          && (secureProxyHostString != null)) {
83                 httpsProxy = new URL JavaDoc("https", secureProxyHostString,
84                                       Integer.parseInt(secureProxyPortString), "");
85                 useProxy = true;
86             }
87         } catch(MalformedURLException JavaDoc mue) {
88             System.out.println("Invalid URL for proxy");
89             mue.printStackTrace();
90             useProxy = false;
91         }
92
93         try {
94             String JavaDoc isDebug = System.getProperty("org.uddi4j.logEnabled");
95             if ((isDebug != null) && (isDebug.equalsIgnoreCase("true"))) {
96                 debug = true;
97             } else {
98                 debug = false;
99             }
100         // Open the file even if logEnabled is false. This enables users
101
// to turn on debug mid-program.
102
String JavaDoc fileName = System.getProperty("hpsoap.logFileName");
103             String JavaDoc dirName = System.getProperty("hpsoap.logDirectory");
104             if ((fileName != null) && !(fileName.trim().equals (""))) {
105                if ((dirName != null) && !(dirName.trim().equals (""))) {
106                   File JavaDoc dir = new File JavaDoc(dirName);
107                   if (dir.exists()) {
108                          fileName = dirName + File.separator + fileName;
109                   }
110                }
111                print = new PrintStream JavaDoc(new FileOutputStream JavaDoc(fileName, true), true);
112                System.out.println ("Log file set to " + fileName + "\n");
113             } else { // fileName is null or empty
114
System.out.println ("Logging set to command window\n");
115             }
116         } catch (Exception JavaDoc e) {
117             e.printStackTrace();
118             print = System.out;
119         }
120     }
121
122
123     /**
124      * Sends a UDDIElement to either the inquiry or publish URL.
125      *
126      * @param el Element to send.
127      * @param inquiry Destination URL
128      * @return An element representing a XML DOM tree containing the UDDI
129      * response.
130      * @exception SOAPException
131      */

132     public Element send(UDDIElement el, URL JavaDoc url) throws TransportException {
133         Element base = null;
134
135         try {
136             DocumentBuilder JavaDoc docBuilder =
137                 DocumentBuilderFactory.newInstance().newDocumentBuilder();
138
139             base = docBuilder.newDocument().createElement("SOAP:Body");
140         } catch (Exception JavaDoc 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     /**
151      * Sends an XML DOM tree, identified by the given element, to either the
152      * inquiry or publish URL.
153      *
154      * @param el Element to send.
155      * @param inquiry Destination URL
156      * @return An element representing a XML DOM tree containing the UDDI
157      * response.
158      * @exception SOAPException
159      */

160     public Element send(Element el, URL JavaDoc url) throws TransportException {
161         Element base = null;
162         Node JavaDoc node = null;
163         SoapClient client = null;
164         ClientMessage response = null;
165         String JavaDoc 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 JavaDoc()) + "]\n");
179                 client.setLogStream(print);
180             } else {
181                 client.setLogStream(null);
182             }
183             ClientMessage request = new ClientMessage();
184
185             // Fix to remove the SOAP:encodingStyle namespace from the envelope.
186
// UDDI API Spec Version 2 Appendix B, section 6.3, specifies
187
// that UDDI does not support SOAP Encoding.
188
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 JavaDoc)(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 JavaDoc faultString = e.getMessage();
206             Element ele = null;
207
208             try {
209                 ele = XMLUtil.stringToElement(faultString);
210             } catch (Exception JavaDoc exe) {
211                 throw new TransportException(exe);
212             }
213             return ele;
214         } catch (IOException JavaDoc ioe) {
215             throw new TransportException(ioe);
216         }
217         return base;
218     }
219 }
220
Popular Tags