1/*
2 * Copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 * associated documentation files (the "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
8 * following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in all copies or substantial
11 * portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
14 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
15 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
16 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
17 * USE OR OTHER DEALINGS IN THE SOFTWARE.
18 */
19
20package org.ksoap2;
21
22import java.io.IOException;
23
24import org.ksoap2.kdom.Node;
25import org.xmlpull.v1.XmlPullParser;
26import org.xmlpull.v1.XmlPullParserException;
27import org.xmlpull.v1.XmlSerializer;
28
29/**
30 * Exception class encapsulating SOAP Faults
31 */
32
33public class SoapFault extends IOException {
34
35    private static final long serialVersionUID = 1011001L;
36    /** The SOAP fault code */
37    public String faultcode;
38    /** The SOAP fault code */
39    public String faultstring;
40    /** The SOAP fault code */
41    public String faultactor;
42    /** A KDom Node holding the details of the fault */
43    public Node detail;
44    /** an integer that holds current soap version */
45    public int version;
46
47    public SoapFault() {
48        super();
49        this.version = SoapEnvelope.VER11;
50    }
51
52    public SoapFault(int version) {
53        super();
54        this.version = version;
55    }
56
57    /** Fills the fault details from the given XML stream */
58    public void parse(XmlPullParser parser) throws IOException, XmlPullParserException {
59        parser.require(XmlPullParser.START_TAG, SoapEnvelope.ENV, "Fault");
60        while (parser.nextTag() == XmlPullParser.START_TAG) {
61            String name = parser.getName();
62            if (name.equals("detail")) {
63                detail = new Node();
64                detail.parse(parser);
65                // Handle case '...<detail/></soap:Fault>'
66                if (parser.getNamespace().equals(SoapEnvelope.ENV)
67                        && parser.getName().equals("Fault")) {
68                    break;
69                }
70                continue;
71            } else if (name.equals("faultcode")) {
72                faultcode = parser.nextText();
73            } else if (name.equals("faultstring")) {
74                faultstring = parser.nextText();
75            } else if (name.equals("faultactor")) {
76                faultactor = parser.nextText();
77            } else {
78                throw new RuntimeException("unexpected tag:" + name);
79            }
80            parser.require(XmlPullParser.END_TAG, null, name);
81        }
82        parser.require(XmlPullParser.END_TAG, SoapEnvelope.ENV, "Fault");
83        parser.nextTag();
84    }
85
86    /** Writes the fault to the given XML stream */
87    public void write(XmlSerializer xw) throws IOException {
88        xw.startTag(SoapEnvelope.ENV, "Fault");
89        xw.startTag(null, "faultcode");
90        xw.text("" + faultcode);
91        xw.endTag(null, "faultcode");
92        xw.startTag(null, "faultstring");
93        xw.text("" + faultstring);
94        xw.endTag(null, "faultstring");
95        xw.startTag(null, "detail");
96        if (detail != null) {
97            detail.write(xw);
98        }
99        xw.endTag(null, "detail");
100        xw.endTag(SoapEnvelope.ENV, "Fault");
101    }
102
103    /**
104     * @see java.lang.Throwable#getMessage()
105     */
106    public String getMessage() {
107        return faultstring;
108    }
109
110    /** Returns a simple string representation of the fault */
111    public String toString() {
112        return "SoapFault - faultcode: '" + faultcode + "' faultstring: '"
113                + faultstring + "' faultactor: '" + faultactor + "' detail: " +
114                detail;
115    }
116}
117