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

aoqi@0: *

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