KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > Fault


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap;
59
60 import java.io.*;
61 import java.util.*;
62 import org.w3c.dom.*;
63 import org.apache.soap.util.*;
64 import org.apache.soap.util.xml.*;
65 import org.apache.soap.encoding.*;
66 import org.apache.soap.rpc.Parameter;
67 import org.apache.soap.rpc.RPCConstants;
68 import org.apache.soap.rpc.SOAPContext;
69
70 /**
71  * A <code>Fault</code> object represents the contents and semantics
72  * of a <code>&lt;SOAP-ENV:Fault&gt;</code> element.
73  *
74  * @author Matthew J. Duftler (duftler@us.ibm.com)
75  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
76  * @author Eric M. Dashofy (edashofy@ics.uci.edu)
77  * @author Kevin J. Mitchell (kevin.mitchell@xmls.com)
78  */

79 public class Fault
80 {
81   private String JavaDoc faultCode = null;
82   private String JavaDoc faultString = null;
83   private String JavaDoc faultActorURI = null;
84   private Vector detailEntries = null;
85   private Vector faultEntries = null;
86   private AttributeHandler attrHandler = new AttributeHandler();
87
88   public Fault() {}
89
90   public Fault(SOAPException _soapException)
91   {
92     faultCode = _soapException.getFaultCode();
93     faultString = _soapException.getMessage();
94   }
95
96   public void setAttribute(QName attrQName, String JavaDoc value)
97   {
98     attrHandler.setAttribute(attrQName, value);
99   }
100
101   public String JavaDoc getAttribute(QName attrQName)
102   {
103     return attrHandler.getAttribute(attrQName);
104   }
105
106   public void removeAttribute(QName attrQName)
107   {
108     attrHandler.removeAttribute(attrQName);
109   }
110
111   public void declareNamespace(String JavaDoc nsPrefix, String JavaDoc namespaceURI)
112   {
113     attrHandler.declareNamespace(nsPrefix, namespaceURI);
114   }
115
116   public void setFaultCode(String JavaDoc faultCode)
117   {
118     this.faultCode = faultCode;
119   }
120
121   public String JavaDoc getFaultCode()
122   {
123     return faultCode;
124   }
125
126   public void setFaultString(String JavaDoc faultString)
127   {
128     this.faultString = faultString;
129   }
130
131   public String JavaDoc getFaultString()
132   {
133     return faultString;
134   }
135
136   public void setFaultActorURI(String JavaDoc faultActorURI)
137   {
138     this.faultActorURI = faultActorURI;
139   }
140
141   public String JavaDoc getFaultActorURI()
142   {
143     return faultActorURI;
144   }
145
146   public void setDetailEntries(Vector detailEntries)
147   {
148     this.detailEntries = detailEntries;
149   }
150
151   public Vector getDetailEntries()
152   {
153     return detailEntries;
154   }
155
156   public void setFaultEntries(Vector faultEntries)
157   {
158     this.faultEntries = faultEntries;
159   }
160
161   public Vector getFaultEntries()
162   {
163     return faultEntries;
164   }
165
166   public void marshall(String JavaDoc inScopeEncStyle, Writer sink, NSStack nsStack,
167                        XMLJavaMappingRegistry xjmr, SOAPContext ctx)
168     throws IllegalArgumentException JavaDoc, IOException
169   {
170
171     attrHandler.populateNSStack(nsStack);
172
173     String JavaDoc faultCode = getFaultCode();
174     String JavaDoc faultString = getFaultString();
175     String JavaDoc faultActorURI = getFaultActorURI();
176     Vector detailEntries = getDetailEntries();
177     Vector faultEntries = getFaultEntries();
178
179     // Determine the prefix associated with the NS_URI_SOAP_ENV namespace URI.
180
String JavaDoc soapEnvNSPrefix = attrHandler.getUniquePrefixFromURI(
181       Constants.NS_URI_SOAP_ENV, Constants.NS_PRE_SOAP_ENV, nsStack);
182
183     // Generate the required <SOAP-ENV:Fault>, <faultcode> and
184
// <faultstring> elements.
185
sink.write('<' + soapEnvNSPrefix + ':' + Constants.ELEM_FAULT);
186
187     // Serialize any fault attributes.
188
attrHandler.marshall(sink, ctx);
189
190     sink.write('>' + StringUtils.lineSeparator +
191                '<' + Constants.ELEM_FAULT_CODE + '>' + faultCode +
192                "</" + Constants.ELEM_FAULT_CODE + '>' +
193                StringUtils.lineSeparator +
194                '<' + Constants.ELEM_FAULT_STRING + '>' + faultString +
195                "</" + Constants.ELEM_FAULT_STRING + '>' +
196                StringUtils.lineSeparator);
197
198     // Generate the <faultactor> element if a value is present.
199
if (faultActorURI != null)
200     {
201       sink.write('<' + Constants.ELEM_FAULT_ACTOR + '>' + faultActorURI +
202                  "</" + Constants.ELEM_FAULT_ACTOR + '>' +
203                  StringUtils.lineSeparator);
204     }
205
206     // If there are detail entries, generate the <detail> element,
207
// and serialize the detail entries.
208
if (detailEntries != null)
209     {
210       sink.write('<' + Constants.ELEM_DETAIL + '>' +
211                  StringUtils.lineSeparator);
212
213       // Serialize the detail entries within the <detail> element.
214
for (Enumeration e = detailEntries.elements(); e.hasMoreElements();)
215       {
216         Object JavaDoc detailEntry = e.nextElement();
217
218         // If the detail entry is an Element, just write it out.
219
if (detailEntry instanceof Element)
220         {
221           Element detailEntryEl = (Element)detailEntry;
222
223           Utils.marshallNode(detailEntryEl, sink);
224           sink.write(StringUtils.lineSeparator);
225         }
226         /*
227           If the detail entry is a Parameter, try to find a serializer.
228           If there is an error, write nothing.
229         */

230         else if (detailEntry instanceof Parameter)
231         {
232           try
233           {
234             Parameter detailEntryParameter = (Parameter)detailEntry;
235             Serializer s = xjmr.querySerializer(Parameter.class, inScopeEncStyle);
236
237             if (s != null)
238             {
239               s.marshall(null,
240                          Parameter.class,
241                          detailEntryParameter,
242                          Constants.ELEM_FAULT_DETAIL_ENTRY,
243                          sink,
244                          nsStack,
245                          xjmr,
246                          ctx);
247               sink.write(StringUtils.lineSeparator);
248             }
249             else
250             {
251               throw new IllegalArgumentException JavaDoc("Could not find Parameter " +
252                                                  "serializer.");
253             }
254           }
255           catch (IllegalArgumentException JavaDoc iae)
256           {
257           }
258         }
259       }
260
261       sink.write("</" + Constants.ELEM_DETAIL + '>' +
262                  StringUtils.lineSeparator);
263     }
264
265     // Serialize any fault entries (in addition to <faultcode>, <faultstring>,
266
// <faultactor>, and <detail>).
267
if (faultEntries != null)
268     {
269       for (Enumeration e = faultEntries.elements(); e.hasMoreElements();)
270       {
271         Element faultEntryEl = (Element)e.nextElement();
272
273         Utils.marshallNode(faultEntryEl, sink);
274
275         sink.write(StringUtils.lineSeparator);
276       }
277     }
278
279     sink.write("</" + soapEnvNSPrefix + ':' + Constants.ELEM_FAULT + '>' +
280                StringUtils.lineSeparator);
281
282     nsStack.popScope();
283   }
284
285   public static Fault unmarshall(String JavaDoc inScopeEncStyle, Node src,
286                                  XMLJavaMappingRegistry xjmr, SOAPContext ctx)
287     throws IllegalArgumentException JavaDoc
288   {
289     Element root = (Element)src;
290     Fault fault = new Fault();
291
292
293     if (Constants.Q_ELEM_FAULT.matches(root))
294     {
295       Element faultCodeEl = null;
296       Element faultStringEl = null;
297       Element faultActorEl = null;
298       Element detailEl = null;
299       Vector faultEntries = new Vector();
300       Element tempEl = DOMUtils.getFirstChildElement(root);
301
302       // Deserialize any fault attributes.
303
fault.attrHandler = AttributeHandler.unmarshall(root, ctx);
304
305       // Examine the subelements of the fault.
306
while (tempEl != null)
307       {
308         String JavaDoc namespaceURI = tempEl.getNamespaceURI();
309         String JavaDoc localPart = tempEl.getLocalName();
310
311         if (localPart == null)
312         {
313           localPart = tempEl.getTagName();
314         }
315
316         // SOAP-ENV namespace is ok, as is no namespace at all.
317
if (namespaceURI == null
318             || namespaceURI.equals(Constants.NS_URI_SOAP_ENV))
319         {
320           if (localPart.equals(Constants.ELEM_FAULT_CODE))
321           {
322             faultCodeEl = tempEl;
323           }
324           else if (localPart.equals(Constants.ELEM_FAULT_STRING))
325           {
326             faultStringEl = tempEl;
327           }
328           else if (localPart.equals(Constants.ELEM_FAULT_ACTOR))
329           {
330             faultActorEl = tempEl;
331           }
332           else if (localPart.equals(Constants.ELEM_DETAIL))
333           {
334             detailEl = tempEl;
335           }
336           else
337           {
338             // This must be an additional fault entry.
339
faultEntries.addElement(tempEl);
340           }
341         }
342         else
343         {
344           // This must be an additional fault entry.
345
faultEntries.addElement(tempEl);
346         }
347
348         tempEl = DOMUtils.getNextSiblingElement(tempEl);
349       }
350
351       // Deserialize the required <faultcode> element.
352
if (faultCodeEl != null)
353       {
354         String JavaDoc faultCode = DOMUtils.getChildCharacterData(faultCodeEl);
355
356         fault.setFaultCode(faultCode);
357       }
358       else
359       {
360         throw new IllegalArgumentException JavaDoc("A '" + Constants.Q_ELEM_FAULT +
361                                            "' element must contain a: '" +
362                                            Constants.ELEM_FAULT_CODE +
363                                            "' element.");
364       }
365
366       // Deserialize the required <faultstring> element.
367
if (faultStringEl != null)
368       {
369         String JavaDoc faultString = DOMUtils.getChildCharacterData(faultStringEl);
370
371         fault.setFaultString(faultString);
372       }
373       else
374       {
375         throw new IllegalArgumentException JavaDoc("A '" + Constants.Q_ELEM_FAULT +
376                                            "' element must contain a: '" +
377                                            Constants.ELEM_FAULT_STRING +
378                                            "' element.");
379       }
380
381       // Deserialize the <faultactor> element, if present.
382
if (faultActorEl != null)
383       {
384         String JavaDoc faultActorURI = DOMUtils.getChildCharacterData(faultActorEl);
385
386         fault.setFaultActorURI(faultActorURI);
387       }
388
389       // Deserialize any detail entries.
390
if (detailEl != null)
391       {
392         Vector detailEntries = new Vector();
393
394         for (Element el = DOMUtils.getFirstChildElement(detailEl);
395                      el != null;
396                      el = DOMUtils.getNextSiblingElement(el))
397         {
398           // Try to deserialize. If it fails, just add element to list.
399
try
400           {
401             String JavaDoc declEncStyle =
402               DOMUtils.getAttributeNS(el,
403                                       Constants.NS_URI_SOAP_ENV,
404                                       Constants.ATTR_ENCODING_STYLE);
405             String JavaDoc actualEncStyle = declEncStyle != null
406                                     ? declEncStyle
407                                     : inScopeEncStyle;
408             Bean paramBean = xjmr.unmarshall(declEncStyle,
409                                              RPCConstants.Q_ELEM_PARAMETER,
410                                              el,
411                                              ctx);
412             Parameter param = (Parameter)paramBean.value;
413
414             detailEntries.addElement(param);
415           }
416           catch (Exception JavaDoc e)
417           {
418             detailEntries.addElement(el);
419           }
420         }
421
422         fault.setDetailEntries(detailEntries);
423       }
424
425       // Set the faultEntries property, if any additional fault entries
426
// were encountered.
427
if (faultEntries.size() > 0)
428       {
429         fault.setFaultEntries(faultEntries);
430       }
431     }
432     else
433     {
434       throw new IllegalArgumentException JavaDoc("Root element of a SOAP Fault " +
435                                          "must be: '" +
436                                          Constants.Q_ELEM_FAULT + "'.");
437     }
438
439     return fault;
440   }
441
442   public String JavaDoc toString()
443   {
444     StringWriter sw = new StringWriter();
445     PrintWriter pw = new PrintWriter(sw);
446
447     pw.print("[Attributes=" + attrHandler + "] " +
448              "[faultCode=" + faultCode + "] " +
449              "[faultString=" + faultString + "] " +
450              "[faultActorURI=" + faultActorURI + "] " +
451              "[DetailEntries=");
452
453     if (detailEntries != null)
454     {
455       pw.println();
456
457       for (int i = 0; i < detailEntries.size(); i++)
458       {
459         Object JavaDoc detailEl = detailEntries.elementAt(i);
460
461         if (detailEl instanceof Parameter)
462         {
463           Parameter param = (Parameter)detailEl;
464
465           pw.println("[(" + i + ")=" + param +"]");
466         }
467         else
468         {
469           pw.println("[(" + i + ")=" +
470                      DOM2Writer.nodeToString((Element)detailEl) + "]");
471         }
472       }
473     }
474
475     pw.print("] [FaultEntries=");
476
477     if (faultEntries != null)
478     {
479       pw.println();
480
481       for (int i = 0; i < faultEntries.size(); i++)
482       {
483         pw.println("[(" + i + ")=" +
484                 DOM2Writer.nodeToString((Element)faultEntries.elementAt(i)) +
485                 "]");
486       }
487     }
488
489     pw.print("]");
490
491     return sw.toString();
492   }
493 }
494
Popular Tags