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 com.sun.xml.ws.util.DOMUtil;
041: import org.w3c.dom.Element;
042: import org.w3c.dom.Node;
043:
044: import javax.xml.bind.annotation.XmlAccessType;
045: import javax.xml.bind.annotation.XmlAccessorType;
046: import javax.xml.bind.annotation.XmlElement;
047: import javax.xml.bind.annotation.XmlRootElement;
048: import javax.xml.bind.annotation.XmlType;
049: import javax.xml.namespace.QName;
050: import javax.xml.soap.Detail;
051: import javax.xml.soap.SOAPException;
052: import javax.xml.soap.SOAPFault;
053: import javax.xml.ws.WebServiceException;
054: import javax.xml.ws.soap.SOAPFaultException;
055:
056: /**
057: * This class represents SOAP1.1 Fault. This class will be used to marshall/unmarshall a soap fault using JAXB.
058: * <p/>
059: * <pre>
060: * Example:
061: * <p/>
062: * <soap:Fault xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
063: * <faultcode>soap:Client</faultcode>
064: * <faultstring>Invalid message format</faultstring>
065: * <faultactor>http://example.org/someactor</faultactor>
066: * <detail>
067: * <m:msg xmlns:m='http://example.org/faults/exceptions'>
068: * Test message
069: * </m:msg>
070: * </detail>
071: * </soap:Fault>
072: * <p/>
073: * 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
074: * Java object otherwise it should be deserialized as {@link javax.xml.soap.Detail}
075: * </pre>
076: * <p/>
077: *
078: * @author Vivek Pandey
079: */
080:
081: @XmlAccessorType(XmlAccessType.FIELD)
082: @XmlType(name="",propOrder={"faultcode","faultstring","faultactor","detail"})
083: @XmlRootElement(name="Fault",namespace="http://schemas.xmlsoap.org/soap/envelope/")
084: class SOAP11Fault extends SOAPFaultBuilder {
085: @XmlElement(namespace="")
086: private QName faultcode;
087:
088: @XmlElement(namespace="")
089: private String faultstring;
090:
091: @XmlElement(namespace="")
092: private String faultactor;
093:
094: @XmlElement(namespace="")
095: private DetailType detail;
096:
097: SOAP11Fault() {
098: }
099:
100: /**
101: * This constructor takes soap fault detail among other things. The detail could represent {@link javax.xml.soap.Detail}
102: * or a java object that can be marshalled/unmarshalled by JAXB.
103: *
104: * @param code
105: * @param reason
106: * @param actor
107: * @param detailObject
108: */
109: SOAP11Fault(QName code, String reason, String actor,
110: Element detailObject) {
111: this .faultcode = code;
112: this .faultstring = reason;
113: this .faultactor = actor;
114: if (detailObject != null) {
115: if (detailObject.getNamespaceURI().equals("")
116: && detailObject.getLocalName().equals("detail")) {
117: detail = new DetailType();
118: for (Element detailEntry : DOMUtil
119: .getChildElements(detailObject)) {
120: detail.getDetails().add(detailEntry);
121: }
122: } else {
123: detail = new DetailType(detailObject);
124: }
125: }
126: }
127:
128: SOAP11Fault(SOAPFault fault) {
129: this .faultcode = fault.getFaultCodeAsQName();
130: this .faultstring = fault.getFaultString();
131: this .faultactor = fault.getFaultActor();
132: if (fault.getDetail() != null) {
133: detail = new DetailType(fault.getDetail());
134: }
135: }
136:
137: QName getFaultcode() {
138: return faultcode;
139: }
140:
141: void setFaultcode(QName faultcode) {
142: this .faultcode = faultcode;
143: }
144:
145: @Override
146: String getFaultString() {
147: return faultstring;
148: }
149:
150: void setFaultstring(String faultstring) {
151: this .faultstring = faultstring;
152: }
153:
154: String getFaultactor() {
155: return faultactor;
156: }
157:
158: void setFaultactor(String faultactor) {
159: this .faultactor = faultactor;
160: }
161:
162: /**
163: * returns the object that represents detail.
164: */
165: @Override
166: DetailType getDetail() {
167: return detail;
168: }
169:
170: void setDetail(DetailType detail) {
171: this .detail = detail;
172: }
173:
174: protected Throwable getProtocolException() {
175: try {
176: SOAPFault fault = SOAPVersion.SOAP_11.saajSoapFactory
177: .createFault(faultstring, faultcode);
178: if (detail != null) {
179: Detail d = fault.addDetail();
180: for (Element det : detail.getDetails()) {
181: Node n = fault.getOwnerDocument().importNode(det,
182: true);
183: d.appendChild(n);
184: }
185: }
186: fault.setFaultActor(faultactor);
187: return new SOAPFaultException(fault);
188: } catch (SOAPException e) {
189: throw new WebServiceException(e);
190: }
191: }
192: }
|