View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.xml.internal.ws.fault;
27  
28  import com.sun.xml.internal.ws.api.SOAPVersion;
29  import com.sun.xml.internal.ws.util.DOMUtil;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.Node;
32  
33  import javax.xml.bind.annotation.*;
34  import javax.xml.namespace.QName;
35  import javax.xml.soap.Detail;
36  import javax.xml.soap.SOAPException;
37  import javax.xml.soap.SOAPFault;
38  import javax.xml.ws.WebServiceException;
39  import java.util.Iterator;
40  
41  /**
42   * This class represents SOAP1.1 Fault. This class will be used to marshall/unmarshall a soap fault using JAXB.
43   * <p/>
44   * <pre>
45   * Example:
46   * <p/>
47   *     &lt;soap:Fault xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>;
48   *         &lt;faultcode>soap:Client&lt;/faultcode>
49   *         &lt;faultstring>Invalid message format&lt;/faultstring>
50   *         &lt;faultactor>http://example.org/someactor&lt;/faultactor>;
51   *         &lt;detail>
52   *             &lt;m:msg xmlns:m='http://example.org/faults/exceptions'>;
53   *                 Test message
54   *             &lt;/m:msg>
55   *         &lt;/detail>
56   *     &lt;/soap:Fault>
57   * <p/>
58   * Above, m:msg, if a known fault (described in the WSDL), IOW, if m:msg is known by JAXBContext it should be unmarshalled into a
59   * Java object otherwise it should be deserialized as {@link javax.xml.soap.Detail}
60   * </pre>
61   * <p/>
62   *
63   * @author Vivek Pandey
64   */
65  
66  @XmlAccessorType(XmlAccessType.FIELD)
67  @XmlType(name = "", propOrder = {
68          "faultcode",
69          "faultstring",
70          "faultactor",
71          "detail"
72          })
73  @XmlRootElement(name = "Fault", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
74  class SOAP11Fault extends SOAPFaultBuilder {
75      @XmlElement(namespace = "")
76      private QName faultcode;
77  
78      @XmlElement(namespace = "")
79      private String faultstring;
80  
81      @XmlElement(namespace = "")
82      private String faultactor;
83  
84      @XmlElement(namespace = "")
85      private DetailType detail;
86  
87      SOAP11Fault() {
88      }
89  
90      /**
91       * This constructor takes soap fault detail among other things. The detail could represent {@link javax.xml.soap.Detail}
92       * or a java object that can be marshalled/unmarshalled by JAXB.
93       *
94       * @param code
95       * @param reason
96       * @param actor
97       * @param detailObject
98       */
99      SOAP11Fault(QName code, String reason, String actor, Element detailObject) {
100         this.faultcode = code;
101         this.faultstring = reason;
102         this.faultactor = actor;
103         if (detailObject != null) {
104             if ((detailObject.getNamespaceURI() == null ||
105                  "".equals(detailObject.getNamespaceURI())) && "detail".equals(detailObject.getLocalName())) {
106                 detail = new DetailType();
107                 for(Element detailEntry : DOMUtil.getChildElements(detailObject)) {
108                     detail.getDetails().add(detailEntry);
109                 }
110             } else {
111                 detail = new DetailType(detailObject);
112             }
113         }
114     }
115 
116     SOAP11Fault(SOAPFault fault) {
117         this.faultcode = fault.getFaultCodeAsQName();
118         this.faultstring = fault.getFaultString();
119         this.faultactor = fault.getFaultActor();
120         if (fault.getDetail() != null) {
121             detail = new DetailType();
122             Iterator iter = fault.getDetail().getDetailEntries();
123             while(iter.hasNext()){
124                 Element fd = (Element)iter.next();
125                 detail.getDetails().add(fd);
126             }
127         }
128     }
129 
130     QName getFaultcode() {
131         return faultcode;
132     }
133 
134     void setFaultcode(QName faultcode) {
135         this.faultcode = faultcode;
136     }
137 
138     @Override
139     String getFaultString() {
140         return faultstring;
141     }
142 
143     void setFaultstring(String faultstring) {
144         this.faultstring = faultstring;
145     }
146 
147     String getFaultactor() {
148         return faultactor;
149     }
150 
151     void setFaultactor(String faultactor) {
152         this.faultactor = faultactor;
153     }
154 
155     /**
156      * returns the object that represents detail.
157      */
158     @Override
159     DetailType getDetail() {
160         return detail;
161     }
162 
163     void setDetail(DetailType detail) {
164         this.detail = detail;
165     }
166 
167     protected Throwable getProtocolException() {
168         try {
169             SOAPFault fault = SOAPVersion.SOAP_11.getSOAPFactory().createFault(faultstring, faultcode);
170             fault.setFaultActor(faultactor);
171             if(detail != null){
172                 Detail d = fault.addDetail();
173                 for(Element det : detail.getDetails()){
174                     Node n = fault.getOwnerDocument().importNode(det, true);
175                     d.appendChild(n);
176                 }
177             }
178             return new ServerSOAPFaultException(fault);
179         } catch (SOAPException e) {
180             throw new WebServiceException(e);
181         }
182     }
183 }