Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, 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.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.SOAPVersion; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.message.Message; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.model.ExceptionType; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.encoding.soap.SOAP12Constants; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.encoding.soap.SOAPConstants; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.encoding.soap.SerializationException; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.message.jaxb.JAXBMessage; |
aoqi@0 | 37 | import com.sun.xml.internal.ws.message.FaultMessage; |
aoqi@0 | 38 | import com.sun.xml.internal.ws.model.CheckedExceptionImpl; |
aoqi@0 | 39 | import com.sun.xml.internal.ws.model.JavaMethodImpl; |
aoqi@0 | 40 | import com.sun.xml.internal.ws.spi.db.XMLBridge; |
aoqi@0 | 41 | import com.sun.xml.internal.ws.util.DOMUtil; |
aoqi@0 | 42 | import com.sun.xml.internal.ws.util.StringUtils; |
aoqi@0 | 43 | import org.w3c.dom.Document; |
aoqi@0 | 44 | import org.w3c.dom.Element; |
aoqi@0 | 45 | import org.w3c.dom.Node; |
aoqi@0 | 46 | |
aoqi@0 | 47 | import javax.xml.bind.JAXBContext; |
aoqi@0 | 48 | import javax.xml.bind.JAXBException; |
aoqi@0 | 49 | import javax.xml.bind.annotation.XmlTransient; |
aoqi@0 | 50 | import javax.xml.namespace.QName; |
aoqi@0 | 51 | import javax.xml.soap.SOAPFault; |
aoqi@0 | 52 | import javax.xml.soap.Detail; |
aoqi@0 | 53 | import javax.xml.soap.DetailEntry; |
aoqi@0 | 54 | import javax.xml.transform.dom.DOMResult; |
aoqi@0 | 55 | import javax.xml.ws.ProtocolException; |
aoqi@0 | 56 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 57 | import javax.xml.ws.soap.SOAPFaultException; |
aoqi@0 | 58 | import java.lang.reflect.Constructor; |
aoqi@0 | 59 | import java.lang.reflect.Field; |
aoqi@0 | 60 | import java.lang.reflect.Method; |
aoqi@0 | 61 | import java.lang.reflect.ReflectPermission; |
aoqi@0 | 62 | import java.security.AccessControlContext; |
aoqi@0 | 63 | import java.security.AccessController; |
aoqi@0 | 64 | import java.security.Permissions; |
aoqi@0 | 65 | import java.security.PrivilegedAction; |
aoqi@0 | 66 | import java.security.ProtectionDomain; |
aoqi@0 | 67 | import java.util.Iterator; |
aoqi@0 | 68 | import java.util.Map; |
aoqi@0 | 69 | import java.util.logging.Level; |
aoqi@0 | 70 | import java.util.logging.Logger; |
aoqi@0 | 71 | |
aoqi@0 | 72 | /** |
aoqi@0 | 73 | * Base class that represents SOAP 1.1 or SOAP 1.2 fault. This class can be used by the invocation handlers to create |
aoqi@0 | 74 | * an Exception from a received messge. |
aoqi@0 | 75 | * |
aoqi@0 | 76 | * @author Vivek Pandey |
aoqi@0 | 77 | */ |
aoqi@0 | 78 | public abstract class SOAPFaultBuilder { |
aoqi@0 | 79 | |
aoqi@0 | 80 | /** |
aoqi@0 | 81 | * Gives the {@link DetailType} for a Soap 1.1 or Soap 1.2 message that can be used to create either a checked exception or |
aoqi@0 | 82 | * a protocol specific exception |
aoqi@0 | 83 | */ |
aoqi@0 | 84 | abstract DetailType getDetail(); |
aoqi@0 | 85 | |
aoqi@0 | 86 | abstract void setDetail(DetailType detailType); |
aoqi@0 | 87 | |
aoqi@0 | 88 | public @XmlTransient @Nullable QName getFirstDetailEntryName() { |
aoqi@0 | 89 | DetailType dt = getDetail(); |
aoqi@0 | 90 | if (dt != null) { |
aoqi@0 | 91 | Node entry = dt.getDetail(0); |
aoqi@0 | 92 | if (entry != null) { |
aoqi@0 | 93 | return new QName(entry.getNamespaceURI(), entry.getLocalName()); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | } |
aoqi@0 | 96 | return null; |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | /** |
aoqi@0 | 100 | * gives the fault string that can be used to create an {@link Exception} |
aoqi@0 | 101 | */ |
aoqi@0 | 102 | abstract String getFaultString(); |
aoqi@0 | 103 | |
aoqi@0 | 104 | /** |
aoqi@0 | 105 | * This should be called from the client side to throw an {@link Exception} for a given soap mesage |
aoqi@0 | 106 | */ |
aoqi@0 | 107 | public Throwable createException(Map<QName, CheckedExceptionImpl> exceptions) throws JAXBException { |
aoqi@0 | 108 | DetailType dt = getDetail(); |
aoqi@0 | 109 | Node detail = null; |
aoqi@0 | 110 | if(dt != null) detail = dt.getDetail(0); |
aoqi@0 | 111 | |
aoqi@0 | 112 | //return ProtocolException if the detail is not present or there is no checked exception |
aoqi@0 | 113 | if(detail == null || exceptions == null){ |
aoqi@0 | 114 | // No soap detail, doesnt look like its a checked exception |
aoqi@0 | 115 | // throw a protocol exception |
aoqi@0 | 116 | return attachServerException(getProtocolException()); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | //check if the detail is a checked exception, if not throw a ProtocolException |
aoqi@0 | 120 | QName detailName = new QName(detail.getNamespaceURI(), detail.getLocalName()); |
aoqi@0 | 121 | CheckedExceptionImpl ce = exceptions.get(detailName); |
aoqi@0 | 122 | if (ce == null) { |
aoqi@0 | 123 | //No Checked exception for the received detail QName, throw a SOAPFault exception |
aoqi@0 | 124 | return attachServerException(getProtocolException()); |
aoqi@0 | 125 | |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | if (ce.getExceptionType().equals(ExceptionType.UserDefined)) { |
aoqi@0 | 129 | return attachServerException(createUserDefinedException(ce)); |
aoqi@0 | 130 | |
aoqi@0 | 131 | } |
aoqi@0 | 132 | Class exceptionClass = ce.getExceptionClass(); |
aoqi@0 | 133 | try { |
aoqi@0 | 134 | Constructor constructor = exceptionClass.getConstructor(String.class, (Class) ce.getDetailType().type); |
aoqi@0 | 135 | Exception exception = (Exception) constructor.newInstance(getFaultString(), getJAXBObject(detail, ce)); |
aoqi@0 | 136 | return attachServerException(exception); |
aoqi@0 | 137 | } catch (Exception e) { |
aoqi@0 | 138 | throw new WebServiceException(e); |
aoqi@0 | 139 | } |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | /** |
aoqi@0 | 143 | * To be called to convert a {@link ProtocolException} and faultcode for a given {@link SOAPVersion} in to a {@link Message}. |
aoqi@0 | 144 | * |
aoqi@0 | 145 | * @param soapVersion {@link SOAPVersion#SOAP_11} or {@link SOAPVersion#SOAP_12} |
aoqi@0 | 146 | * @param ex a ProtocolException |
aoqi@0 | 147 | * @param faultcode soap faultcode. Its ignored if the {@link ProtocolException} instance is {@link SOAPFaultException} and it has a |
aoqi@0 | 148 | * faultcode present in the underlying {@link SOAPFault}. |
aoqi@0 | 149 | * @return {@link Message} representing SOAP fault |
aoqi@0 | 150 | */ |
aoqi@0 | 151 | public static @NotNull Message createSOAPFaultMessage(@NotNull SOAPVersion soapVersion, @NotNull ProtocolException ex, @Nullable QName faultcode){ |
aoqi@0 | 152 | Object detail = getFaultDetail(null, ex); |
aoqi@0 | 153 | if(soapVersion == SOAPVersion.SOAP_12) |
aoqi@0 | 154 | return createSOAP12Fault(soapVersion, ex, detail, null, faultcode); |
aoqi@0 | 155 | return createSOAP11Fault(soapVersion, ex, detail, null, faultcode); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | /** |
aoqi@0 | 159 | * To be called by the server runtime in the situations when there is an Exception that needs to be transformed in |
aoqi@0 | 160 | * to a soapenv:Fault payload. |
aoqi@0 | 161 | * |
aoqi@0 | 162 | * @param ceModel {@link CheckedExceptionImpl} model that provides useful informations such as the detail tagname |
aoqi@0 | 163 | * and the Exception associated with it. Caller of this constructor should get the CheckedException |
aoqi@0 | 164 | * model by calling {@link JavaMethodImpl#getCheckedException(Class)}, where |
aoqi@0 | 165 | * Class is t.getClass(). |
aoqi@0 | 166 | * <p> |
aoqi@0 | 167 | * If its null then this is not a checked exception and in that case the soap fault will be |
aoqi@0 | 168 | * serialized only from the exception as described below. |
aoqi@0 | 169 | * @param ex Exception that needs to be translated into soapenv:Fault, always non-null. |
aoqi@0 | 170 | * <ul> |
aoqi@0 | 171 | * <li>If t is instance of {@link SOAPFaultException} then its serilaized as protocol exception. |
aoqi@0 | 172 | * <li>If t.getCause() is instance of {@link SOAPFaultException} and t is a checked exception then |
aoqi@0 | 173 | * the soap fault detail is serilaized from t and the fault actor/string/role is taken from t.getCause(). |
aoqi@0 | 174 | * </ul> |
aoqi@0 | 175 | * @param soapVersion non-null |
aoqi@0 | 176 | */ |
aoqi@0 | 177 | public static Message createSOAPFaultMessage(SOAPVersion soapVersion, CheckedExceptionImpl ceModel, Throwable ex) { |
aoqi@0 | 178 | // Sometimes InvocationTargetException.getCause() is null |
aoqi@0 | 179 | // but InvocationTargetException.getTargetException() contains the real exception |
aoqi@0 | 180 | // even though they are supposed to be equivalent. |
aoqi@0 | 181 | // If we only look at .getCause this results in the real exception being lost. |
aoqi@0 | 182 | // Looks like a JDK bug. |
aoqi@0 | 183 | final Throwable t = |
aoqi@0 | 184 | ex instanceof java.lang.reflect.InvocationTargetException |
aoqi@0 | 185 | ? |
aoqi@0 | 186 | ((java.lang.reflect.InvocationTargetException)ex).getTargetException() |
aoqi@0 | 187 | : |
aoqi@0 | 188 | ex; |
aoqi@0 | 189 | return createSOAPFaultMessage(soapVersion, ceModel, t, null); |
aoqi@0 | 190 | } |
aoqi@0 | 191 | |
aoqi@0 | 192 | /** |
aoqi@0 | 193 | * Create the Message with the specified faultCode |
aoqi@0 | 194 | * |
aoqi@0 | 195 | * @see #createSOAPFaultMessage(SOAPVersion, CheckedExceptionImpl, Throwable) |
aoqi@0 | 196 | */ |
aoqi@0 | 197 | public static Message createSOAPFaultMessage(SOAPVersion soapVersion, CheckedExceptionImpl ceModel, Throwable ex, QName faultCode) { |
aoqi@0 | 198 | Object detail = getFaultDetail(ceModel, ex); |
aoqi@0 | 199 | if(soapVersion == SOAPVersion.SOAP_12) |
aoqi@0 | 200 | return createSOAP12Fault(soapVersion, ex, detail, ceModel, faultCode); |
aoqi@0 | 201 | return createSOAP11Fault(soapVersion, ex, detail, ceModel, faultCode); |
aoqi@0 | 202 | } |
aoqi@0 | 203 | |
aoqi@0 | 204 | /** |
aoqi@0 | 205 | * Server runtime will call this when there is some internal error not resulting from an exception. |
aoqi@0 | 206 | * |
aoqi@0 | 207 | * @param soapVersion {@link SOAPVersion#SOAP_11} or {@link SOAPVersion#SOAP_12} |
aoqi@0 | 208 | * @param faultString must be non-null |
aoqi@0 | 209 | * @param faultCode For SOAP 1.1, it must be one of |
aoqi@0 | 210 | * <ul> |
aoqi@0 | 211 | * <li>{@link SOAPVersion#faultCodeClient} |
aoqi@0 | 212 | * <li>{@link SOAPVersion#faultCodeServer} |
aoqi@0 | 213 | * <li>{@link SOAPConstants#FAULT_CODE_MUST_UNDERSTAND} |
aoqi@0 | 214 | * <li>{@link SOAPConstants#FAULT_CODE_VERSION_MISMATCH} |
aoqi@0 | 215 | * </ul> |
aoqi@0 | 216 | * |
aoqi@0 | 217 | * For SOAP 1.2 |
aoqi@0 | 218 | * <ul> |
aoqi@0 | 219 | * <li>{@link SOAPVersion#faultCodeClient} |
aoqi@0 | 220 | * <li>{@link SOAPVersion#faultCodeServer} |
aoqi@0 | 221 | * <li>{@link SOAP12Constants#FAULT_CODE_MUST_UNDERSTAND} |
aoqi@0 | 222 | * <li>{@link SOAP12Constants#FAULT_CODE_VERSION_MISMATCH} |
aoqi@0 | 223 | * <li>{@link SOAP12Constants#FAULT_CODE_DATA_ENCODING_UNKNOWN} |
aoqi@0 | 224 | * </ul> |
aoqi@0 | 225 | * @return non-null {@link Message} |
aoqi@0 | 226 | */ |
aoqi@0 | 227 | public static Message createSOAPFaultMessage(SOAPVersion soapVersion, String faultString, QName faultCode) { |
aoqi@0 | 228 | if (faultCode == null) |
aoqi@0 | 229 | faultCode = getDefaultFaultCode(soapVersion); |
aoqi@0 | 230 | return createSOAPFaultMessage(soapVersion, faultString, faultCode, null); |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | public static Message createSOAPFaultMessage(SOAPVersion soapVersion, SOAPFault fault) { |
aoqi@0 | 234 | switch (soapVersion) { |
aoqi@0 | 235 | case SOAP_11: |
aoqi@0 | 236 | return JAXBMessage.create(JAXB_CONTEXT, new SOAP11Fault(fault), soapVersion); |
aoqi@0 | 237 | case SOAP_12: |
aoqi@0 | 238 | return JAXBMessage.create(JAXB_CONTEXT, new SOAP12Fault(fault), soapVersion); |
aoqi@0 | 239 | default: |
aoqi@0 | 240 | throw new AssertionError(); |
aoqi@0 | 241 | } |
aoqi@0 | 242 | } |
aoqi@0 | 243 | |
aoqi@0 | 244 | private static Message createSOAPFaultMessage(SOAPVersion soapVersion, String faultString, QName faultCode, Element detail) { |
aoqi@0 | 245 | switch (soapVersion) { |
aoqi@0 | 246 | case SOAP_11: |
aoqi@0 | 247 | return JAXBMessage.create(JAXB_CONTEXT, new SOAP11Fault(faultCode, faultString, null, detail), soapVersion); |
aoqi@0 | 248 | case SOAP_12: |
aoqi@0 | 249 | return JAXBMessage.create(JAXB_CONTEXT, new SOAP12Fault(faultCode, faultString, detail), soapVersion); |
aoqi@0 | 250 | default: |
aoqi@0 | 251 | throw new AssertionError(); |
aoqi@0 | 252 | } |
aoqi@0 | 253 | } |
aoqi@0 | 254 | |
aoqi@0 | 255 | /** |
aoqi@0 | 256 | * Creates a DOM node that represents the complete stack trace of the exception, |
aoqi@0 | 257 | * and attach that to {@link DetailType}. |
aoqi@0 | 258 | */ |
aoqi@0 | 259 | final void captureStackTrace(@Nullable Throwable t) { |
aoqi@0 | 260 | if(t==null) return; |
aoqi@0 | 261 | if(!captureStackTrace) return; // feature disabled |
aoqi@0 | 262 | |
aoqi@0 | 263 | try { |
aoqi@0 | 264 | Document d = DOMUtil.createDom(); |
aoqi@0 | 265 | ExceptionBean.marshal(t,d); |
aoqi@0 | 266 | |
aoqi@0 | 267 | DetailType detail = getDetail(); |
aoqi@0 | 268 | if(detail==null) |
aoqi@0 | 269 | setDetail(detail=new DetailType()); |
aoqi@0 | 270 | |
aoqi@0 | 271 | detail.getDetails().add(d.getDocumentElement()); |
aoqi@0 | 272 | } catch (JAXBException e) { |
aoqi@0 | 273 | // this should never happen |
aoqi@0 | 274 | logger.log(Level.WARNING, "Unable to capture the stack trace into XML",e); |
aoqi@0 | 275 | } |
aoqi@0 | 276 | } |
aoqi@0 | 277 | |
aoqi@0 | 278 | /** |
aoqi@0 | 279 | * Initialize the cause of this exception by attaching the server side exception. |
aoqi@0 | 280 | */ |
aoqi@0 | 281 | private <T extends Throwable> T attachServerException(T t) { |
aoqi@0 | 282 | DetailType detail = getDetail(); |
aoqi@0 | 283 | if(detail==null) return t; // no details |
aoqi@0 | 284 | |
aoqi@0 | 285 | for (Element n : detail.getDetails()) { |
aoqi@0 | 286 | if(ExceptionBean.isStackTraceXml(n)) { |
aoqi@0 | 287 | try { |
aoqi@0 | 288 | t.initCause(ExceptionBean.unmarshal(n)); |
aoqi@0 | 289 | } catch (JAXBException e) { |
aoqi@0 | 290 | // perhaps incorrectly formatted XML. |
aoqi@0 | 291 | logger.log(Level.WARNING, "Unable to read the capture stack trace in the fault",e); |
aoqi@0 | 292 | } |
aoqi@0 | 293 | return t; |
aoqi@0 | 294 | } |
aoqi@0 | 295 | } |
aoqi@0 | 296 | |
aoqi@0 | 297 | return t; |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | abstract protected Throwable getProtocolException(); |
aoqi@0 | 301 | |
aoqi@0 | 302 | private Object getJAXBObject(Node jaxbBean, CheckedExceptionImpl ce) throws JAXBException { |
aoqi@0 | 303 | XMLBridge bridge = ce.getBond(); |
aoqi@0 | 304 | return bridge.unmarshal(jaxbBean,null); |
aoqi@0 | 305 | } |
aoqi@0 | 306 | |
aoqi@0 | 307 | private Exception createUserDefinedException(CheckedExceptionImpl ce) { |
aoqi@0 | 308 | Class exceptionClass = ce.getExceptionClass(); |
aoqi@0 | 309 | Class detailBean = ce.getDetailBean(); |
aoqi@0 | 310 | try{ |
aoqi@0 | 311 | Node detailNode = getDetail().getDetails().get(0); |
aoqi@0 | 312 | Object jaxbDetail = getJAXBObject(detailNode, ce); |
aoqi@0 | 313 | Constructor exConstructor; |
aoqi@0 | 314 | try{ |
aoqi@0 | 315 | exConstructor = exceptionClass.getConstructor(String.class, detailBean); |
aoqi@0 | 316 | return (Exception) exConstructor.newInstance(getFaultString(), jaxbDetail); |
aoqi@0 | 317 | }catch(NoSuchMethodException e){ |
aoqi@0 | 318 | exConstructor = exceptionClass.getConstructor(String.class); |
aoqi@0 | 319 | return (Exception) exConstructor.newInstance(getFaultString()); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | } catch (Exception e) { |
aoqi@0 | 322 | throw new WebServiceException(e); |
aoqi@0 | 323 | } |
aoqi@0 | 324 | } |
aoqi@0 | 325 | |
aoqi@0 | 326 | private static String getWriteMethod(Field f) { |
aoqi@0 | 327 | return "set" + StringUtils.capitalize(f.getName()); |
aoqi@0 | 328 | } |
aoqi@0 | 329 | |
aoqi@0 | 330 | private static Object getFaultDetail(CheckedExceptionImpl ce, Throwable exception) { |
aoqi@0 | 331 | if (ce == null) |
aoqi@0 | 332 | return null; |
aoqi@0 | 333 | if (ce.getExceptionType().equals(ExceptionType.UserDefined)) { |
aoqi@0 | 334 | return createDetailFromUserDefinedException(ce, exception); |
aoqi@0 | 335 | } |
aoqi@0 | 336 | try { |
aoqi@0 | 337 | Method m = exception.getClass().getMethod("getFaultInfo"); |
aoqi@0 | 338 | return m.invoke(exception); |
aoqi@0 | 339 | } catch (Exception e) { |
aoqi@0 | 340 | throw new SerializationException(e); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | } |
aoqi@0 | 343 | |
aoqi@0 | 344 | private static Object createDetailFromUserDefinedException(CheckedExceptionImpl ce, Object exception) { |
aoqi@0 | 345 | Class detailBean = ce.getDetailBean(); |
aoqi@0 | 346 | Field[] fields = detailBean.getDeclaredFields(); |
aoqi@0 | 347 | try { |
aoqi@0 | 348 | Object detail = detailBean.newInstance(); |
aoqi@0 | 349 | for (Field f : fields) { |
aoqi@0 | 350 | Method em = exception.getClass().getMethod(getReadMethod(f)); |
aoqi@0 | 351 | try { |
aoqi@0 | 352 | Method sm = detailBean.getMethod(getWriteMethod(f), em.getReturnType()); |
aoqi@0 | 353 | sm.invoke(detail, em.invoke(exception)); |
aoqi@0 | 354 | } catch(NoSuchMethodException ne) { |
aoqi@0 | 355 | // Try to use exception bean's public field to populate the value. |
aoqi@0 | 356 | Field sf = detailBean.getField(f.getName()); |
aoqi@0 | 357 | sf.set(detail, em.invoke(exception)); |
aoqi@0 | 358 | } |
aoqi@0 | 359 | } |
aoqi@0 | 360 | return detail; |
aoqi@0 | 361 | } catch (Exception e) { |
aoqi@0 | 362 | throw new SerializationException(e); |
aoqi@0 | 363 | } |
aoqi@0 | 364 | } |
aoqi@0 | 365 | |
aoqi@0 | 366 | private static String getReadMethod(Field f) { |
aoqi@0 | 367 | if (f.getType().isAssignableFrom(boolean.class)) |
aoqi@0 | 368 | return "is" + StringUtils.capitalize(f.getName()); |
aoqi@0 | 369 | return "get" + StringUtils.capitalize(f.getName()); |
aoqi@0 | 370 | } |
aoqi@0 | 371 | |
aoqi@0 | 372 | private static Message createSOAP11Fault(SOAPVersion soapVersion, Throwable e, Object detail, CheckedExceptionImpl ce, QName faultCode) { |
aoqi@0 | 373 | SOAPFaultException soapFaultException = null; |
aoqi@0 | 374 | String faultString = null; |
aoqi@0 | 375 | String faultActor = null; |
aoqi@0 | 376 | Throwable cause = e.getCause(); |
aoqi@0 | 377 | if (e instanceof SOAPFaultException) { |
aoqi@0 | 378 | soapFaultException = (SOAPFaultException) e; |
aoqi@0 | 379 | } else if (cause != null && cause instanceof SOAPFaultException) { |
aoqi@0 | 380 | soapFaultException = (SOAPFaultException) e.getCause(); |
aoqi@0 | 381 | } |
aoqi@0 | 382 | if (soapFaultException != null) { |
aoqi@0 | 383 | QName soapFaultCode = soapFaultException.getFault().getFaultCodeAsQName(); |
aoqi@0 | 384 | if(soapFaultCode != null) |
aoqi@0 | 385 | faultCode = soapFaultCode; |
aoqi@0 | 386 | |
aoqi@0 | 387 | faultString = soapFaultException.getFault().getFaultString(); |
aoqi@0 | 388 | faultActor = soapFaultException.getFault().getFaultActor(); |
aoqi@0 | 389 | } |
aoqi@0 | 390 | |
aoqi@0 | 391 | if (faultCode == null) { |
aoqi@0 | 392 | faultCode = getDefaultFaultCode(soapVersion); |
aoqi@0 | 393 | } |
aoqi@0 | 394 | |
aoqi@0 | 395 | if (faultString == null) { |
aoqi@0 | 396 | faultString = e.getMessage(); |
aoqi@0 | 397 | if (faultString == null) { |
aoqi@0 | 398 | faultString = e.toString(); |
aoqi@0 | 399 | } |
aoqi@0 | 400 | } |
aoqi@0 | 401 | Element detailNode = null; |
aoqi@0 | 402 | QName firstEntry = null; |
aoqi@0 | 403 | if (detail == null && soapFaultException != null) { |
aoqi@0 | 404 | detailNode = soapFaultException.getFault().getDetail(); |
aoqi@0 | 405 | firstEntry = getFirstDetailEntryName((Detail)detailNode); |
aoqi@0 | 406 | } else if(ce != null){ |
aoqi@0 | 407 | try { |
aoqi@0 | 408 | DOMResult dr = new DOMResult(); |
aoqi@0 | 409 | ce.getBond().marshal(detail,dr); |
aoqi@0 | 410 | detailNode = (Element)dr.getNode().getFirstChild(); |
aoqi@0 | 411 | firstEntry = getFirstDetailEntryName(detailNode); |
aoqi@0 | 412 | } catch (JAXBException e1) { |
aoqi@0 | 413 | //Should we throw Internal Server Error??? |
aoqi@0 | 414 | faultString = e.getMessage(); |
aoqi@0 | 415 | faultCode = getDefaultFaultCode(soapVersion); |
aoqi@0 | 416 | } |
aoqi@0 | 417 | } |
aoqi@0 | 418 | SOAP11Fault soap11Fault = new SOAP11Fault(faultCode, faultString, faultActor, detailNode); |
aoqi@0 | 419 | |
aoqi@0 | 420 | //Don't fill the stacktrace for Service specific exceptions. |
aoqi@0 | 421 | if(ce == null) { |
aoqi@0 | 422 | soap11Fault.captureStackTrace(e); |
aoqi@0 | 423 | } |
aoqi@0 | 424 | Message msg = JAXBMessage.create(JAXB_CONTEXT, soap11Fault, soapVersion); |
aoqi@0 | 425 | return new FaultMessage(msg, firstEntry); |
aoqi@0 | 426 | } |
aoqi@0 | 427 | |
aoqi@0 | 428 | private static @Nullable QName getFirstDetailEntryName(@Nullable Detail detail) { |
aoqi@0 | 429 | if (detail != null) { |
aoqi@0 | 430 | Iterator<DetailEntry> it = detail.getDetailEntries(); |
aoqi@0 | 431 | if (it.hasNext()) { |
aoqi@0 | 432 | DetailEntry entry = it.next(); |
aoqi@0 | 433 | return getFirstDetailEntryName(entry); |
aoqi@0 | 434 | } |
aoqi@0 | 435 | } |
aoqi@0 | 436 | return null; |
aoqi@0 | 437 | } |
aoqi@0 | 438 | |
aoqi@0 | 439 | private static @NotNull QName getFirstDetailEntryName(@NotNull Element entry) { |
aoqi@0 | 440 | return new QName(entry.getNamespaceURI(), entry.getLocalName()); |
aoqi@0 | 441 | } |
aoqi@0 | 442 | |
aoqi@0 | 443 | private static Message createSOAP12Fault(SOAPVersion soapVersion, Throwable e, Object detail, CheckedExceptionImpl ce, QName faultCode) { |
aoqi@0 | 444 | SOAPFaultException soapFaultException = null; |
aoqi@0 | 445 | CodeType code = null; |
aoqi@0 | 446 | String faultString = null; |
aoqi@0 | 447 | String faultRole = null; |
aoqi@0 | 448 | String faultNode = null; |
aoqi@0 | 449 | Throwable cause = e.getCause(); |
aoqi@0 | 450 | if (e instanceof SOAPFaultException) { |
aoqi@0 | 451 | soapFaultException = (SOAPFaultException) e; |
aoqi@0 | 452 | } else if (cause != null && cause instanceof SOAPFaultException) { |
aoqi@0 | 453 | soapFaultException = (SOAPFaultException) e.getCause(); |
aoqi@0 | 454 | } |
aoqi@0 | 455 | if (soapFaultException != null) { |
aoqi@0 | 456 | SOAPFault fault = soapFaultException.getFault(); |
aoqi@0 | 457 | QName soapFaultCode = fault.getFaultCodeAsQName(); |
aoqi@0 | 458 | if(soapFaultCode != null){ |
aoqi@0 | 459 | faultCode = soapFaultCode; |
aoqi@0 | 460 | code = new CodeType(faultCode); |
aoqi@0 | 461 | Iterator iter = fault.getFaultSubcodes(); |
aoqi@0 | 462 | boolean first = true; |
aoqi@0 | 463 | SubcodeType subcode = null; |
aoqi@0 | 464 | while(iter.hasNext()){ |
aoqi@0 | 465 | QName value = (QName)iter.next(); |
aoqi@0 | 466 | if(first){ |
aoqi@0 | 467 | SubcodeType sct = new SubcodeType(value); |
aoqi@0 | 468 | code.setSubcode(sct); |
aoqi@0 | 469 | subcode = sct; |
aoqi@0 | 470 | first = false; |
aoqi@0 | 471 | continue; |
aoqi@0 | 472 | } |
aoqi@0 | 473 | subcode = fillSubcodes(subcode, value); |
aoqi@0 | 474 | } |
aoqi@0 | 475 | } |
aoqi@0 | 476 | faultString = soapFaultException.getFault().getFaultString(); |
aoqi@0 | 477 | faultRole = soapFaultException.getFault().getFaultActor(); |
aoqi@0 | 478 | faultNode = soapFaultException.getFault().getFaultNode(); |
aoqi@0 | 479 | } |
aoqi@0 | 480 | |
aoqi@0 | 481 | if (faultCode == null) { |
aoqi@0 | 482 | faultCode = getDefaultFaultCode(soapVersion); |
aoqi@0 | 483 | code = new CodeType(faultCode); |
aoqi@0 | 484 | }else if(code == null){ |
aoqi@0 | 485 | code = new CodeType(faultCode); |
aoqi@0 | 486 | } |
aoqi@0 | 487 | |
aoqi@0 | 488 | if (faultString == null) { |
aoqi@0 | 489 | faultString = e.getMessage(); |
aoqi@0 | 490 | if (faultString == null) { |
aoqi@0 | 491 | faultString = e.toString(); |
aoqi@0 | 492 | } |
aoqi@0 | 493 | } |
aoqi@0 | 494 | |
aoqi@0 | 495 | ReasonType reason = new ReasonType(faultString); |
aoqi@0 | 496 | Element detailNode = null; |
aoqi@0 | 497 | QName firstEntry = null; |
aoqi@0 | 498 | if (detail == null && soapFaultException != null) { |
aoqi@0 | 499 | detailNode = soapFaultException.getFault().getDetail(); |
aoqi@0 | 500 | firstEntry = getFirstDetailEntryName((Detail)detailNode); |
aoqi@0 | 501 | } else if(detail != null){ |
aoqi@0 | 502 | try { |
aoqi@0 | 503 | DOMResult dr = new DOMResult(); |
aoqi@0 | 504 | ce.getBond().marshal(detail, dr); |
aoqi@0 | 505 | detailNode = (Element)dr.getNode().getFirstChild(); |
aoqi@0 | 506 | firstEntry = getFirstDetailEntryName(detailNode); |
aoqi@0 | 507 | } catch (JAXBException e1) { |
aoqi@0 | 508 | //Should we throw Internal Server Error??? |
aoqi@0 | 509 | faultString = e.getMessage(); |
aoqi@0 | 510 | } |
aoqi@0 | 511 | } |
aoqi@0 | 512 | |
aoqi@0 | 513 | SOAP12Fault soap12Fault = new SOAP12Fault(code, reason, faultNode, faultRole, detailNode); |
aoqi@0 | 514 | |
aoqi@0 | 515 | //Don't fill the stacktrace for Service specific exceptions. |
aoqi@0 | 516 | if(ce == null) { |
aoqi@0 | 517 | soap12Fault.captureStackTrace(e); |
aoqi@0 | 518 | } |
aoqi@0 | 519 | Message msg = JAXBMessage.create(JAXB_CONTEXT, soap12Fault, soapVersion); |
aoqi@0 | 520 | return new FaultMessage(msg, firstEntry); |
aoqi@0 | 521 | } |
aoqi@0 | 522 | |
aoqi@0 | 523 | private static SubcodeType fillSubcodes(SubcodeType parent, QName value){ |
aoqi@0 | 524 | SubcodeType newCode = new SubcodeType(value); |
aoqi@0 | 525 | parent.setSubcode(newCode); |
aoqi@0 | 526 | return newCode; |
aoqi@0 | 527 | } |
aoqi@0 | 528 | |
aoqi@0 | 529 | private static QName getDefaultFaultCode(SOAPVersion soapVersion) { |
aoqi@0 | 530 | return soapVersion.faultCodeServer; |
aoqi@0 | 531 | } |
aoqi@0 | 532 | |
aoqi@0 | 533 | /** |
aoqi@0 | 534 | * Parses a fault {@link Message} and returns it as a {@link SOAPFaultBuilder}. |
aoqi@0 | 535 | * |
aoqi@0 | 536 | * @return always non-null valid object. |
aoqi@0 | 537 | * @throws JAXBException if the parsing fails. |
aoqi@0 | 538 | */ |
aoqi@0 | 539 | public static SOAPFaultBuilder create(Message msg) throws JAXBException { |
aoqi@0 | 540 | return msg.readPayloadAsJAXB(JAXB_CONTEXT.createUnmarshaller()); |
aoqi@0 | 541 | } |
aoqi@0 | 542 | |
aoqi@0 | 543 | /** |
aoqi@0 | 544 | * This {@link JAXBContext} can handle SOAP 1.1/1.2 faults. |
aoqi@0 | 545 | */ |
aoqi@0 | 546 | private static final JAXBContext JAXB_CONTEXT; |
aoqi@0 | 547 | |
aoqi@0 | 548 | private static final Logger logger = Logger.getLogger(SOAPFaultBuilder.class.getName()); |
aoqi@0 | 549 | |
aoqi@0 | 550 | /** |
aoqi@0 | 551 | * Set to false if you don't want the generated faults to have stack trace in it. |
aoqi@0 | 552 | */ |
aoqi@0 | 553 | public static final boolean captureStackTrace; |
aoqi@0 | 554 | |
aoqi@0 | 555 | /*package*/ static final String CAPTURE_STACK_TRACE_PROPERTY = SOAPFaultBuilder.class.getName()+".captureStackTrace"; |
aoqi@0 | 556 | |
aoqi@0 | 557 | static { |
aoqi@0 | 558 | boolean tmpVal = false; |
aoqi@0 | 559 | try { |
aoqi@0 | 560 | tmpVal = Boolean.getBoolean(CAPTURE_STACK_TRACE_PROPERTY); |
aoqi@0 | 561 | } catch (SecurityException e) { |
aoqi@0 | 562 | // ignore |
aoqi@0 | 563 | } |
aoqi@0 | 564 | captureStackTrace = tmpVal; |
aoqi@0 | 565 | JAXB_CONTEXT = createJAXBContext(); |
aoqi@0 | 566 | } |
aoqi@0 | 567 | |
aoqi@0 | 568 | private static JAXBContext createJAXBContext() { |
aoqi@0 | 569 | |
aoqi@0 | 570 | // in jdk runtime doPrivileged is necessary since JAX-WS internal classes are in restricted packages |
aoqi@0 | 571 | if (isJDKRuntime()) { |
aoqi@0 | 572 | Permissions permissions = new Permissions(); |
aoqi@0 | 573 | permissions.add(new RuntimePermission("accessClassInPackage.com.sun." + "xml.internal.ws.fault")); |
aoqi@0 | 574 | permissions.add(new ReflectPermission("suppressAccessChecks")); |
aoqi@0 | 575 | return AccessController.doPrivileged( |
aoqi@0 | 576 | new PrivilegedAction<JAXBContext>() { |
aoqi@0 | 577 | @Override |
aoqi@0 | 578 | public JAXBContext run() { |
aoqi@0 | 579 | try { |
aoqi@0 | 580 | return JAXBContext.newInstance(SOAP11Fault.class, SOAP12Fault.class); |
aoqi@0 | 581 | } catch (JAXBException e) { |
aoqi@0 | 582 | throw new Error(e); |
aoqi@0 | 583 | } |
aoqi@0 | 584 | } |
aoqi@0 | 585 | }, |
aoqi@0 | 586 | new AccessControlContext(new ProtectionDomain[]{new ProtectionDomain(null, permissions)}) |
aoqi@0 | 587 | ); |
aoqi@0 | 588 | |
aoqi@0 | 589 | } else { |
aoqi@0 | 590 | try { |
aoqi@0 | 591 | return JAXBContext.newInstance(SOAP11Fault.class, SOAP12Fault.class); |
aoqi@0 | 592 | } catch (JAXBException e) { |
aoqi@0 | 593 | throw new Error(e); |
aoqi@0 | 594 | } |
aoqi@0 | 595 | } |
aoqi@0 | 596 | } |
aoqi@0 | 597 | |
aoqi@0 | 598 | private static boolean isJDKRuntime() { |
aoqi@0 | 599 | return SOAPFaultBuilder.class.getName().contains("internal"); |
aoqi@0 | 600 | } |
aoqi@0 | 601 | } |