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.client.sei; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.xml.internal.ws.api.SOAPVersion; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.api.message.Message; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.message.Messages; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.message.jaxb.JAXBMessage; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.model.ParameterImpl; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.model.WrapperParameter; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.spi.db.BindingContext; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.spi.db.XMLBridge; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.spi.db.PropertyAccessor; |
aoqi@0 | 37 | import com.sun.xml.internal.ws.spi.db.WrapperComposite; |
aoqi@0 | 38 | |
aoqi@0 | 39 | import javax.xml.bind.JAXBException; |
aoqi@0 | 40 | import javax.xml.namespace.QName; |
aoqi@0 | 41 | import javax.xml.ws.Holder; |
aoqi@0 | 42 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 43 | import java.util.List; |
aoqi@0 | 44 | |
aoqi@0 | 45 | /** |
aoqi@0 | 46 | * Builds a JAXB object that represents the payload. |
aoqi@0 | 47 | * |
aoqi@0 | 48 | * @see MessageFiller |
aoqi@0 | 49 | * @author Kohsuke Kawaguchi |
aoqi@0 | 50 | */ |
aoqi@0 | 51 | abstract class BodyBuilder { |
aoqi@0 | 52 | abstract Message createMessage(Object[] methodArgs); |
aoqi@0 | 53 | |
aoqi@0 | 54 | static final BodyBuilder EMPTY_SOAP11 = new Empty(SOAPVersion.SOAP_11); |
aoqi@0 | 55 | static final BodyBuilder EMPTY_SOAP12 = new Empty(SOAPVersion.SOAP_12); |
aoqi@0 | 56 | |
aoqi@0 | 57 | private static final class Empty extends BodyBuilder { |
aoqi@0 | 58 | private final SOAPVersion soapVersion; |
aoqi@0 | 59 | |
aoqi@0 | 60 | public Empty(SOAPVersion soapVersion) { |
aoqi@0 | 61 | this.soapVersion = soapVersion; |
aoqi@0 | 62 | } |
aoqi@0 | 63 | |
aoqi@0 | 64 | Message createMessage(Object[] methodArgs) { |
aoqi@0 | 65 | return Messages.createEmpty(soapVersion); |
aoqi@0 | 66 | } |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | /** |
aoqi@0 | 70 | * Base class for those {@link BodyBuilder}s that build a {@link Message} |
aoqi@0 | 71 | * from JAXB objects. |
aoqi@0 | 72 | */ |
aoqi@0 | 73 | private static abstract class JAXB extends BodyBuilder { |
aoqi@0 | 74 | /** |
aoqi@0 | 75 | * This object determines the binding of the object returned |
aoqi@0 | 76 | * from {@link #build(Object[])}. |
aoqi@0 | 77 | */ |
aoqi@0 | 78 | private final XMLBridge bridge; |
aoqi@0 | 79 | private final SOAPVersion soapVersion; |
aoqi@0 | 80 | |
aoqi@0 | 81 | protected JAXB(XMLBridge bridge, SOAPVersion soapVersion) { |
aoqi@0 | 82 | assert bridge!=null; |
aoqi@0 | 83 | this.bridge = bridge; |
aoqi@0 | 84 | this.soapVersion = soapVersion; |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | final Message createMessage(Object[] methodArgs) { |
aoqi@0 | 88 | return JAXBMessage.create( bridge, build(methodArgs), soapVersion ); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | /** |
aoqi@0 | 92 | * Builds a JAXB object that becomes the payload. |
aoqi@0 | 93 | */ |
aoqi@0 | 94 | abstract Object build(Object[] methodArgs); |
aoqi@0 | 95 | } |
aoqi@0 | 96 | |
aoqi@0 | 97 | /** |
aoqi@0 | 98 | * Used to create a payload JAXB object just by taking |
aoqi@0 | 99 | * one of the parameters. |
aoqi@0 | 100 | */ |
aoqi@0 | 101 | final static class Bare extends JAXB { |
aoqi@0 | 102 | /** |
aoqi@0 | 103 | * The index of the method invocation parameters that goes into the payload. |
aoqi@0 | 104 | */ |
aoqi@0 | 105 | private final int methodPos; |
aoqi@0 | 106 | |
aoqi@0 | 107 | private final ValueGetter getter; |
aoqi@0 | 108 | |
aoqi@0 | 109 | /** |
aoqi@0 | 110 | * Creates a {@link BodyBuilder} from a bare parameter. |
aoqi@0 | 111 | */ |
aoqi@0 | 112 | Bare(ParameterImpl p, SOAPVersion soapVersion, ValueGetter getter) { |
aoqi@0 | 113 | super(p.getXMLBridge(), soapVersion); |
aoqi@0 | 114 | this.methodPos = p.getIndex(); |
aoqi@0 | 115 | this.getter = getter; |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | /** |
aoqi@0 | 119 | * Picks up an object from the method arguments and uses it. |
aoqi@0 | 120 | */ |
aoqi@0 | 121 | Object build(Object[] methodArgs) { |
aoqi@0 | 122 | return getter.get(methodArgs[methodPos]); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | |
aoqi@0 | 127 | /** |
aoqi@0 | 128 | * Used to handle a 'wrapper' style request. |
aoqi@0 | 129 | * Common part of rpc/lit and doc/lit. |
aoqi@0 | 130 | */ |
aoqi@0 | 131 | abstract static class Wrapped extends JAXB { |
aoqi@0 | 132 | |
aoqi@0 | 133 | /** |
aoqi@0 | 134 | * Where in the method argument list do they come from? |
aoqi@0 | 135 | */ |
aoqi@0 | 136 | protected final int[] indices; |
aoqi@0 | 137 | |
aoqi@0 | 138 | /** |
aoqi@0 | 139 | * Abstracts away the {@link Holder} handling when touching method arguments. |
aoqi@0 | 140 | */ |
aoqi@0 | 141 | protected final ValueGetter[] getters; |
aoqi@0 | 142 | |
aoqi@0 | 143 | /** |
aoqi@0 | 144 | * How does each wrapped parameter binds to XML? |
aoqi@0 | 145 | */ |
aoqi@0 | 146 | protected XMLBridge[] parameterBridges; |
aoqi@0 | 147 | |
aoqi@0 | 148 | /** |
aoqi@0 | 149 | * List of Parameters packed in the body. |
aoqi@0 | 150 | * Only used for error diagnostics. |
aoqi@0 | 151 | */ |
aoqi@0 | 152 | protected List<ParameterImpl> children; |
aoqi@0 | 153 | |
aoqi@0 | 154 | protected Wrapped(WrapperParameter wp, SOAPVersion soapVersion, ValueGetterFactory getter) { |
aoqi@0 | 155 | super(wp.getXMLBridge(), soapVersion); |
aoqi@0 | 156 | children = wp.getWrapperChildren(); |
aoqi@0 | 157 | indices = new int[children.size()]; |
aoqi@0 | 158 | getters = new ValueGetter[children.size()]; |
aoqi@0 | 159 | for( int i=0; i<indices.length; i++ ) { |
aoqi@0 | 160 | ParameterImpl p = children.get(i); |
aoqi@0 | 161 | indices[i] = p.getIndex(); |
aoqi@0 | 162 | getters[i] = getter.get(p); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | /** |
aoqi@0 | 167 | * Packs a bunch of arguments into a {@link WrapperComposite}. |
aoqi@0 | 168 | */ |
aoqi@0 | 169 | protected WrapperComposite buildWrapperComposite(Object[] methodArgs) { |
aoqi@0 | 170 | WrapperComposite cs = new WrapperComposite(); |
aoqi@0 | 171 | cs.bridges = parameterBridges; |
aoqi@0 | 172 | cs.values = new Object[parameterBridges.length]; |
aoqi@0 | 173 | |
aoqi@0 | 174 | // fill in wrapped parameters from methodArgs |
aoqi@0 | 175 | for( int i=indices.length-1; i>=0; i-- ) { |
aoqi@0 | 176 | Object arg = getters[i].get(methodArgs[indices[i]]); |
aoqi@0 | 177 | if(arg==null) { |
aoqi@0 | 178 | throw new WebServiceException("Method Parameter: "+ |
aoqi@0 | 179 | children.get(i).getName()+" cannot be null. This is BP 1.1 R2211 violation."); |
aoqi@0 | 180 | } |
aoqi@0 | 181 | cs.values[i] = arg; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | return cs; |
aoqi@0 | 185 | } |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | /** |
aoqi@0 | 189 | * Used to create a payload JAXB object by wrapping |
aoqi@0 | 190 | * multiple parameters into one "wrapper bean". |
aoqi@0 | 191 | */ |
aoqi@0 | 192 | final static class DocLit extends Wrapped { |
aoqi@0 | 193 | /** |
aoqi@0 | 194 | * How does each wrapped parameter binds to XML? |
aoqi@0 | 195 | */ |
aoqi@0 | 196 | private final PropertyAccessor[] accessors; |
aoqi@0 | 197 | |
aoqi@0 | 198 | /** |
aoqi@0 | 199 | * Wrapper bean. |
aoqi@0 | 200 | */ |
aoqi@0 | 201 | private final Class wrapper; |
aoqi@0 | 202 | |
aoqi@0 | 203 | /** |
aoqi@0 | 204 | * Needed to get wrapper instantiation method. |
aoqi@0 | 205 | */ |
aoqi@0 | 206 | private BindingContext bindingContext; |
aoqi@0 | 207 | private boolean dynamicWrapper; |
aoqi@0 | 208 | |
aoqi@0 | 209 | /** |
aoqi@0 | 210 | * Creates a {@link BodyBuilder} from a {@link WrapperParameter}. |
aoqi@0 | 211 | */ |
aoqi@0 | 212 | DocLit(WrapperParameter wp, SOAPVersion soapVersion, ValueGetterFactory getter) { |
aoqi@0 | 213 | super(wp, soapVersion, getter); |
aoqi@0 | 214 | bindingContext = wp.getOwner().getBindingContext(); |
aoqi@0 | 215 | wrapper = (Class)wp.getXMLBridge().getTypeInfo().type; |
aoqi@0 | 216 | dynamicWrapper = WrapperComposite.class.equals(wrapper); |
aoqi@0 | 217 | parameterBridges = new XMLBridge[children.size()]; |
aoqi@0 | 218 | accessors = new PropertyAccessor[children.size()]; |
aoqi@0 | 219 | for( int i=0; i<accessors.length; i++ ) { |
aoqi@0 | 220 | ParameterImpl p = children.get(i); |
aoqi@0 | 221 | QName name = p.getName(); |
aoqi@0 | 222 | if (dynamicWrapper) { |
aoqi@0 | 223 | parameterBridges[i] = children.get(i).getInlinedRepeatedElementBridge(); |
aoqi@0 | 224 | if (parameterBridges[i] == null) parameterBridges[i] = children.get(i).getXMLBridge(); |
aoqi@0 | 225 | } else { |
aoqi@0 | 226 | try { |
aoqi@0 | 227 | accessors[i] = p.getOwner().getBindingContext().getElementPropertyAccessor( |
aoqi@0 | 228 | wrapper, name.getNamespaceURI(), name.getLocalPart() ); |
aoqi@0 | 229 | } catch (JAXBException e) { |
aoqi@0 | 230 | throw new WebServiceException( // TODO: i18n |
aoqi@0 | 231 | wrapper+" do not have a property of the name "+name,e); |
aoqi@0 | 232 | } |
aoqi@0 | 233 | } |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | } |
aoqi@0 | 237 | |
aoqi@0 | 238 | /** |
aoqi@0 | 239 | * Packs a bunch of arguments into a {@link WrapperComposite}. |
aoqi@0 | 240 | */ |
aoqi@0 | 241 | Object build(Object[] methodArgs) { |
aoqi@0 | 242 | if (dynamicWrapper) return buildWrapperComposite(methodArgs); |
aoqi@0 | 243 | try { |
aoqi@0 | 244 | //Object bean = wrapper.newInstance(); |
aoqi@0 | 245 | Object bean = bindingContext.newWrapperInstace(wrapper); |
aoqi@0 | 246 | |
aoqi@0 | 247 | // fill in wrapped parameters from methodArgs |
aoqi@0 | 248 | for( int i=indices.length-1; i>=0; i-- ) { |
aoqi@0 | 249 | accessors[i].set(bean,getters[i].get(methodArgs[indices[i]])); |
aoqi@0 | 250 | } |
aoqi@0 | 251 | |
aoqi@0 | 252 | return bean; |
aoqi@0 | 253 | } catch (InstantiationException e) { |
aoqi@0 | 254 | // this is irrecoverable |
aoqi@0 | 255 | Error x = new InstantiationError(e.getMessage()); |
aoqi@0 | 256 | x.initCause(e); |
aoqi@0 | 257 | throw x; |
aoqi@0 | 258 | } catch (IllegalAccessException e) { |
aoqi@0 | 259 | // this is irrecoverable |
aoqi@0 | 260 | Error x = new IllegalAccessError(e.getMessage()); |
aoqi@0 | 261 | x.initCause(e); |
aoqi@0 | 262 | throw x; |
aoqi@0 | 263 | } catch (com.sun.xml.internal.ws.spi.db.DatabindingException e) { |
aoqi@0 | 264 | // this can happen when the set method throw a checked exception or something like that |
aoqi@0 | 265 | throw new WebServiceException(e); // TODO:i18n |
aoqi@0 | 266 | } |
aoqi@0 | 267 | } |
aoqi@0 | 268 | } |
aoqi@0 | 269 | |
aoqi@0 | 270 | |
aoqi@0 | 271 | /** |
aoqi@0 | 272 | * Used to create a payload JAXB object by wrapping |
aoqi@0 | 273 | * multiple parameters into a {@link WrapperComposite}. |
aoqi@0 | 274 | * |
aoqi@0 | 275 | * <p> |
aoqi@0 | 276 | * This is used for rpc/lit, as we don't have a wrapper bean for it. |
aoqi@0 | 277 | * (TODO: Why don't we have a wrapper bean for this, when doc/lit does!?) |
aoqi@0 | 278 | */ |
aoqi@0 | 279 | final static class RpcLit extends Wrapped { |
aoqi@0 | 280 | |
aoqi@0 | 281 | /** |
aoqi@0 | 282 | * Creates a {@link BodyBuilder} from a {@link WrapperParameter}. |
aoqi@0 | 283 | */ |
aoqi@0 | 284 | RpcLit(WrapperParameter wp, SOAPVersion soapVersion, ValueGetterFactory getter) { |
aoqi@0 | 285 | super(wp, soapVersion, getter); |
aoqi@0 | 286 | // we'll use CompositeStructure to pack requests |
aoqi@0 | 287 | assert wp.getTypeInfo().type==WrapperComposite.class; |
aoqi@0 | 288 | |
aoqi@0 | 289 | parameterBridges = new XMLBridge[children.size()]; |
aoqi@0 | 290 | for( int i=0; i<parameterBridges.length; i++ ) |
aoqi@0 | 291 | parameterBridges[i] = children.get(i).getXMLBridge(); |
aoqi@0 | 292 | } |
aoqi@0 | 293 | |
aoqi@0 | 294 | Object build(Object[] methodArgs) { |
aoqi@0 | 295 | return buildWrapperComposite(methodArgs); |
aoqi@0 | 296 | } |
aoqi@0 | 297 | } |
aoqi@0 | 298 | } |