src/share/jaxws_classes/com/sun/xml/internal/ws/fault/SOAP11Fault.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/fault/SOAP11Fault.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,183 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.fault;
    1.30 +
    1.31 +import com.sun.xml.internal.ws.api.SOAPVersion;
    1.32 +import com.sun.xml.internal.ws.util.DOMUtil;
    1.33 +import org.w3c.dom.Element;
    1.34 +import org.w3c.dom.Node;
    1.35 +
    1.36 +import javax.xml.bind.annotation.*;
    1.37 +import javax.xml.namespace.QName;
    1.38 +import javax.xml.soap.Detail;
    1.39 +import javax.xml.soap.SOAPException;
    1.40 +import javax.xml.soap.SOAPFault;
    1.41 +import javax.xml.ws.WebServiceException;
    1.42 +import javax.xml.ws.soap.SOAPFaultException;
    1.43 +import java.util.Iterator;
    1.44 +
    1.45 +/**
    1.46 + * This class represents SOAP1.1 Fault. This class will be used to marshall/unmarshall a soap fault using JAXB.
    1.47 + * <p/>
    1.48 + * <pre>
    1.49 + * Example:
    1.50 + * <p/>
    1.51 + *     &lt;soap:Fault xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
    1.52 + *         &lt;faultcode>soap:Client&lt;/faultcode>
    1.53 + *         &lt;faultstring>Invalid message format&lt;/faultstring>
    1.54 + *         &lt;faultactor>http://example.org/someactor&lt;/faultactor>
    1.55 + *         &lt;detail>
    1.56 + *             &lt;m:msg xmlns:m='http://example.org/faults/exceptions'>
    1.57 + *                 Test message
    1.58 + *             &lt;/m:msg>
    1.59 + *         &lt;/detail>
    1.60 + *     &lt;/soap:Fault>
    1.61 + * <p/>
    1.62 + * 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
    1.63 + * Java object otherwise it should be deserialized as {@link javax.xml.soap.Detail}
    1.64 + * </pre>
    1.65 + * <p/>
    1.66 + *
    1.67 + * @author Vivek Pandey
    1.68 + */
    1.69 +
    1.70 +@XmlAccessorType(XmlAccessType.FIELD)
    1.71 +@XmlType(name = "", propOrder = {
    1.72 +        "faultcode",
    1.73 +        "faultstring",
    1.74 +        "faultactor",
    1.75 +        "detail"
    1.76 +        })
    1.77 +@XmlRootElement(name = "Fault", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
    1.78 +class SOAP11Fault extends SOAPFaultBuilder {
    1.79 +    @XmlElement(namespace = "")
    1.80 +    private QName faultcode;
    1.81 +
    1.82 +    @XmlElement(namespace = "")
    1.83 +    private String faultstring;
    1.84 +
    1.85 +    @XmlElement(namespace = "")
    1.86 +    private String faultactor;
    1.87 +
    1.88 +    @XmlElement(namespace = "")
    1.89 +    private DetailType detail;
    1.90 +
    1.91 +    SOAP11Fault() {
    1.92 +    }
    1.93 +
    1.94 +    /**
    1.95 +     * This constructor takes soap fault detail among other things. The detail could represent {@link javax.xml.soap.Detail}
    1.96 +     * or a java object that can be marshalled/unmarshalled by JAXB.
    1.97 +     *
    1.98 +     * @param code
    1.99 +     * @param reason
   1.100 +     * @param actor
   1.101 +     * @param detailObject
   1.102 +     */
   1.103 +    SOAP11Fault(QName code, String reason, String actor, Element detailObject) {
   1.104 +        this.faultcode = code;
   1.105 +        this.faultstring = reason;
   1.106 +        this.faultactor = actor;
   1.107 +        if (detailObject != null) {
   1.108 +            if("".equals(detailObject.getNamespaceURI()) && "detail".equals(detailObject.getLocalName())){
   1.109 +                detail = new DetailType();
   1.110 +                for(Element detailEntry : DOMUtil.getChildElements(detailObject)){
   1.111 +                    detail.getDetails().add(detailEntry);
   1.112 +                }
   1.113 +            }else{
   1.114 +                detail = new DetailType(detailObject);
   1.115 +            }
   1.116 +        }
   1.117 +    }
   1.118 +
   1.119 +    SOAP11Fault(SOAPFault fault) {
   1.120 +        this.faultcode = fault.getFaultCodeAsQName();
   1.121 +        this.faultstring = fault.getFaultString();
   1.122 +        this.faultactor = fault.getFaultActor();
   1.123 +        if (fault.getDetail() != null) {
   1.124 +            detail = new DetailType();
   1.125 +            Iterator iter = fault.getDetail().getDetailEntries();
   1.126 +            while(iter.hasNext()){
   1.127 +                Element fd = (Element)iter.next();
   1.128 +                detail.getDetails().add(fd);
   1.129 +            }
   1.130 +        }
   1.131 +    }
   1.132 +
   1.133 +    QName getFaultcode() {
   1.134 +        return faultcode;
   1.135 +    }
   1.136 +
   1.137 +    void setFaultcode(QName faultcode) {
   1.138 +        this.faultcode = faultcode;
   1.139 +    }
   1.140 +
   1.141 +    @Override
   1.142 +    String getFaultString() {
   1.143 +        return faultstring;
   1.144 +    }
   1.145 +
   1.146 +    void setFaultstring(String faultstring) {
   1.147 +        this.faultstring = faultstring;
   1.148 +    }
   1.149 +
   1.150 +    String getFaultactor() {
   1.151 +        return faultactor;
   1.152 +    }
   1.153 +
   1.154 +    void setFaultactor(String faultactor) {
   1.155 +        this.faultactor = faultactor;
   1.156 +    }
   1.157 +
   1.158 +    /**
   1.159 +     * returns the object that represents detail.
   1.160 +     */
   1.161 +    @Override
   1.162 +    DetailType getDetail() {
   1.163 +        return detail;
   1.164 +    }
   1.165 +
   1.166 +    void setDetail(DetailType detail) {
   1.167 +        this.detail = detail;
   1.168 +    }
   1.169 +
   1.170 +    protected Throwable getProtocolException() {
   1.171 +        try {
   1.172 +            SOAPFault fault = SOAPVersion.SOAP_11.getSOAPFactory().createFault(faultstring, faultcode);
   1.173 +            fault.setFaultActor(faultactor);
   1.174 +            if(detail != null){
   1.175 +                Detail d = fault.addDetail();
   1.176 +                for(Element det : detail.getDetails()){
   1.177 +                    Node n = fault.getOwnerDocument().importNode(det, true);
   1.178 +                    d.appendChild(n);
   1.179 +                }
   1.180 +            }
   1.181 +            return new ServerSOAPFaultException(fault);
   1.182 +        } catch (SOAPException e) {
   1.183 +            throw new WebServiceException(e);
   1.184 +        }
   1.185 +    }
   1.186 +}

mercurial