ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, 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: 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.*; ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.soap.Detail; ohair@286: import javax.xml.soap.SOAPException; ohair@286: import javax.xml.soap.SOAPFault; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: import java.util.Iterator; ohair@286: ohair@286: /** ohair@286: * This class represents SOAP1.1 Fault. This class will be used to marshall/unmarshall a soap fault using JAXB. ohair@286: *

ohair@286: *

ohair@286:  * Example:
ohair@286:  * 

ohair@286: * <soap:Fault xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> ohair@286: * <faultcode>soap:Client</faultcode> ohair@286: * <faultstring>Invalid message format</faultstring> ohair@286: * <faultactor>http://example.org/someactor</faultactor> ohair@286: * <detail> ohair@286: * <m:msg xmlns:m='http://example.org/faults/exceptions'> ohair@286: * Test message ohair@286: * </m:msg> ohair@286: * </detail> ohair@286: * </soap:Fault> ohair@286: *

ohair@286: * 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 ohair@286: * Java object otherwise it should be deserialized as {@link javax.xml.soap.Detail} ohair@286: *

ohair@286: *

ohair@286: * ohair@286: * @author Vivek Pandey ohair@286: */ ohair@286: ohair@286: @XmlAccessorType(XmlAccessType.FIELD) ohair@286: @XmlType(name = "", propOrder = { ohair@286: "faultcode", ohair@286: "faultstring", ohair@286: "faultactor", ohair@286: "detail" ohair@286: }) ohair@286: @XmlRootElement(name = "Fault", namespace = "http://schemas.xmlsoap.org/soap/envelope/") ohair@286: class SOAP11Fault extends SOAPFaultBuilder { ohair@286: @XmlElement(namespace = "") ohair@286: private QName faultcode; ohair@286: ohair@286: @XmlElement(namespace = "") ohair@286: private String faultstring; ohair@286: ohair@286: @XmlElement(namespace = "") ohair@286: private String faultactor; ohair@286: ohair@286: @XmlElement(namespace = "") ohair@286: private DetailType detail; ohair@286: ohair@286: SOAP11Fault() { ohair@286: } ohair@286: ohair@286: /** ohair@286: * This constructor takes soap fault detail among other things. The detail could represent {@link javax.xml.soap.Detail} ohair@286: * or a java object that can be marshalled/unmarshalled by JAXB. ohair@286: * ohair@286: * @param code ohair@286: * @param reason ohair@286: * @param actor ohair@286: * @param detailObject ohair@286: */ ohair@286: SOAP11Fault(QName code, String reason, String actor, Element detailObject) { ohair@286: this.faultcode = code; ohair@286: this.faultstring = reason; ohair@286: this.faultactor = actor; ohair@286: if (detailObject != null) { alanb@368: if ((detailObject.getNamespaceURI() == null || alanb@368: "".equals(detailObject.getNamespaceURI())) && "detail".equals(detailObject.getLocalName())) { ohair@286: detail = new DetailType(); alanb@368: for(Element detailEntry : DOMUtil.getChildElements(detailObject)) { ohair@286: detail.getDetails().add(detailEntry); ohair@286: } alanb@368: } else { ohair@286: detail = new DetailType(detailObject); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: SOAP11Fault(SOAPFault fault) { ohair@286: this.faultcode = fault.getFaultCodeAsQName(); ohair@286: this.faultstring = fault.getFaultString(); ohair@286: this.faultactor = fault.getFaultActor(); 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: QName getFaultcode() { ohair@286: return faultcode; ohair@286: } ohair@286: ohair@286: void setFaultcode(QName faultcode) { ohair@286: this.faultcode = faultcode; ohair@286: } ohair@286: ohair@286: @Override ohair@286: String getFaultString() { ohair@286: return faultstring; ohair@286: } ohair@286: ohair@286: void setFaultstring(String faultstring) { ohair@286: this.faultstring = faultstring; ohair@286: } ohair@286: ohair@286: String getFaultactor() { ohair@286: return faultactor; ohair@286: } ohair@286: ohair@286: void setFaultactor(String faultactor) { ohair@286: this.faultactor = faultactor; ohair@286: } ohair@286: ohair@286: /** ohair@286: * returns the object that represents detail. ohair@286: */ ohair@286: @Override ohair@286: DetailType getDetail() { ohair@286: return detail; ohair@286: } ohair@286: ohair@286: void setDetail(DetailType detail) { ohair@286: this.detail = detail; ohair@286: } ohair@286: ohair@286: protected Throwable getProtocolException() { ohair@286: try { ohair@286: SOAPFault fault = SOAPVersion.SOAP_11.getSOAPFactory().createFault(faultstring, faultcode); ohair@286: fault.setFaultActor(faultactor); ohair@286: if(detail != null){ ohair@286: Detail d = fault.addDetail(); ohair@286: for(Element det : detail.getDetails()){ ohair@286: Node n = fault.getOwnerDocument().importNode(det, true); ohair@286: d.appendChild(n); 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: }