KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > message > SOAPFault


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.message;
17
18 import org.apache.axis.AxisFault;
19 import org.apache.axis.Constants;
20 import org.apache.axis.description.FaultDesc;
21 import org.apache.axis.description.OperationDesc;
22 import org.apache.axis.encoding.DeserializationContext;
23 import org.apache.axis.encoding.SerializationContext;
24 import org.apache.axis.soap.SOAPConstants;
25 import org.apache.axis.utils.Messages;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.helpers.AttributesImpl JavaDoc;
30
31 import javax.xml.namespace.QName JavaDoc;
32 import javax.xml.soap.DetailEntry JavaDoc;
33 import javax.xml.soap.Name JavaDoc;
34 import javax.xml.soap.SOAPElement JavaDoc;
35 import javax.xml.soap.SOAPException JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Locale JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 /** A Fault body element.
41  *
42  * @author Sam Ruby (rubys@us.ibm.com)
43  * @author Glen Daniels (gdaniels@apache.org)
44  * @author Tom Jordahl (tomj@macromedia.com)
45  */

46 public class SOAPFault extends SOAPBodyElement implements javax.xml.soap.SOAPFault JavaDoc
47 {
48     protected AxisFault fault;
49     protected String JavaDoc prefix;
50     private java.util.Locale JavaDoc locale;
51     protected Detail detail = null;
52     
53     public SOAPFault(String JavaDoc namespace, String JavaDoc localName, String JavaDoc prefix,
54                      Attributes JavaDoc attrs, DeserializationContext context)
55         throws AxisFault
56     {
57         super(namespace, localName, prefix, attrs, context);
58     }
59     
60     public SOAPFault(AxisFault fault)
61     {
62         this.fault = fault;
63     }
64     
65     public void outputImpl(SerializationContext context)
66             throws Exception JavaDoc
67     {
68         SOAPConstants soapConstants = context.getMessageContext() == null ?
69                                         SOAPConstants.SOAP11_CONSTANTS :
70                                         context.getMessageContext().getSOAPConstants();
71
72         namespaceURI = soapConstants.getEnvelopeURI();
73         name = Constants.ELEM_FAULT;
74         
75         context.registerPrefixForURI(prefix, soapConstants.getEnvelopeURI());
76         context.startElement(new QName JavaDoc(this.getNamespaceURI(),
77                                        this.getName()),
78                              attributes);
79         
80         // XXX - Can fault be anything but an AxisFault here?
81
if (fault instanceof AxisFault) {
82             AxisFault axisFault = fault;
83             if (axisFault.getFaultCode() != null) {
84                 // Do this BEFORE starting the element, so the prefix gets
85
// registered if needed.
86
if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
87                     String JavaDoc faultCode = context.qName2String(axisFault.getFaultCode());
88                     context.startElement(Constants.QNAME_FAULTCODE_SOAP12, null);
89                     context.startElement(Constants.QNAME_FAULTVALUE_SOAP12, null);
90                     context.writeSafeString(faultCode);
91                     context.endElement();
92                     QName JavaDoc[] subcodes = axisFault.getFaultSubCodes();
93                     if (subcodes != null) {
94                         for (int i = 0; i < subcodes.length; i++) {
95                             faultCode = context.qName2String(subcodes[i]);
96                             context.startElement(Constants.QNAME_FAULTSUBCODE_SOAP12, null);
97                             context.startElement(Constants.QNAME_FAULTVALUE_SOAP12, null);
98                             context.writeSafeString(faultCode);
99                             context.endElement();
100                         }
101
102                         for (int i = 0; i < subcodes.length; i++)
103                             context.endElement();
104
105                     }
106                     context.endElement();
107                 } else {
108                     String JavaDoc faultCode = context.qName2String(axisFault.getFaultCode());
109                     context.startElement(Constants.QNAME_FAULTCODE, null);
110                     context.writeSafeString(faultCode);
111                     context.endElement();
112                 }
113             }
114             
115             if (axisFault.getFaultString() != null) {
116                 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
117                     context.startElement(Constants.QNAME_FAULTREASON_SOAP12, null);
118                     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
119                     attrs.addAttribute("http://www.w3.org/XML/1998/namespace", "lang", "xml:lang", "CDATA", "en");
120                     context.startElement(Constants.QNAME_TEXT_SOAP12, attrs);
121                 } else
122                     context.startElement(Constants.QNAME_FAULTSTRING, null);
123                 context.writeSafeString(axisFault.getFaultString());
124                 context.endElement();
125                 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
126                     context.endElement();
127                 }
128             }
129             
130             if (axisFault.getFaultActor() != null) {
131                 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS)
132                     context.startElement(Constants.QNAME_FAULTROLE_SOAP12, null);
133                 else
134                     context.startElement(Constants.QNAME_FAULTACTOR, null);
135
136                 context.writeSafeString(axisFault.getFaultActor());
137                 context.endElement();
138             }
139
140             if (axisFault.getFaultNode() != null) {
141                 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
142                     context.startElement(Constants.QNAME_FAULTNODE_SOAP12, null);
143                     context.writeSafeString(axisFault.getFaultNode());
144                     context.endElement();
145                 }
146             }
147             
148             // get the QName for this faults detail element
149
QName JavaDoc qname = getFaultQName(fault.getClass(), context);
150             if (qname == null && fault.detail != null) {
151                 qname = getFaultQName(fault.detail.getClass(), context);
152             }
153             if (qname == null) {
154                 // not the greatest, but...
155
qname = new QName JavaDoc("", "faultData");
156             }
157             Element JavaDoc[] faultDetails = axisFault.getFaultDetails();
158             if (faultDetails != null) {
159                 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS)
160                     context.startElement(Constants.QNAME_FAULTDETAIL_SOAP12, null);
161                 else
162                     context.startElement(Constants.QNAME_FAULTDETAILS, null);
163
164                 // Allow the fault to write its data, if any
165
axisFault.writeDetails(qname, context);
166                 // Then output any other elements
167
for (int i = 0; i < faultDetails.length; i++) {
168                     context.writeDOMElement(faultDetails[i]);
169                 }
170
171                 if (detail!= null) {
172                     for (Iterator JavaDoc it = detail.getChildren().iterator(); it.hasNext();) {
173                         ((NodeImpl)it.next()).output(context);
174                     }
175                 }
176         
177                 context.endElement();
178             }
179         }
180         
181         context.endElement();
182     }
183
184     private QName JavaDoc getFaultQName(Class JavaDoc cls, SerializationContext context) {
185         QName JavaDoc qname = null;
186         if (! cls.equals(AxisFault.class)) {
187             FaultDesc faultDesc = null;
188             if (context.getMessageContext() != null) {
189                 OperationDesc op = context.getMessageContext().getOperation();
190                 if (op != null) {
191                     faultDesc = op.getFaultByClass(cls);
192                 }
193             }
194                 
195             if (faultDesc != null) {
196                 qname = faultDesc.getQName();
197             }
198         }
199         return qname;
200     }
201
202     public AxisFault getFault()
203     {
204         return fault;
205     }
206     
207     public void setFault(AxisFault fault)
208     {
209         this.fault = fault;
210     }
211     
212     /**
213      * Sets this <CODE>SOAPFaultException</CODE> object with the given
214      * fault code.
215      *
216      * <P>Fault codes, which given information about the fault,
217      * are defined in the SOAP 1.1 specification.</P>
218      * @param faultCode a <CODE>String</CODE> giving
219      * the fault code to be set; must be one of the fault codes
220      * defined in the SOAP 1.1 specification
221      * @throws SOAPException if there was an error in
222      * adding the <CODE>faultCode</CODE> to the underlying XML
223      * tree.
224      */

225     public void setFaultCode(String JavaDoc faultCode) throws SOAPException JavaDoc {
226         fault.setFaultCodeAsString(faultCode);
227     }
228     
229     /**
230      * Gets the fault code for this <CODE>SOAPFaultException</CODE>
231      * object.
232      * @return a <CODE>String</CODE> with the fault code
233      */

234     public String JavaDoc getFaultCode() {
235         return fault.getFaultCode().getLocalPart();
236     }
237     
238     /**
239      * Sets this <CODE>SOAPFaultException</CODE> object with the given
240      * fault actor.
241      *
242      * <P>The fault actor is the recipient in the message path who
243      * caused the fault to happen.</P>
244      * @param faultActor a <CODE>String</CODE>
245      * identifying the actor that caused this <CODE>
246      * SOAPFaultException</CODE> object
247      * @throws SOAPException if there was an error in
248      * adding the <CODE>faultActor</CODE> to the underlying XML
249      * tree.
250      */

251     public void setFaultActor(String JavaDoc faultActor) throws SOAPException JavaDoc {
252         fault.setFaultActor(faultActor);
253     }
254     
255     /**
256      * Gets the fault actor for this <CODE>SOAPFaultException</CODE>
257      * object.
258      * @return a <CODE>String</CODE> giving the actor in the message
259      * path that caused this <CODE>SOAPFaultException</CODE> object
260      * @see #setFaultActor(java.lang.String) setFaultActor(java.lang.String)
261      */

262     public String JavaDoc getFaultActor() {
263         return fault.getFaultActor();
264     }
265     
266     /**
267      * Sets the fault string for this <CODE>SOAPFaultException</CODE>
268      * object to the given string.
269      *
270      * @param faultString a <CODE>String</CODE>
271      * giving an explanation of the fault
272      * @throws SOAPException if there was an error in
273      * adding the <CODE>faultString</CODE> to the underlying XML
274      * tree.
275      * @see #getFaultString() getFaultString()
276      */

277     public void setFaultString(String JavaDoc faultString) throws SOAPException JavaDoc {
278         fault.setFaultString(faultString);
279     }
280     
281     /**
282      * Gets the fault string for this <CODE>SOAPFaultException</CODE>
283      * object.
284      * @return a <CODE>String</CODE> giving an explanation of the
285      * fault
286      */

287     public String JavaDoc getFaultString() {
288         return fault.getFaultString();
289     }
290     
291     /**
292      * Returns the detail element for this <CODE>SOAPFaultException</CODE>
293      * object.
294      *
295      * <P>A <CODE>Detail</CODE> object carries
296      * application-specific error information related to <CODE>
297      * SOAPBodyElement</CODE> objects.</P>
298      * @return a <CODE>Detail</CODE> object with
299      * application-specific error information
300      */

301     public javax.xml.soap.Detail JavaDoc getDetail() {
302         List JavaDoc children = this.getChildren();
303         if(children==null || children.size()<=0)
304             return null;
305
306         // find detail element
307
for (int i=0; i < children.size(); i++) {
308             Object JavaDoc obj = children.get(i);
309             if (obj instanceof javax.xml.soap.Detail JavaDoc) {
310                 return (javax.xml.soap.Detail JavaDoc) obj;
311             }
312         }
313         return null;
314     }
315     
316     /**
317      * Creates a <CODE>Detail</CODE> object and sets it as the
318      * <CODE>Detail</CODE> object for this <CODE>SOAPFaultException</CODE>
319      * object.
320      *
321      * <P>It is illegal to add a detail when the fault already
322      * contains a detail. Therefore, this method should be called
323      * only after the existing detail has been removed.</P>
324      * @return the new <CODE>Detail</CODE> object
325      * @throws SOAPException if this
326      * <CODE>SOAPFaultException</CODE> object already contains a valid
327      * <CODE>Detail</CODE> object
328      */

329     public javax.xml.soap.Detail JavaDoc addDetail() throws SOAPException JavaDoc {
330         if(getDetail() != null){
331             throw new SOAPException JavaDoc(Messages.getMessage("valuePresent"));
332         }
333         Detail detail = convertToDetail(fault);
334         addChildElement(detail);
335         return detail;
336     }
337
338     public void setFaultCode(Name JavaDoc faultCodeQName) throws SOAPException JavaDoc {
339         String JavaDoc uri = faultCodeQName.getURI();
340         String JavaDoc local = faultCodeQName.getLocalName();
341         String JavaDoc prefix = faultCodeQName.getPrefix();
342
343         this.prefix = prefix;
344         QName JavaDoc qname = new QName JavaDoc(uri,local);
345         fault.setFaultCode(qname);
346     }
347
348     public Name JavaDoc getFaultCodeAsName() {
349         QName JavaDoc qname = fault.getFaultCode();
350         String JavaDoc uri = qname.getNamespaceURI();
351         String JavaDoc local = qname.getLocalPart();
352         return new PrefixedQName(uri, local, prefix);
353     }
354
355     public void setFaultString(String JavaDoc faultString, Locale JavaDoc locale) throws SOAPException JavaDoc {
356         fault.setFaultString(faultString);
357         this.locale = locale;
358     }
359
360     public Locale JavaDoc getFaultStringLocale() {
361         return locale;
362     }
363
364     /**
365      * Convert the details in an AxisFault to a Detail object
366      *
367      * @param fault source of the fault details
368      * @return a detail element contructed from the AxisFault details
369      * @throws SOAPException
370      */

371     private Detail convertToDetail(AxisFault fault)
372             throws SOAPException JavaDoc
373     {
374         detail = new Detail();
375         Element JavaDoc[] darray = fault.getFaultDetails();
376         fault.setFaultDetail(new Element JavaDoc[]{});
377         for (int i = 0; i < darray.length; i++)
378         {
379             Element JavaDoc detailtEntryElem = darray[i];
380             DetailEntry detailEntry = detail.addDetailEntry(
381                     new PrefixedQName(detailtEntryElem.getNamespaceURI(),
382                             detailtEntryElem.getLocalName(), detailtEntryElem.getPrefix()));
383             copyChildren(detailEntry, detailtEntryElem);
384         }
385         return detail;
386     }
387
388     /**
389      * Copy the children of a DOM element to a SOAPElement.
390      *
391      * @param soapElement target of the copy
392      * @param domElement source for the copy
393      * @throws SOAPException
394      */

395     private static void copyChildren(SOAPElement JavaDoc soapElement, Element JavaDoc domElement)
396             throws SOAPException JavaDoc
397     {
398         org.w3c.dom.NodeList JavaDoc nl = domElement.getChildNodes();
399         for (int j = 0; j < nl.getLength(); j++)
400         {
401             org.w3c.dom.Node JavaDoc childNode = nl.item(j);
402             if (childNode.getNodeType() == Node.TEXT_NODE)
403             {
404                 soapElement.addTextNode(childNode.getNodeValue());
405                 break; // only one text node assmed
406
}
407             if (childNode.getNodeType() == Node.ELEMENT_NODE)
408             {
409                 String JavaDoc uri = childNode.getNamespaceURI();
410                 SOAPElement JavaDoc childSoapElement = null;
411                 if (uri == null)
412                 {
413                     childSoapElement = soapElement.addChildElement(childNode.getLocalName
414                             ());
415                 }
416                 else
417                 {
418                     childSoapElement = soapElement.addChildElement(
419                             childNode.getLocalName(),
420                             childNode.getPrefix(), uri);
421                 }
422                 copyChildren(childSoapElement, (Element JavaDoc) childNode);
423             }
424         }
425     }
426 }
427
Popular Tags