KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jaxr > juddi > axis > JBossJuddiAxisTransport


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jaxr.juddi.axis;
8
9 /*import org.apache.axis.AxisFault;
10 import org.apache.axis.Message;
11 import org.apache.axis.client.Call;
12 import org.apache.axis.client.Service;
13 import org.apache.axis.message.SOAPBodyElementAxisImpl;
14 import org.apache.axis.utils.XMLUtils; */

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 JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import javax.xml.transform.TransformerFactory JavaDoc;
36 import javax.xml.transform.Transformer JavaDoc;
37 import javax.xml.transform.Result JavaDoc;
38 import javax.xml.transform.dom.DOMSource JavaDoc;
39 import javax.xml.transform.stream.StreamResult JavaDoc;
40 import java.io.ByteArrayInputStream JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.ByteArrayOutputStream JavaDoc;
45 import java.net.URL JavaDoc;
46 import java.util.Vector JavaDoc;
47 import java.util.regex.Matcher JavaDoc;
48 import java.util.regex.Pattern JavaDoc;
49
50 /**
51  * Overrides the AxisTransport class used by jUDDI
52  *
53  * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
54  * @since Nov 20, 2004
55  */

56 public class JBossJuddiAxisTransport implements Transport
57 {
58    // private reference to the jUDDI logger
59
private static Log log = LogFactory.getLog(JBossJuddiAxisTransport.class);
60
61    public Element JavaDoc send(Element JavaDoc request, URL JavaDoc endpointURL)
62            throws RegistryException
63    {
64       return this.send(request, endpointURL, false, null, 0);
65    }
66
67    public Element JavaDoc send(Element JavaDoc request, URL JavaDoc endpointURL, boolean proxySet, String JavaDoc proxyHost, int proxyPort)
68            throws RegistryException
69    {
70       // make sure we're using the correct proxy
71
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 JavaDoc 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 JavaDoc requestString = XMLUtils.ElementToString(request);
91          /** Hack for Jboss axis-ws4ee */
92          requestString = updateNS(requestString);
93          SOAPBodyElementAxisImpl body =
94                  new SOAPBodyElementAxisImpl(new ByteArrayInputStream JavaDoc(requestString.getBytes("UTF-8")));
95          Object JavaDoc[] soapBodies = new Object JavaDoc[]{body};
96
97          SOAPBodyElementAxisImpl res = null;
98          Vector JavaDoc result = null;
99          Object JavaDoc r = call.invoke(soapBodies);
100          if(r instanceof Vector JavaDoc)
101              result = (Vector JavaDoc)r;
102          else
103          if( r instanceof SOAPBodyElementAxisImpl)
104            res = (SOAPBodyElementAxisImpl) r;
105          // response = ((SOAPBodyElementAxisImpl) result.elementAt(0)).getAsDOM();
106

107
108          //Need to Hack again for namespaces for JBoss axis-wsee
109
if(result!=null) res= (SOAPBodyElementAxisImpl)result.elementAt(0);
110          String JavaDoc retstr = res.getAsString();
111          retstr = removeNS(retstr);
112          retstr = updateNS(retstr);
113          //System.out.println(retstr);
114
SOAPBodyElementAxisImpl updatedbody =
115                  new SOAPBodyElementAxisImpl(new ByteArrayInputStream JavaDoc(retstr.getBytes("UTF-8")));
116          response = updatedbody.getAsDOM();
117          //System.out.println(response.getNodeName());
118

119          //if(res!=null)
120
//System.out.println(res.getAsString());
121
//response = res.getAsDOM();
122
}
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 JavaDoc ex)
134          {
135             throw new RegistryException(ex);
136          }
137       }
138       catch (Exception JavaDoc 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 JavaDoc send(String JavaDoc request, URL JavaDoc endpointURL)
151            throws RegistryException
152    {
153       Element JavaDoc response = null;
154       try
155       {
156          response = this.send(parse(request), endpointURL);
157       }
158       catch (Exception JavaDoc 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    /**
170     * A Hack for JBoss axis-ws4ee
171     *
172     * @param str
173     * @return
174     */

175    private String JavaDoc updateNS(String JavaDoc str)
176    {
177       str = replaceElements(str);
178       str = replaceJustElements(str);
179
180       return str;
181    }
182
183    /**
184     * Update namespace for elements only (no attribs)
185     *
186     * @param str
187     * @return
188     */

189    private String JavaDoc replaceJustElements(String JavaDoc str)
190    {
191       Pattern JavaDoc pIn = Pattern.compile("<[a-zA-Z]*>");
192       Matcher JavaDoc matcher = pIn.matcher(str);
193
194       // Replace all occurrences of pattern in input
195
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
196       boolean found = false;
197       while ((found = matcher.find()))
198       {
199          // Get the match result
200
String JavaDoc replaceStr = matcher.group();
201          replaceStr = replaceStr.substring(0, replaceStr.length() - 1); //Chop off last char
202

203          // prepare replacement string
204
replaceStr = replaceStr + " xmlns=\"urn:uddi-org:api_v2\"> ";
205
206          // Insert replacement
207
matcher.appendReplacement(buf, replaceStr);
208       }
209       matcher.appendTail(buf);
210
211       return buf.toString();
212
213    }
214
215    /**
216     * Update namespace for elements with attribs
217     *
218     * @param str
219     * @return
220     */

221    private String JavaDoc replaceElements(String JavaDoc str)
222    {
223       Pattern JavaDoc pIn = Pattern.compile("<[a-zA-Z]* ");
224       Matcher JavaDoc matcher = pIn.matcher(str);
225
226       // Replace all occurrences of pattern in input
227
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
228       boolean found = false;
229       while ((found = matcher.find()))
230       {
231          // Get the match result
232
String JavaDoc replaceStr = matcher.group();
233
234          // prepare replacement string
235
replaceStr = replaceStr + " xmlns=\"urn:uddi-org:api_v2\" ";
236
237          // Insert replacement
238
matcher.appendReplacement(buf, replaceStr);
239       }
240       matcher.appendTail(buf);
241
242       return buf.toString();
243
244    }
245
246    /**
247     * Axis 1.1 seems to have a bug wherein it sets the namespace
248     * xmlns:xmlns and the parser will complain if present.
249     *
250     * @param str
251     * @return
252     */

253    private String JavaDoc removeNS(String JavaDoc str)
254    {
255       String JavaDoc ns = "xmlns=\"urn:uddi-org:api_v2\"";
256       String JavaDoc 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    /** Parse the given XML string and return the root Element
264     */

265    private Element JavaDoc parse(String JavaDoc xmlString) throws IOException JavaDoc
266    {
267       try
268       {
269          return parse(new ByteArrayInputStream JavaDoc(xmlString.getBytes()));
270       }
271       catch (IOException JavaDoc e)
272       {
273          log.error("Cannot parse: " + xmlString);
274          throw e;
275       }
276    }
277
278    DocumentBuilder JavaDoc builder = getDocumentBuilder();
279
280    /** Initialise the the DocumentBuilder
281     */

282    private DocumentBuilder JavaDoc getDocumentBuilder()
283    {
284       if (builder == null)
285       {
286          try
287          {
288             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
289             factory.setValidating(false);
290             factory.setNamespaceAware(true);
291             builder = factory.newDocumentBuilder();
292          }
293          catch (ParserConfigurationException JavaDoc e)
294          {
295             log.error(e);
296          }
297       }
298       return builder;
299    }
300
301    /** Parse the given XML stream and return the root Element
302     */

303    private Element JavaDoc parse(InputStream JavaDoc xmlStream) throws IOException JavaDoc
304    {
305       try
306       {
307          Document JavaDoc doc = builder.parse(xmlStream);
308          Element JavaDoc root = doc.getDocumentElement();
309          return root;
310       }
311       catch (SAXException JavaDoc e)
312       {
313          throw new IOException JavaDoc(e.toString());
314       }
315    }
316
317    private void writeXML(Element JavaDoc element,OutputStream JavaDoc stream)
318   {
319     try {
320       TransformerFactory JavaDoc xformerFactory = TransformerFactory.newInstance();
321       Transformer JavaDoc xformer = xformerFactory.newTransformer();
322       Result JavaDoc output = new StreamResult JavaDoc(stream);
323       DOMSource JavaDoc source = new DOMSource JavaDoc(element);
324
325       // print the xml to the specified OutputStream
326
xformer.transform(source,output);
327     }
328     catch(Exception JavaDoc ex) {
329       ex.printStackTrace();
330     }
331   }
332
333   private String JavaDoc toString(Element JavaDoc element)
334   {
335       ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
336       writeXML(element,stream);
337
338       return stream.toString();
339   }
340 }
341
Popular Tags