src/share/jaxws_classes/com/sun/xml/internal/ws/fault/SOAP11Fault.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 1997, 2010, 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 javax.xml.ws.soap.SOAPFaultException;
40 import java.util.Iterator;
41
42 /**
43 * This class represents SOAP1.1 Fault. This class will be used to marshall/unmarshall a soap fault using JAXB.
44 * <p/>
45 * <pre>
46 * Example:
47 * <p/>
48 * &lt;soap:Fault xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
49 * &lt;faultcode>soap:Client&lt;/faultcode>
50 * &lt;faultstring>Invalid message format&lt;/faultstring>
51 * &lt;faultactor>http://example.org/someactor&lt;/faultactor>
52 * &lt;detail>
53 * &lt;m:msg xmlns:m='http://example.org/faults/exceptions'>
54 * Test message
55 * &lt;/m:msg>
56 * &lt;/detail>
57 * &lt;/soap:Fault>
58 * <p/>
59 * 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
60 * Java object otherwise it should be deserialized as {@link javax.xml.soap.Detail}
61 * </pre>
62 * <p/>
63 *
64 * @author Vivek Pandey
65 */
66
67 @XmlAccessorType(XmlAccessType.FIELD)
68 @XmlType(name = "", propOrder = {
69 "faultcode",
70 "faultstring",
71 "faultactor",
72 "detail"
73 })
74 @XmlRootElement(name = "Fault", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
75 class SOAP11Fault extends SOAPFaultBuilder {
76 @XmlElement(namespace = "")
77 private QName faultcode;
78
79 @XmlElement(namespace = "")
80 private String faultstring;
81
82 @XmlElement(namespace = "")
83 private String faultactor;
84
85 @XmlElement(namespace = "")
86 private DetailType detail;
87
88 SOAP11Fault() {
89 }
90
91 /**
92 * This constructor takes soap fault detail among other things. The detail could represent {@link javax.xml.soap.Detail}
93 * or a java object that can be marshalled/unmarshalled by JAXB.
94 *
95 * @param code
96 * @param reason
97 * @param actor
98 * @param detailObject
99 */
100 SOAP11Fault(QName code, String reason, String actor, Element detailObject) {
101 this.faultcode = code;
102 this.faultstring = reason;
103 this.faultactor = actor;
104 if (detailObject != null) {
105 if("".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 }

mercurial