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