KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > SOAPFaultImpl


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

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

267    public void setFaultCode
268            (String JavaDoc
269            faultCode) throws SOAPException JavaDoc
270    {
271       fault.setFaultCodeAsString(faultCode);
272    }
273
274    /**
275     * Gets the fault code for this <CODE>SOAPFaultException</CODE>
276     * object.
277     *
278     * @return a <CODE>String</CODE> with the fault code
279     */

280    public String JavaDoc getFaultCode
281            ()
282    {
283       return fault.getFaultCode().getLocalPart();
284    }
285
286    /**
287     * Sets this <CODE>SOAPFaultException</CODE> object with the given
288     * fault actor.
289     * <p/>
290     * <P>The fault actor is the recipient in the message path who
291     * caused the fault to happen.</P>
292     *
293     * @param faultActor a <CODE>String</CODE>
294     * identifying the actor that caused this <CODE>
295     * SOAPFaultException</CODE> object
296     * @throws SOAPException if there was an error in
297     * adding the <CODE>faultActor</CODE> to the underlying XML
298     * tree.
299     */

300    public void setFaultActor
301            (String JavaDoc
302            faultActor) throws SOAPException JavaDoc
303    {
304       fault.setFaultActor(faultActor);
305    }
306
307    /**
308     * Gets the fault actor for this <CODE>SOAPFaultException</CODE>
309     * object.
310     *
311     * @return a <CODE>String</CODE> giving the actor in the message
312     * path that caused this <CODE>SOAPFaultException</CODE> object
313     * @see #setFaultActor(java.lang.String) setFaultActor(java.lang.String)
314     */

315    public String JavaDoc getFaultActor
316            ()
317    {
318       return fault.getFaultActor();
319    }
320
321    /**
322     * Sets the fault string for this <CODE>SOAPFaultException</CODE>
323     * object to the given string.
324     *
325     * @param faultString a <CODE>String</CODE>
326     * giving an explanation of the fault
327     * @throws SOAPException if there was an error in
328     * adding the <CODE>faultString</CODE> to the underlying XML
329     * tree.
330     * @see #getFaultString() getFaultString()
331     */

332    public void setFaultString
333            (String JavaDoc
334            faultString) throws SOAPException JavaDoc
335    {
336       fault.setFaultString(faultString);
337    }
338
339    /**
340     * Gets the fault string for this <CODE>SOAPFaultException</CODE>
341     * object.
342     *
343     * @return a <CODE>String</CODE> giving an explanation of the
344     * fault
345     */

346    public String JavaDoc getFaultString
347            ()
348    {
349       return fault.getFaultString();
350    }
351
352    /**
353     * Returns the detail element for this <CODE>SOAPFaultException</CODE>
354     * object.
355     * <p/>
356     * <P>A <CODE>Detail</CODE> object carries
357     * application-specific error information related to <CODE>
358     * SOAPBodyElement</CODE> objects.</P>
359     *
360     * @return a <CODE>Detail</CODE> object with
361     * application-specific error information
362     */

363    public javax.xml.soap.Detail JavaDoc getDetail
364            ()
365    {
366
367       List JavaDoc children = this.getChildren();
368       if (children == null || children.size() <= 0)
369          return null;
370
371       // find detail element
372
for (int i = 0; i < children.size(); i++)
373       {
374          Object JavaDoc obj = children.get(i);
375          if (obj instanceof javax.xml.soap.Detail JavaDoc)
376          {
377             return (javax.xml.soap.Detail JavaDoc)obj;
378          }
379       }
380       return null;
381    }
382
383    /**
384     * Creates a <CODE>Detail</CODE> object and sets it as the
385     * <CODE>Detail</CODE> object for this <CODE>SOAPFaultException</CODE>
386     * object.
387     * <p/>
388     * <P>It is illegal to add a detail when the fault already
389     * contains a detail. Therefore, this method should be called
390     * only after the existing detail has been removed.</P>
391     *
392     * @return the new <CODE>Detail</CODE> object
393     * @throws SOAPException if this
394     * <CODE>SOAPFaultException</CODE> object already contains a valid
395     * <CODE>Detail</CODE> object
396     */

397    public javax.xml.soap.Detail JavaDoc addDetail
398            () throws SOAPException JavaDoc
399    {
400       if (getDetail() != null)
401       {
402          throw new SOAPException JavaDoc(Messages.getMessage("valuePresent"));
403       }
404       DetailImpl detail = convertToDetail(fault);
405       addChildElement(detail);
406       return detail;
407    }
408
409    public void setFaultCode
410            (Name JavaDoc
411            faultCodeQName) throws SOAPException JavaDoc
412    {
413       String JavaDoc uri = faultCodeQName.getURI();
414       String JavaDoc local = faultCodeQName.getLocalName();
415       String JavaDoc prefix = faultCodeQName.getPrefix();
416
417       this.prefix = prefix;
418       QName JavaDoc qname = new QName JavaDoc(uri, local);
419       fault.setFaultCode(qname);
420    }
421
422    public Name JavaDoc getFaultCodeAsName
423            ()
424    {
425       QName JavaDoc qname = fault.getFaultCode();
426       String JavaDoc uri = qname.getNamespaceURI();
427       String JavaDoc local = qname.getLocalPart();
428       return new NameImpl(local, prefix, uri);
429    }
430
431    public void setFaultString
432            (String JavaDoc
433            faultString, Locale JavaDoc
434            locale) throws SOAPException JavaDoc
435    {
436       fault.setFaultString(faultString);
437       this.locale = locale;
438    }
439
440    public Locale JavaDoc getFaultStringLocale
441            ()
442    {
443       return locale;
444    }
445
446    /**
447     * Convert the details in an AxisFault to a Detail object
448     *
449     * @param fault source of the fault details
450     * @return a detail element contructed from the AxisFault details
451     * @throws SOAPException
452     */

453    private static DetailImpl convertToDetail
454            (AxisFault
455            fault)
456            throws SOAPException JavaDoc
457    {
458       DetailImpl detail = new DetailImpl(fault);
459       Element JavaDoc[] darray = fault.getFaultDetails();
460       for (int i = 0; i < darray.length; i++)
461       {
462          Element JavaDoc element = darray[i];
463          NameImpl name = new NameImpl(element.getLocalName(), element.getPrefix(), element.getNamespaceURI());
464          DetailEntry JavaDoc detailEntry = detail.addDetailEntry(name);
465          copyChildren(detailEntry, element);
466       }
467       return detail;
468    }
469
470    /**
471     * Copy the children of a DOM element to a SOAPElement.
472     *
473     * @param soapElement target of the copy
474     * @param domElement source for the copy
475     * @throws SOAPException
476     */

477    private static void copyChildren
478            (SOAPElement JavaDoc
479            soapElement, Element JavaDoc
480            domElement)
481            throws SOAPException JavaDoc
482    {
483       org.w3c.dom.NodeList JavaDoc nl = domElement.getChildNodes();
484       for (int j = 0; j < nl.getLength(); j++)
485       {
486          org.w3c.dom.Node JavaDoc childNode = nl.item(j);
487          if (childNode.getNodeType() == Node.TEXT_NODE)
488          {
489             soapElement.addTextNode(childNode.getNodeValue());
490             break; // only one text node assmed
491
}
492          if (childNode.getNodeType() == Node.ELEMENT_NODE)
493          {
494             String JavaDoc uri = childNode.getNamespaceURI();
495             SOAPElement JavaDoc childSoapElement = null;
496             if (uri == null)
497             {
498                childSoapElement = soapElement.addChildElement(childNode.getLocalName
499                        ());
500             }
501             else
502             {
503                childSoapElement = soapElement.addChildElement(childNode.getLocalName(),
504                        childNode.getPrefix(), uri);
505             }
506             copyChildren(childSoapElement, (Element JavaDoc)childNode);
507          }
508       }
509    }
510 }
511
Popular Tags