KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > uddi4j > UDDIException


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;
10
11 import java.util.StringTokenizer JavaDoc;
12
13 import org.uddi4j.response.DispositionReport;
14 import org.w3c.dom.Element JavaDoc;
15 import org.w3c.dom.Node JavaDoc;
16 import org.w3c.dom.NodeList JavaDoc;
17
18 /**
19  * Represents a UDDI defined error. This exception is thrown in cases
20  * where the return value on the UDDI API cannot indicate an error condition.<P>
21  *
22  * UDDIException usually contains a disposition report that has detailed
23  * information about the UDDI error, as defined by the UDDI specification.
24  * If the response is a SOAP fault, but does not contain a disposition
25  * report, this exception may still be thrown.<P>
26  *
27  * The DispositionReport class contains definitions for the various error values
28  * that can be returned.<P>
29  *
30  * @author David Melgar (dmelgar@us.ibm.com)
31  */

32 public class UDDIException extends Exception JavaDoc {
33
34    static final String JavaDoc UDDI_TAG = "Fault";
35
36    String JavaDoc faultCode = null;
37    String JavaDoc faultString = null;
38    String JavaDoc faultActor = null;
39    String JavaDoc detail = null;
40
41    Element JavaDoc detailElement= null;
42    DispositionReport dispositionReport = null;
43
44    public UDDIException() {
45    }
46
47    /**
48     * Constructer that parses the XML dom tree and extracts
49     * useful attributes.
50     *
51     * @param el Root element of the tree within the SOAP body.
52     */

53    public UDDIException(Element JavaDoc el, boolean createDispositionReport) {
54       if (isValidElement(el)) {
55          // Extract useful attributes
56
NodeList JavaDoc nl;
57          Element JavaDoc tmp;
58          nl = el.getElementsByTagName("faultcode");
59          if (nl.getLength()==0) { // Handle possible DOM level 2 response
60
nl = el.getElementsByTagNameNS(UDDIElement.SOAPNS, "faultcode");
61          }
62          if (nl!=null && nl.getLength()>0) {
63             tmp = (Element JavaDoc)nl.item(0);
64             faultCode = getText(tmp);
65          }
66          nl = el.getElementsByTagName("faultstring");
67          if (nl.getLength()==0) { // Handle possible DOM level 2 response
68
nl = el.getElementsByTagNameNS(UDDIElement.SOAPNS, "faultstring");
69          }
70          if (nl!=null && nl.getLength()>0) {
71             tmp = (Element JavaDoc)nl.item(0);
72             faultString = getText(tmp);
73          }
74          nl = el.getElementsByTagName("faultactor");
75          if (nl.getLength()==0) { // Handle possible DOM level 2 response
76
nl = el.getElementsByTagNameNS(UDDIElement.SOAPNS, "faultactor");
77          }
78          if (nl!=null && nl.getLength()>0) {
79             tmp = (Element JavaDoc)nl.item(0);
80             faultActor = getText(tmp);
81          }
82          nl = el.getElementsByTagName("detail");
83          if (nl.getLength()==0) { // Handle possible DOM level 2 response
84
nl = el.getElementsByTagNameNS(UDDIElement.SOAPNS, "detail");
85          }
86          // Try to create a disposition report
87
if (nl!=null && nl.getLength()>0) {
88             tmp = (Element JavaDoc)nl.item(0);
89             detailElement = tmp;
90             if (createDispositionReport) {
91                try {
92                   nl = el.getElementsByTagName(DispositionReport.UDDI_TAG);
93                   if (nl!=null && nl.getLength()>0) {
94                      tmp = (Element JavaDoc)nl.item(0);
95                      dispositionReport = new DispositionReport(tmp);
96                   }
97                } catch (UDDIException e) {
98                   // Ignore exception, we're handling it already
99
}
100             }
101          }
102       }
103    }
104
105    /**
106     * Tests the passed in element to determine if the
107     * element is a serialized version of this object.
108     *
109     * @param el Root element for this object
110     */

111    public static boolean isValidElement(Element JavaDoc el) {
112       // This method can be in the base class even if this class sets the value
113
// Handle soapFault if it returns a dom tree. Parse out values
114
// Fault DOES show up as a "Fault" element within the body
115
String JavaDoc name = el.getNodeName();
116       // Take care of the Namespaces qualifier being present
117
// (IDOOX server returns it).
118
StringTokenizer JavaDoc strtok = new StringTokenizer JavaDoc(name ,":");
119         String JavaDoc value = "";
120         while (strtok.hasMoreTokens()) {
121              value = strtok.nextToken();
122         }
123         return UDDI_TAG.equals(value);
124    }
125
126    // Getters
127
public String JavaDoc getFaultCode() {
128       return faultCode;
129    }
130
131    public String JavaDoc getFaultString() {
132       return faultString;
133    }
134
135    public String JavaDoc getFaultActor() {
136       return faultActor;
137    }
138
139    public String JavaDoc getDetail() {
140       return detail;
141    }
142
143    public Element JavaDoc getDetailElement() {
144       return detailElement;
145    }
146
147    public DispositionReport getDispositionReport() {
148       return dispositionReport;
149    }
150
151    /**
152     * Utility function.
153     * Returns text contained in child elements of the
154     * passed in element
155     *
156     * @param el Element
157     * @return java.lang.String
158     */

159    protected String JavaDoc getText(Node JavaDoc el) {
160       NodeList JavaDoc nl = el.getChildNodes();
161       String JavaDoc result = "";
162       for (int i = 0; i < nl.getLength(); i++) {
163          if (nl.item(i).getNodeType()==Element.TEXT_NODE) {
164             result += nl.item(i).getNodeValue();
165          }
166       }
167       // Trim result to remove whitespace
168
return result.trim();
169    }
170
171    /**
172    * Provide simple text exception message
173    * For full details, examine the dispositionReport object itself
174    * @return java.lang.String
175    */

176    public String JavaDoc toString() {
177       if (dispositionReport != null &&
178           dispositionReport.getResultVector()!=null &&
179           dispositionReport.getResultVector().size()>0)
180       {
181           org.uddi4j.response.Result result =
182             (org.uddi4j.response.Result)dispositionReport.getResultVector().elementAt(0);
183           if(result!=null)
184             return result.getErrInfo().getText();
185       }
186       return getFaultString();
187    }
188 }
189
Popular Tags