KickJava   Java API By Example, From Geeks To Geeks.

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


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, International Business Machines Corporation
5  * All Rights Reserved.
6  *
7  */

8
9 package org.uddi4j.transport;
10
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.util.Vector JavaDoc;
16 import javax.xml.parsers.DocumentBuilder JavaDoc;
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.Message;
22 import org.apache.axis.client.Call;
23 import org.apache.axis.client.Service;
24 import org.apache.axis.message.SOAPBodyElement;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 /**
30  * Transport implementation for Apache AXIS SOAP stack.
31  * <p>
32  * <b>
33  * Note: Axis does not support proxies on a per connection basis
34  * </b><p>
35  * Axis uses the JVM property support.
36  *
37  * @author David Melgar (dmelgar@us.ibm.com)
38  * @author Ozzy (ozzy@hursley.ibm.com)
39  */

40 public class ApacheAxisTransport extends TransportBase {
41     /**
42      * Sends a UDDIElement to URL.
43      *
44      * @param el UDDIElement to send
45      * @param url Destination URL
46      * @return An element representing a XML DOM tree containing the UDDI response.
47      * @exception TransportException
48      * Thrown if a problem occurs during transmission
49      */

50     public Element send(Element el, URL JavaDoc url) throws TransportException {
51
52         Element base = null;
53         boolean debug = logEnabled();
54         Call call = null;
55
56         try {
57             Service service = new Service();
58             call = (Call) service.createCall();
59
60             call.setUsername(System.getProperty("http.basicAuthUserName"));
61             call.setPassword(System.getProperty("http.basicAuthPassword"));
62
63             call.setTargetEndpointAddress(url);
64             //call.setProperty(HTTPConstants.MC_HTTP_SOAPACTION, "");
65

66             Vector JavaDoc result = null;
67
68             // Rebuild the body. This convoluted process lets Axis handle the level 1 DOM tree
69
// from UDDI4J. When UDDI4J moves to a level 2 DOM tree, the more obvious method
70
// can be tried.
71
String JavaDoc str = null;
72
73             str = org.apache.axis.utils.XMLUtils.ElementToString(el);
74             SOAPBodyElement body =
75                 new SOAPBodyElement(
76                     new java.io.ByteArrayInputStream JavaDoc(str.getBytes()));
77
78             // if DOM level 2 use this more obvious approach
79
// SOAPBodyElement body = new SOAPBodyElement(el);
80

81             Object JavaDoc[] params = new Object JavaDoc[] { body };
82
83             if (debug) {
84                 System.err.println("\nRequest message:\n" + params[0]);
85             }
86
87             result = (Vector JavaDoc) call.invoke(params);
88
89             base = stringToElement(((SOAPBodyElement) result.elementAt(0)).toString());
90         } catch (AxisFault fault) {
91             try {
92                 Message m = call.getResponseMessage();
93                 base = stringToElement(m.getSOAPEnvelope().getFirstBody().toString());
94             } catch (Exception JavaDoc e) {
95                 throw new TransportException(e);
96             }
97         } catch (Exception JavaDoc e) {
98             throw new TransportException(e);
99         }
100         if (debug && base != null) {
101             System.err.println(
102                 "\nResponse message:\n"
103                     + org.apache.axis.utils.XMLUtils.ElementToString(base));
104         }
105
106         return base;
107     }
108
109     public Element stringToElement(String JavaDoc s) throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
110         DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
111         docBuilderFactory.setNamespaceAware(true);
112         DocumentBuilder JavaDoc docBuilder = docBuilderFactory.newDocumentBuilder();
113         Document JavaDoc doc = docBuilder.parse(new ByteArrayInputStream JavaDoc(s.getBytes()));
114         return doc.getDocumentElement();
115      }
116
117 }
118
Popular Tags