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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.fault;
aoqi@0 27
aoqi@0 28
aoqi@0 29 import com.sun.xml.internal.ws.api.SOAPVersion;
aoqi@0 30 import com.sun.xml.internal.ws.util.DOMUtil;
aoqi@0 31 import org.w3c.dom.Element;
aoqi@0 32 import org.w3c.dom.Node;
aoqi@0 33
aoqi@0 34 import javax.xml.bind.annotation.XmlAccessType;
aoqi@0 35 import javax.xml.bind.annotation.XmlAccessorType;
aoqi@0 36 import javax.xml.bind.annotation.XmlElement;
aoqi@0 37 import javax.xml.bind.annotation.XmlRootElement;
aoqi@0 38 import javax.xml.bind.annotation.XmlTransient;
aoqi@0 39 import javax.xml.bind.annotation.XmlType;
aoqi@0 40 import javax.xml.namespace.QName;
aoqi@0 41 import javax.xml.soap.SOAPException;
aoqi@0 42 import javax.xml.soap.SOAPFault;
aoqi@0 43 import javax.xml.ws.WebServiceException;
aoqi@0 44 import javax.xml.ws.soap.SOAPFaultException;
aoqi@0 45 import java.util.Iterator;
aoqi@0 46
aoqi@0 47 /**
aoqi@0 48 * SOAP 1.2 Fault class that can be marshalled/unmarshalled by JAXB
aoqi@0 49 * <p/>
aoqi@0 50 * <pre>
aoqi@0 51 * Example:
aoqi@0 52 * &lt;env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
aoqi@0 53 * xmlns:m="http://www.example.org/timeouts"
aoqi@0 54 * xmlns:xml="http://www.w3.org/XML/1998/namespace">
aoqi@0 55 * &lt;env:Body>
aoqi@0 56 * &lt;env:Fault>
aoqi@0 57 * &lt;env:Code>
aoqi@0 58 * &lt;env:Value>env:Sender* &lt;/env:Value>
aoqi@0 59 * &lt;env:Subcode>
aoqi@0 60 * &lt;env:Value>m:MessageTimeout* &lt;/env:Value>
aoqi@0 61 * &lt;/env:Subcode>
aoqi@0 62 * &lt;/env:Code>
aoqi@0 63 * &lt;env:Reason>
aoqi@0 64 * &lt;env:Text xml:lang="en">Sender Timeout* &lt;/env:Text>
aoqi@0 65 * &lt;/env:Reason>
aoqi@0 66 * &lt;env:Detail>
aoqi@0 67 * &lt;m:MaxTime>P5M* &lt;/m:MaxTime>
aoqi@0 68 * &lt;/env:Detail>
aoqi@0 69 * &lt;/env:Fault>
aoqi@0 70 * &lt;/env:Body>
aoqi@0 71 * &lt;/env:Envelope>
aoqi@0 72 * </pre>
aoqi@0 73 *
aoqi@0 74 * @author Vivek Pandey
aoqi@0 75 */
aoqi@0 76 @XmlRootElement(name = "Fault", namespace = "http://www.w3.org/2003/05/soap-envelope")
aoqi@0 77 @XmlAccessorType(XmlAccessType.FIELD)
aoqi@0 78 @XmlType(name = "", propOrder = {
aoqi@0 79 "code",
aoqi@0 80 "reason",
aoqi@0 81 "node",
aoqi@0 82 "role",
aoqi@0 83 "detail"
aoqi@0 84 })
aoqi@0 85 class SOAP12Fault extends SOAPFaultBuilder {
aoqi@0 86 @XmlTransient
aoqi@0 87 private static final String ns = "http://www.w3.org/2003/05/soap-envelope";
aoqi@0 88
aoqi@0 89 @XmlElement(namespace=ns, name="Code")
aoqi@0 90 private CodeType code;
aoqi@0 91
aoqi@0 92 @XmlElement(namespace=ns, name="Reason")
aoqi@0 93 private ReasonType reason;
aoqi@0 94
aoqi@0 95 @XmlElement(namespace=ns, name="Node")
aoqi@0 96 private String node;
aoqi@0 97
aoqi@0 98 @XmlElement(namespace=ns, name="Role")
aoqi@0 99 private String role;
aoqi@0 100
aoqi@0 101 @XmlElement(namespace=ns, name="Detail")
aoqi@0 102 private DetailType detail;
aoqi@0 103
aoqi@0 104 SOAP12Fault() {
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 SOAP12Fault(CodeType code, ReasonType reason, String node, String role, DetailType detail) {
aoqi@0 108 this.code = code;
aoqi@0 109 this.reason = reason;
aoqi@0 110 this.node = node;
aoqi@0 111 this.role = role;
aoqi@0 112 this.detail = detail;
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 SOAP12Fault(CodeType code, ReasonType reason, String node, String role, Element detailObject) {
aoqi@0 116 this.code = code;
aoqi@0 117 this.reason = reason;
aoqi@0 118 this.node = node;
aoqi@0 119 this.role = role;
aoqi@0 120 if (detailObject != null) {
aoqi@0 121 if(detailObject.getNamespaceURI().equals(ns) && detailObject.getLocalName().equals("Detail")){
aoqi@0 122 detail = new DetailType();
aoqi@0 123 for(Element detailEntry : DOMUtil.getChildElements(detailObject)){
aoqi@0 124 detail.getDetails().add(detailEntry);
aoqi@0 125 }
aoqi@0 126 }else{
aoqi@0 127 detail = new DetailType(detailObject);
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 SOAP12Fault(SOAPFault fault) {
aoqi@0 133 code = new CodeType(fault.getFaultCodeAsQName());
aoqi@0 134 try {
aoqi@0 135 fillFaultSubCodes(fault);
aoqi@0 136 } catch (SOAPException e) {
aoqi@0 137 throw new WebServiceException(e);
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 reason = new ReasonType(fault.getFaultString());
aoqi@0 141 role = fault.getFaultRole();
aoqi@0 142 node = fault.getFaultNode();
aoqi@0 143 if (fault.getDetail() != null) {
aoqi@0 144 detail = new DetailType();
aoqi@0 145 Iterator iter = fault.getDetail().getDetailEntries();
aoqi@0 146 while(iter.hasNext()){
aoqi@0 147 Element fd = (Element)iter.next();
aoqi@0 148 detail.getDetails().add(fd);
aoqi@0 149 }
aoqi@0 150 }
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 SOAP12Fault(QName code, String reason, Element detailObject) {
aoqi@0 154 this(new CodeType(code), new ReasonType(reason), null, null, detailObject);
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 CodeType getCode() {
aoqi@0 158 return code;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 ReasonType getReason() {
aoqi@0 162 return reason;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 String getNode() {
aoqi@0 166 return node;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 String getRole() {
aoqi@0 170 return role;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 @Override
aoqi@0 174 DetailType getDetail() {
aoqi@0 175 return detail;
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 @Override
aoqi@0 179 void setDetail(DetailType detail) {
aoqi@0 180 this.detail = detail;
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 @Override
aoqi@0 184 String getFaultString() {
aoqi@0 185 return reason.texts().get(0).getText();
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 protected Throwable getProtocolException() {
aoqi@0 189 try {
aoqi@0 190 SOAPFault fault = SOAPVersion.SOAP_12.getSOAPFactory().createFault();;
aoqi@0 191 if(reason != null){
aoqi@0 192 for(TextType tt : reason.texts()){
aoqi@0 193 fault.setFaultString(tt.getText());
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 if(code != null){
aoqi@0 198 fault.setFaultCode(code.getValue());
aoqi@0 199 fillFaultSubCodes(fault, code.getSubcode());
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 if(detail != null && detail.getDetail(0) != null){
aoqi@0 203 javax.xml.soap.Detail detail = fault.addDetail();
aoqi@0 204 for(Node obj: this.detail.getDetails()){
aoqi@0 205 Node n = fault.getOwnerDocument().importNode(obj, true);
aoqi@0 206 detail.appendChild(n);
aoqi@0 207 }
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 if(node != null) {
aoqi@0 211 fault.setFaultNode(node);
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 return new ServerSOAPFaultException(fault);
aoqi@0 215 } catch (SOAPException e) {
aoqi@0 216 throw new WebServiceException(e);
aoqi@0 217 }
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 /**
aoqi@0 221 * Recursively populate the Subcodes
aoqi@0 222 */
aoqi@0 223 private void fillFaultSubCodes(SOAPFault fault, SubcodeType subcode) throws SOAPException {
aoqi@0 224 if(subcode != null){
aoqi@0 225 fault.appendFaultSubcode(subcode.getValue());
aoqi@0 226 fillFaultSubCodes(fault, subcode.getSubcode());
aoqi@0 227 }
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 /**
aoqi@0 231 * Adds Fault subcodes from {@link SOAPFault} to {@link #code}
aoqi@0 232 */
aoqi@0 233 private void fillFaultSubCodes(SOAPFault fault) throws SOAPException {
aoqi@0 234 Iterator subcodes = fault.getFaultSubcodes();
aoqi@0 235 SubcodeType firstSct = null;
aoqi@0 236 while(subcodes.hasNext()){
aoqi@0 237 QName subcode = (QName)subcodes.next();
aoqi@0 238 if(firstSct == null){
aoqi@0 239 firstSct = new SubcodeType(subcode);
aoqi@0 240 code.setSubcode(firstSct);
aoqi@0 241 continue;
aoqi@0 242 }
aoqi@0 243 SubcodeType nextSct = new SubcodeType(subcode);
aoqi@0 244 firstSct.setSubcode(nextSct);
aoqi@0 245 firstSct = nextSct;
aoqi@0 246 }
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 }

mercurial