• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany
3  *
4  * Copyright (c) 2011, Petter Uvesten, Everichon AB, Sweden
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7  * associated documentation files (the "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
10  * following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all copies or substantial
13  * portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16  * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
17  * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20  */
21 
22 package org.ksoap2;
23 
24 import java.io.IOException;
25 
26 import org.ksoap2.kdom.Node;
27 import org.xmlpull.v1.XmlPullParser;
28 import org.xmlpull.v1.XmlPullParserException;
29 import org.xmlpull.v1.XmlSerializer;
30 
31 /**
32  * Exception class encapsulating SOAP 1.2 Faults
33  *
34  * see http://www.w3.org/TR/soap12-part1/#soapfault for explanation of fields
35  *
36  * @author Petter Uvesten
37  */
38 
39 public class SoapFault12 extends SoapFault {
40     private static final long serialVersionUID = 1012001L;
41 
42     /** Top-level nodes */
43     public Node Code;
44     public Node Reason;
45     public Node Node;
46     public Node Role;
47     public Node Detail;
48 
SoapFault12()49     public SoapFault12() {
50         super();
51         this.version = SoapEnvelope.VER12;
52     }
53 
SoapFault12(int version)54     public SoapFault12(int version) {
55         super();
56         this.version = version;
57     }
58 
59     /** Fills the fault details from the given XML stream */
parse(XmlPullParser parser)60     public void parse(XmlPullParser parser) throws IOException, XmlPullParserException
61     {
62         parseSelf(parser);
63         // done parsing, populate some of the legacy public members
64         this.faultcode = Code.getElement(SoapEnvelope.ENV2003, "Value").getText(0);
65         this.faultstring = Reason.getElement(SoapEnvelope.ENV2003, "Text").getText(0);
66         this.detail = this.Detail;
67         this.faultactor = null;
68     }
69 
parseSelf(XmlPullParser parser)70     private void parseSelf(XmlPullParser parser) throws IOException, XmlPullParserException {
71         parser.require(XmlPullParser.START_TAG, SoapEnvelope.ENV2003, "Fault");
72 
73         while (parser.nextTag() == XmlPullParser.START_TAG) {
74             String name = parser.getName();
75             String namespace = parser.getNamespace();
76             parser.nextTag();
77             if (name.toLowerCase().equals("Code".toLowerCase())) {
78                 this.Code = new Node();
79                 this.Code.parse(parser);
80             } else if (name.toLowerCase().equals("Reason".toLowerCase())) {
81                 this.Reason = new Node();
82                 this.Reason.parse(parser);
83             } else if (name.toLowerCase().equals("Node".toLowerCase())) {
84                 this.Node = new Node();
85                 this.Node.parse(parser);
86             } else if (name.toLowerCase().equals("Role".toLowerCase())) {
87                 this.Role = new Node();
88                 this.Role.parse(parser);
89             } else if (name.toLowerCase().equals("Detail".toLowerCase())) {
90                 this.Detail = new Node();
91                 this.Detail.parse(parser);
92             } else {
93                 throw new RuntimeException("unexpected tag:" + name);
94             }
95 
96             parser.require(XmlPullParser.END_TAG, namespace, name);
97         }
98         parser.require(XmlPullParser.END_TAG, SoapEnvelope.ENV2003, "Fault");
99         parser.nextTag();
100 
101     }
102 
103     /** Writes the fault to the given XML stream */
write(XmlSerializer xw)104     public void write(XmlSerializer xw) throws IOException
105     {
106         xw.startTag(SoapEnvelope.ENV2003, "Fault");
107         //this.Code.write(xw);
108 
109         xw.startTag(SoapEnvelope.ENV2003, "Code");
110         this.Code.write(xw);
111         xw.endTag(SoapEnvelope.ENV2003, "Code");
112         xw.startTag(SoapEnvelope.ENV2003, "Reason");
113         this.Reason.write(xw);
114         xw.endTag(SoapEnvelope.ENV2003, "Reason");
115 
116         if (this.Node != null) {
117             xw.startTag(SoapEnvelope.ENV2003, "Node");
118             this.Node.write(xw);
119             xw.endTag(SoapEnvelope.ENV2003, "Node");
120         }
121         if (this.Role != null) {
122             xw.startTag(SoapEnvelope.ENV2003, "Role");
123             this.Role.write(xw);
124             xw.endTag(SoapEnvelope.ENV2003, "Role");
125         }
126 
127         if (this.Detail != null) {
128             xw.startTag(SoapEnvelope.ENV2003, "Detail");
129             this.Detail.write(xw);
130             xw.endTag(SoapEnvelope.ENV2003, "Detail");
131         }
132         xw.endTag(SoapEnvelope.ENV2003, "Fault");
133     }
134 
135     /**
136      * @see java.lang.Throwable#getMessage()
137      */
getMessage()138     public String getMessage() {
139         return Reason.getElement(SoapEnvelope.ENV2003, "Text").getText(0);
140     }
141 
142     /** Returns a string representation of the fault */
toString()143     public String toString() {
144         String reason = Reason.getElement(SoapEnvelope.ENV2003, "Text").getText(0);
145         String code = Code.getElement(SoapEnvelope.ENV2003, "Value").getText(0);
146         return "Code: " + code + ", Reason: " + reason;
147     }
148 }
149