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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.fault;
    28 import com.sun.xml.internal.ws.api.SOAPVersion;
    29 import com.sun.xml.internal.ws.util.DOMUtil;
    30 import org.w3c.dom.Element;
    31 import org.w3c.dom.Node;
    33 import javax.xml.bind.annotation.*;
    34 import javax.xml.namespace.QName;
    35 import javax.xml.soap.Detail;
    36 import javax.xml.soap.SOAPException;
    37 import javax.xml.soap.SOAPFault;
    38 import javax.xml.ws.WebServiceException;
    39 import javax.xml.ws.soap.SOAPFaultException;
    40 import java.util.Iterator;
    42 /**
    43  * This class represents SOAP1.1 Fault. This class will be used to marshall/unmarshall a soap fault using JAXB.
    44  * <p/>
    45  * <pre>
    46  * Example:
    47  * <p/>
    48  *     &lt;soap:Fault xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
    49  *         &lt;faultcode>soap:Client&lt;/faultcode>
    50  *         &lt;faultstring>Invalid message format&lt;/faultstring>
    51  *         &lt;faultactor>http://example.org/someactor&lt;/faultactor>
    52  *         &lt;detail>
    53  *             &lt;m:msg xmlns:m='http://example.org/faults/exceptions'>
    54  *                 Test message
    55  *             &lt;/m:msg>
    56  *         &lt;/detail>
    57  *     &lt;/soap:Fault>
    58  * <p/>
    59  * 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
    60  * Java object otherwise it should be deserialized as {@link javax.xml.soap.Detail}
    61  * </pre>
    62  * <p/>
    63  *
    64  * @author Vivek Pandey
    65  */
    67 @XmlAccessorType(XmlAccessType.FIELD)
    68 @XmlType(name = "", propOrder = {
    69         "faultcode",
    70         "faultstring",
    71         "faultactor",
    72         "detail"
    73         })
    74 @XmlRootElement(name = "Fault", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
    75 class SOAP11Fault extends SOAPFaultBuilder {
    76     @XmlElement(namespace = "")
    77     private QName faultcode;
    79     @XmlElement(namespace = "")
    80     private String faultstring;
    82     @XmlElement(namespace = "")
    83     private String faultactor;
    85     @XmlElement(namespace = "")
    86     private DetailType detail;
    88     SOAP11Fault() {
    89     }
    91     /**
    92      * This constructor takes soap fault detail among other things. The detail could represent {@link javax.xml.soap.Detail}
    93      * or a java object that can be marshalled/unmarshalled by JAXB.
    94      *
    95      * @param code
    96      * @param reason
    97      * @param actor
    98      * @param detailObject
    99      */
   100     SOAP11Fault(QName code, String reason, String actor, Element detailObject) {
   101         this.faultcode = code;
   102         this.faultstring = reason;
   103         this.faultactor = actor;
   104         if (detailObject != null) {
   105             if("".equals(detailObject.getNamespaceURI()) && "detail".equals(detailObject.getLocalName())){
   106                 detail = new DetailType();
   107                 for(Element detailEntry : DOMUtil.getChildElements(detailObject)){
   108                     detail.getDetails().add(detailEntry);
   109                 }
   110             }else{
   111                 detail = new DetailType(detailObject);
   112             }
   113         }
   114     }
   116     SOAP11Fault(SOAPFault fault) {
   117         this.faultcode = fault.getFaultCodeAsQName();
   118         this.faultstring = fault.getFaultString();
   119         this.faultactor = fault.getFaultActor();
   120         if (fault.getDetail() != null) {
   121             detail = new DetailType();
   122             Iterator iter = fault.getDetail().getDetailEntries();
   123             while(iter.hasNext()){
   124                 Element fd = (Element)iter.next();
   125                 detail.getDetails().add(fd);
   126             }
   127         }
   128     }
   130     QName getFaultcode() {
   131         return faultcode;
   132     }
   134     void setFaultcode(QName faultcode) {
   135         this.faultcode = faultcode;
   136     }
   138     @Override
   139     String getFaultString() {
   140         return faultstring;
   141     }
   143     void setFaultstring(String faultstring) {
   144         this.faultstring = faultstring;
   145     }
   147     String getFaultactor() {
   148         return faultactor;
   149     }
   151     void setFaultactor(String faultactor) {
   152         this.faultactor = faultactor;
   153     }
   155     /**
   156      * returns the object that represents detail.
   157      */
   158     @Override
   159     DetailType getDetail() {
   160         return detail;
   161     }
   163     void setDetail(DetailType detail) {
   164         this.detail = detail;
   165     }
   167     protected Throwable getProtocolException() {
   168         try {
   169             SOAPFault fault = SOAPVersion.SOAP_11.getSOAPFactory().createFault(faultstring, faultcode);
   170             fault.setFaultActor(faultactor);
   171             if(detail != null){
   172                 Detail d = fault.addDetail();
   173                 for(Element det : detail.getDetails()){
   174                     Node n = fault.getOwnerDocument().importNode(det, true);
   175                     d.appendChild(n);
   176                 }
   177             }
   178             return new ServerSOAPFaultException(fault);
   179         } catch (SOAPException e) {
   180             throw new WebServiceException(e);
   181         }
   182     }
   183 }

mercurial