001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.fault;
038:
039: import com.sun.xml.ws.api.SOAPVersion;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.Node;
042:
043: import javax.xml.bind.annotation.XmlAccessType;
044: import javax.xml.bind.annotation.XmlAccessorType;
045: import javax.xml.bind.annotation.XmlElement;
046: import javax.xml.bind.annotation.XmlRootElement;
047: import javax.xml.bind.annotation.XmlTransient;
048: import javax.xml.bind.annotation.XmlType;
049: import javax.xml.namespace.QName;
050: import javax.xml.soap.SOAPException;
051: import javax.xml.soap.SOAPFault;
052: import javax.xml.ws.WebServiceException;
053: import javax.xml.ws.soap.SOAPFaultException;
054: import java.util.Iterator;
055:
056: /**
057: * SOAP 1.2 Fault class that can be marshalled/unmarshalled by JAXB
058: * <p/>
059: * <pre>
060: * Example:
061: * <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
062: * xmlns:m="http://www.example.org/timeouts"
063: * xmlns:xml="http://www.w3.org/XML/1998/namespace">
064: * <env:Body>
065: * <env:Fault>
066: * <env:Code>
067: * <env:Value>env:Sender* </env:Value>
068: * <env:Subcode>
069: * <env:Value>m:MessageTimeout* </env:Value>
070: * </env:Subcode>
071: * </env:Code>
072: * <env:Reason>
073: * <env:Text xml:lang="en">Sender Timeout* </env:Text>
074: * </env:Reason>
075: * <env:Detail>
076: * <m:MaxTime>P5M* </m:MaxTime>
077: * </env:Detail>
078: * </env:Fault>
079: * </env:Body>
080: * </env:Envelope>
081: * </pre>
082: *
083: * @author Vivek Pandey
084: */
085: @XmlRootElement(name="Fault",namespace="http://www.w3.org/2003/05/soap-envelope")
086: @XmlAccessorType(XmlAccessType.FIELD)
087: @XmlType(name="",propOrder={"code","reason","node","role","detail"})
088: class SOAP12Fault extends SOAPFaultBuilder {
089: @XmlTransient
090: private static final String ns = "http://www.w3.org/2003/05/soap-envelope";
091:
092: @XmlElement(namespace=ns,name="Code")
093: private CodeType code;
094:
095: @XmlElement(namespace=ns,name="Reason")
096: private ReasonType reason;
097:
098: @XmlElement(namespace=ns,name="Node")
099: private String node;
100:
101: @XmlElement(namespace=ns,name="Role")
102: private String role;
103:
104: @XmlElement(namespace=ns,name="Detail")
105: private DetailType detail;
106:
107: SOAP12Fault() {
108: }
109:
110: SOAP12Fault(CodeType code, ReasonType reason, String node,
111: String role, DetailType detail) {
112: this .code = code;
113: this .reason = reason;
114: this .node = node;
115: this .role = role;
116: this .detail = detail;
117: }
118:
119: SOAP12Fault(SOAPFault fault) {
120: code = new CodeType(fault.getFaultCodeAsQName());
121: try {
122: fillFaultSubCodes(fault);
123: } catch (SOAPException e) {
124: throw new WebServiceException(e);
125: }
126:
127: reason = new ReasonType(fault.getFaultString());
128: role = fault.getFaultRole();
129: detail = new DetailType(fault.getDetail());
130: }
131:
132: SOAP12Fault(QName code, String reason, Element detailObject) {
133: this .code = new CodeType(code);
134: this .reason = new ReasonType(reason);
135: if (detailObject != null)
136: detail = new DetailType(detailObject);
137: }
138:
139: CodeType getCode() {
140: return code;
141: }
142:
143: ReasonType getReason() {
144: return reason;
145: }
146:
147: String getNode() {
148: return node;
149: }
150:
151: String getRole() {
152: return role;
153: }
154:
155: @Override
156: DetailType getDetail() {
157: return detail;
158: }
159:
160: @Override
161: void setDetail(DetailType detail) {
162: this .detail = detail;
163: }
164:
165: @Override
166: String getFaultString() {
167: return reason.texts().get(0).getText();
168: }
169:
170: protected Throwable getProtocolException() {
171: try {
172: SOAPFault fault = SOAPVersion.SOAP_12.saajSoapFactory
173: .createFault();
174: ;
175: if (reason != null) {
176: for (TextType tt : reason.texts()) {
177: fault.setFaultString(tt.getText());
178: }
179: }
180:
181: if (code != null) {
182: fault.setFaultCode(code.getValue());
183: fillFaultSubCodes(fault, code.getSubcode());
184: }
185:
186: if (detail != null && detail.getDetail(0) != null) {
187: javax.xml.soap.Detail detail = fault.addDetail();
188: for (Node obj : this .detail.getDetails()) {
189: Node n = fault.getOwnerDocument().importNode(obj,
190: true);
191: detail.appendChild(n);
192: }
193: }
194:
195: return new SOAPFaultException(fault);
196: } catch (SOAPException e) {
197: throw new WebServiceException(e);
198: }
199: }
200:
201: /**
202: * Recursively populate the Subcodes
203: */
204: private void fillFaultSubCodes(SOAPFault fault, SubcodeType subcode)
205: throws SOAPException {
206: if (subcode != null) {
207: fault.appendFaultSubcode(subcode.getValue());
208: fillFaultSubCodes(fault, subcode.getSubcode());
209: }
210: }
211:
212: /**
213: * Adds Fault subcodes from {@link SOAPFault} to {@link #code}
214: */
215: private void fillFaultSubCodes(SOAPFault fault)
216: throws SOAPException {
217: Iterator subcodes = fault.getFaultSubcodes();
218: SubcodeType firstSct = null;
219: while (subcodes.hasNext()) {
220: QName subcode = (QName) subcodes.next();
221: if (firstSct == null) {
222: firstSct = new SubcodeType(subcode);
223: code.setSubcode(firstSct);
224: continue;
225: }
226: SubcodeType nextSct = new SubcodeType(subcode);
227: firstSct.setSubcode(nextSct);
228: firstSct = nextSct;
229: }
230: }
231:
232: }
|