Thu, 31 Aug 2017 15:18:52 +0800
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 | import com.sun.xml.internal.ws.api.SOAPVersion; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.util.DOMUtil; |
aoqi@0 | 30 | import org.w3c.dom.Element; |
aoqi@0 | 31 | import org.w3c.dom.Node; |
aoqi@0 | 32 | |
aoqi@0 | 33 | import javax.xml.bind.annotation.*; |
aoqi@0 | 34 | import javax.xml.namespace.QName; |
aoqi@0 | 35 | import javax.xml.soap.Detail; |
aoqi@0 | 36 | import javax.xml.soap.SOAPException; |
aoqi@0 | 37 | import javax.xml.soap.SOAPFault; |
aoqi@0 | 38 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 39 | import java.util.Iterator; |
aoqi@0 | 40 | |
aoqi@0 | 41 | /** |
aoqi@0 | 42 | * This class represents SOAP1.1 Fault. This class will be used to marshall/unmarshall a soap fault using JAXB. |
aoqi@0 | 43 | * <p/> |
aoqi@0 | 44 | * <pre> |
aoqi@0 | 45 | * Example: |
aoqi@0 | 46 | * <p/> |
aoqi@0 | 47 | * <soap:Fault xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> |
aoqi@0 | 48 | * <faultcode>soap:Client</faultcode> |
aoqi@0 | 49 | * <faultstring>Invalid message format</faultstring> |
aoqi@0 | 50 | * <faultactor>http://example.org/someactor</faultactor> |
aoqi@0 | 51 | * <detail> |
aoqi@0 | 52 | * <m:msg xmlns:m='http://example.org/faults/exceptions'> |
aoqi@0 | 53 | * Test message |
aoqi@0 | 54 | * </m:msg> |
aoqi@0 | 55 | * </detail> |
aoqi@0 | 56 | * </soap:Fault> |
aoqi@0 | 57 | * <p/> |
aoqi@0 | 58 | * 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 |
aoqi@0 | 59 | * Java object otherwise it should be deserialized as {@link javax.xml.soap.Detail} |
aoqi@0 | 60 | * </pre> |
aoqi@0 | 61 | * <p/> |
aoqi@0 | 62 | * |
aoqi@0 | 63 | * @author Vivek Pandey |
aoqi@0 | 64 | */ |
aoqi@0 | 65 | |
aoqi@0 | 66 | @XmlAccessorType(XmlAccessType.FIELD) |
aoqi@0 | 67 | @XmlType(name = "", propOrder = { |
aoqi@0 | 68 | "faultcode", |
aoqi@0 | 69 | "faultstring", |
aoqi@0 | 70 | "faultactor", |
aoqi@0 | 71 | "detail" |
aoqi@0 | 72 | }) |
aoqi@0 | 73 | @XmlRootElement(name = "Fault", namespace = "http://schemas.xmlsoap.org/soap/envelope/") |
aoqi@0 | 74 | class SOAP11Fault extends SOAPFaultBuilder { |
aoqi@0 | 75 | @XmlElement(namespace = "") |
aoqi@0 | 76 | private QName faultcode; |
aoqi@0 | 77 | |
aoqi@0 | 78 | @XmlElement(namespace = "") |
aoqi@0 | 79 | private String faultstring; |
aoqi@0 | 80 | |
aoqi@0 | 81 | @XmlElement(namespace = "") |
aoqi@0 | 82 | private String faultactor; |
aoqi@0 | 83 | |
aoqi@0 | 84 | @XmlElement(namespace = "") |
aoqi@0 | 85 | private DetailType detail; |
aoqi@0 | 86 | |
aoqi@0 | 87 | SOAP11Fault() { |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | /** |
aoqi@0 | 91 | * This constructor takes soap fault detail among other things. The detail could represent {@link javax.xml.soap.Detail} |
aoqi@0 | 92 | * or a java object that can be marshalled/unmarshalled by JAXB. |
aoqi@0 | 93 | * |
aoqi@0 | 94 | * @param code |
aoqi@0 | 95 | * @param reason |
aoqi@0 | 96 | * @param actor |
aoqi@0 | 97 | * @param detailObject |
aoqi@0 | 98 | */ |
aoqi@0 | 99 | SOAP11Fault(QName code, String reason, String actor, Element detailObject) { |
aoqi@0 | 100 | this.faultcode = code; |
aoqi@0 | 101 | this.faultstring = reason; |
aoqi@0 | 102 | this.faultactor = actor; |
aoqi@0 | 103 | if (detailObject != null) { |
aoqi@0 | 104 | if ((detailObject.getNamespaceURI() == null || |
aoqi@0 | 105 | "".equals(detailObject.getNamespaceURI())) && "detail".equals(detailObject.getLocalName())) { |
aoqi@0 | 106 | detail = new DetailType(); |
aoqi@0 | 107 | for(Element detailEntry : DOMUtil.getChildElements(detailObject)) { |
aoqi@0 | 108 | detail.getDetails().add(detailEntry); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | } else { |
aoqi@0 | 111 | detail = new DetailType(detailObject); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | } |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | SOAP11Fault(SOAPFault fault) { |
aoqi@0 | 117 | this.faultcode = fault.getFaultCodeAsQName(); |
aoqi@0 | 118 | this.faultstring = fault.getFaultString(); |
aoqi@0 | 119 | this.faultactor = fault.getFaultActor(); |
aoqi@0 | 120 | if (fault.getDetail() != null) { |
aoqi@0 | 121 | detail = new DetailType(); |
aoqi@0 | 122 | Iterator iter = fault.getDetail().getDetailEntries(); |
aoqi@0 | 123 | while(iter.hasNext()){ |
aoqi@0 | 124 | Element fd = (Element)iter.next(); |
aoqi@0 | 125 | detail.getDetails().add(fd); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | } |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | QName getFaultcode() { |
aoqi@0 | 131 | return faultcode; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | void setFaultcode(QName faultcode) { |
aoqi@0 | 135 | this.faultcode = faultcode; |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | @Override |
aoqi@0 | 139 | String getFaultString() { |
aoqi@0 | 140 | return faultstring; |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | void setFaultstring(String faultstring) { |
aoqi@0 | 144 | this.faultstring = faultstring; |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | String getFaultactor() { |
aoqi@0 | 148 | return faultactor; |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | void setFaultactor(String faultactor) { |
aoqi@0 | 152 | this.faultactor = faultactor; |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | /** |
aoqi@0 | 156 | * returns the object that represents detail. |
aoqi@0 | 157 | */ |
aoqi@0 | 158 | @Override |
aoqi@0 | 159 | DetailType getDetail() { |
aoqi@0 | 160 | return detail; |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | void setDetail(DetailType detail) { |
aoqi@0 | 164 | this.detail = detail; |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | protected Throwable getProtocolException() { |
aoqi@0 | 168 | try { |
aoqi@0 | 169 | SOAPFault fault = SOAPVersion.SOAP_11.getSOAPFactory().createFault(faultstring, faultcode); |
aoqi@0 | 170 | fault.setFaultActor(faultactor); |
aoqi@0 | 171 | if(detail != null){ |
aoqi@0 | 172 | Detail d = fault.addDetail(); |
aoqi@0 | 173 | for(Element det : detail.getDetails()){ |
aoqi@0 | 174 | Node n = fault.getOwnerDocument().importNode(det, true); |
aoqi@0 | 175 | d.appendChild(n); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | } |
aoqi@0 | 178 | return new ServerSOAPFaultException(fault); |
aoqi@0 | 179 | } catch (SOAPException e) { |
aoqi@0 | 180 | throw new WebServiceException(e); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | } |
aoqi@0 | 183 | } |