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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial