ohair@286: /* ohair@286: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.fault; ohair@286: ohair@286: ohair@286: import com.sun.xml.internal.ws.api.SOAPVersion; ohair@286: import com.sun.xml.internal.ws.util.DOMUtil; ohair@286: import org.w3c.dom.Element; ohair@286: import org.w3c.dom.Node; ohair@286: ohair@286: import javax.xml.bind.annotation.XmlAccessType; ohair@286: import javax.xml.bind.annotation.XmlAccessorType; ohair@286: import javax.xml.bind.annotation.XmlElement; ohair@286: import javax.xml.bind.annotation.XmlRootElement; ohair@286: import javax.xml.bind.annotation.XmlTransient; ohair@286: import javax.xml.bind.annotation.XmlType; ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.soap.SOAPException; ohair@286: import javax.xml.soap.SOAPFault; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: import javax.xml.ws.soap.SOAPFaultException; ohair@286: import java.util.Iterator; ohair@286: ohair@286: /** ohair@286: * SOAP 1.2 Fault class that can be marshalled/unmarshalled by JAXB ohair@286: *

ohair@286: *

ohair@286:  * Example:
ohair@286:  * <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
ohair@286:  *            xmlns:m="http://www.example.org/timeouts"
ohair@286:  *            xmlns:xml="http://www.w3.org/XML/1998/namespace">
ohair@286:  * <env:Body>
ohair@286:  *     <env:Fault>
ohair@286:  *         <env:Code>
ohair@286:  *             <env:Value>env:Sender* </env:Value>
ohair@286:  *             <env:Subcode>
ohair@286:  *                 <env:Value>m:MessageTimeout* </env:Value>
ohair@286:  *             </env:Subcode>
ohair@286:  *         </env:Code>
ohair@286:  *         <env:Reason>
ohair@286:  *             <env:Text xml:lang="en">Sender Timeout* </env:Text>
ohair@286:  *         </env:Reason>
ohair@286:  *         <env:Detail>
ohair@286:  *             <m:MaxTime>P5M* </m:MaxTime>
ohair@286:  *         </env:Detail>
ohair@286:  *     </env:Fault>
ohair@286:  * </env:Body>
ohair@286:  * </env:Envelope>
ohair@286:  * 
ohair@286: * ohair@286: * @author Vivek Pandey ohair@286: */ ohair@286: @XmlRootElement(name = "Fault", namespace = "http://www.w3.org/2003/05/soap-envelope") ohair@286: @XmlAccessorType(XmlAccessType.FIELD) ohair@286: @XmlType(name = "", propOrder = { ohair@286: "code", ohair@286: "reason", ohair@286: "node", ohair@286: "role", ohair@286: "detail" ohair@286: }) ohair@286: class SOAP12Fault extends SOAPFaultBuilder { ohair@286: @XmlTransient ohair@286: private static final String ns = "http://www.w3.org/2003/05/soap-envelope"; ohair@286: ohair@286: @XmlElement(namespace=ns, name="Code") ohair@286: private CodeType code; ohair@286: ohair@286: @XmlElement(namespace=ns, name="Reason") ohair@286: private ReasonType reason; ohair@286: ohair@286: @XmlElement(namespace=ns, name="Node") ohair@286: private String node; ohair@286: ohair@286: @XmlElement(namespace=ns, name="Role") ohair@286: private String role; ohair@286: ohair@286: @XmlElement(namespace=ns, name="Detail") ohair@286: private DetailType detail; ohair@286: ohair@286: SOAP12Fault() { ohair@286: } ohair@286: ohair@286: SOAP12Fault(CodeType code, ReasonType reason, String node, String role, DetailType detail) { ohair@286: this.code = code; ohair@286: this.reason = reason; ohair@286: this.node = node; ohair@286: this.role = role; ohair@286: this.detail = detail; ohair@286: } ohair@286: ohair@286: SOAP12Fault(CodeType code, ReasonType reason, String node, String role, Element detailObject) { ohair@286: this.code = code; ohair@286: this.reason = reason; ohair@286: this.node = node; ohair@286: this.role = role; ohair@286: if (detailObject != null) { ohair@286: if(detailObject.getNamespaceURI().equals(ns) && detailObject.getLocalName().equals("Detail")){ ohair@286: detail = new DetailType(); ohair@286: for(Element detailEntry : DOMUtil.getChildElements(detailObject)){ ohair@286: detail.getDetails().add(detailEntry); ohair@286: } ohair@286: }else{ ohair@286: detail = new DetailType(detailObject); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: SOAP12Fault(SOAPFault fault) { ohair@286: code = new CodeType(fault.getFaultCodeAsQName()); ohair@286: try { ohair@286: fillFaultSubCodes(fault); ohair@286: } catch (SOAPException e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: ohair@286: reason = new ReasonType(fault.getFaultString()); ohair@286: role = fault.getFaultRole(); ohair@286: node = fault.getFaultNode(); ohair@286: if (fault.getDetail() != null) { ohair@286: detail = new DetailType(); ohair@286: Iterator iter = fault.getDetail().getDetailEntries(); ohair@286: while(iter.hasNext()){ ohair@286: Element fd = (Element)iter.next(); ohair@286: detail.getDetails().add(fd); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: SOAP12Fault(QName code, String reason, Element detailObject) { ohair@286: this(new CodeType(code), new ReasonType(reason), null, null, detailObject); ohair@286: } ohair@286: ohair@286: CodeType getCode() { ohair@286: return code; ohair@286: } ohair@286: ohair@286: ReasonType getReason() { ohair@286: return reason; ohair@286: } ohair@286: ohair@286: String getNode() { ohair@286: return node; ohair@286: } ohair@286: ohair@286: String getRole() { ohair@286: return role; ohair@286: } ohair@286: ohair@286: @Override ohair@286: DetailType getDetail() { ohair@286: return detail; ohair@286: } ohair@286: ohair@286: @Override ohair@286: void setDetail(DetailType detail) { ohair@286: this.detail = detail; ohair@286: } ohair@286: ohair@286: @Override ohair@286: String getFaultString() { ohair@286: return reason.texts().get(0).getText(); ohair@286: } ohair@286: ohair@286: protected Throwable getProtocolException() { ohair@286: try { ohair@286: SOAPFault fault = SOAPVersion.SOAP_12.getSOAPFactory().createFault();; ohair@286: if(reason != null){ ohair@286: for(TextType tt : reason.texts()){ ohair@286: fault.setFaultString(tt.getText()); ohair@286: } ohair@286: } ohair@286: ohair@286: if(code != null){ ohair@286: fault.setFaultCode(code.getValue()); ohair@286: fillFaultSubCodes(fault, code.getSubcode()); ohair@286: } ohair@286: ohair@286: if(detail != null && detail.getDetail(0) != null){ ohair@286: javax.xml.soap.Detail detail = fault.addDetail(); ohair@286: for(Node obj: this.detail.getDetails()){ ohair@286: Node n = fault.getOwnerDocument().importNode(obj, true); ohair@286: detail.appendChild(n); ohair@286: } ohair@286: } ohair@286: ohair@286: if(node != null) { ohair@286: fault.setFaultNode(node); ohair@286: } ohair@286: ohair@286: return new ServerSOAPFaultException(fault); ohair@286: } catch (SOAPException e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Recursively populate the Subcodes ohair@286: */ ohair@286: private void fillFaultSubCodes(SOAPFault fault, SubcodeType subcode) throws SOAPException { ohair@286: if(subcode != null){ ohair@286: fault.appendFaultSubcode(subcode.getValue()); ohair@286: fillFaultSubCodes(fault, subcode.getSubcode()); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Adds Fault subcodes from {@link SOAPFault} to {@link #code} ohair@286: */ ohair@286: private void fillFaultSubCodes(SOAPFault fault) throws SOAPException { ohair@286: Iterator subcodes = fault.getFaultSubcodes(); ohair@286: SubcodeType firstSct = null; ohair@286: while(subcodes.hasNext()){ ohair@286: QName subcode = (QName)subcodes.next(); ohair@286: if(firstSct == null){ ohair@286: firstSct = new SubcodeType(subcode); ohair@286: code.setSubcode(firstSct); ohair@286: continue; ohair@286: } ohair@286: SubcodeType nextSct = new SubcodeType(subcode); ohair@286: firstSct.setSubcode(nextSct); ohair@286: firstSct = nextSct; ohair@286: } ohair@286: } ohair@286: ohair@286: }