1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/client/sei/BodyBuilder.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,298 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.client.sei; 1.30 + 1.31 +import com.sun.xml.internal.ws.api.SOAPVersion; 1.32 +import com.sun.xml.internal.ws.api.message.Message; 1.33 +import com.sun.xml.internal.ws.api.message.Messages; 1.34 +import com.sun.xml.internal.ws.message.jaxb.JAXBMessage; 1.35 +import com.sun.xml.internal.ws.model.ParameterImpl; 1.36 +import com.sun.xml.internal.ws.model.WrapperParameter; 1.37 +import com.sun.xml.internal.ws.spi.db.BindingContext; 1.38 +import com.sun.xml.internal.ws.spi.db.XMLBridge; 1.39 +import com.sun.xml.internal.ws.spi.db.PropertyAccessor; 1.40 +import com.sun.xml.internal.ws.spi.db.WrapperComposite; 1.41 + 1.42 +import javax.xml.bind.JAXBException; 1.43 +import javax.xml.namespace.QName; 1.44 +import javax.xml.ws.Holder; 1.45 +import javax.xml.ws.WebServiceException; 1.46 +import java.util.List; 1.47 + 1.48 +/** 1.49 + * Builds a JAXB object that represents the payload. 1.50 + * 1.51 + * @see MessageFiller 1.52 + * @author Kohsuke Kawaguchi 1.53 + */ 1.54 +abstract class BodyBuilder { 1.55 + abstract Message createMessage(Object[] methodArgs); 1.56 + 1.57 + static final BodyBuilder EMPTY_SOAP11 = new Empty(SOAPVersion.SOAP_11); 1.58 + static final BodyBuilder EMPTY_SOAP12 = new Empty(SOAPVersion.SOAP_12); 1.59 + 1.60 + private static final class Empty extends BodyBuilder { 1.61 + private final SOAPVersion soapVersion; 1.62 + 1.63 + public Empty(SOAPVersion soapVersion) { 1.64 + this.soapVersion = soapVersion; 1.65 + } 1.66 + 1.67 + Message createMessage(Object[] methodArgs) { 1.68 + return Messages.createEmpty(soapVersion); 1.69 + } 1.70 + } 1.71 + 1.72 + /** 1.73 + * Base class for those {@link BodyBuilder}s that build a {@link Message} 1.74 + * from JAXB objects. 1.75 + */ 1.76 + private static abstract class JAXB extends BodyBuilder { 1.77 + /** 1.78 + * This object determines the binding of the object returned 1.79 + * from {@link #build(Object[])}. 1.80 + */ 1.81 + private final XMLBridge bridge; 1.82 + private final SOAPVersion soapVersion; 1.83 + 1.84 + protected JAXB(XMLBridge bridge, SOAPVersion soapVersion) { 1.85 + assert bridge!=null; 1.86 + this.bridge = bridge; 1.87 + this.soapVersion = soapVersion; 1.88 + } 1.89 + 1.90 + final Message createMessage(Object[] methodArgs) { 1.91 + return JAXBMessage.create( bridge, build(methodArgs), soapVersion ); 1.92 + } 1.93 + 1.94 + /** 1.95 + * Builds a JAXB object that becomes the payload. 1.96 + */ 1.97 + abstract Object build(Object[] methodArgs); 1.98 + } 1.99 + 1.100 + /** 1.101 + * Used to create a payload JAXB object just by taking 1.102 + * one of the parameters. 1.103 + */ 1.104 + final static class Bare extends JAXB { 1.105 + /** 1.106 + * The index of the method invocation parameters that goes into the payload. 1.107 + */ 1.108 + private final int methodPos; 1.109 + 1.110 + private final ValueGetter getter; 1.111 + 1.112 + /** 1.113 + * Creates a {@link BodyBuilder} from a bare parameter. 1.114 + */ 1.115 + Bare(ParameterImpl p, SOAPVersion soapVersion, ValueGetter getter) { 1.116 + super(p.getXMLBridge(), soapVersion); 1.117 + this.methodPos = p.getIndex(); 1.118 + this.getter = getter; 1.119 + } 1.120 + 1.121 + /** 1.122 + * Picks up an object from the method arguments and uses it. 1.123 + */ 1.124 + Object build(Object[] methodArgs) { 1.125 + return getter.get(methodArgs[methodPos]); 1.126 + } 1.127 + } 1.128 + 1.129 + 1.130 + /** 1.131 + * Used to handle a 'wrapper' style request. 1.132 + * Common part of rpc/lit and doc/lit. 1.133 + */ 1.134 + abstract static class Wrapped extends JAXB { 1.135 + 1.136 + /** 1.137 + * Where in the method argument list do they come from? 1.138 + */ 1.139 + protected final int[] indices; 1.140 + 1.141 + /** 1.142 + * Abstracts away the {@link Holder} handling when touching method arguments. 1.143 + */ 1.144 + protected final ValueGetter[] getters; 1.145 + 1.146 + /** 1.147 + * How does each wrapped parameter binds to XML? 1.148 + */ 1.149 + protected XMLBridge[] parameterBridges; 1.150 + 1.151 + /** 1.152 + * List of Parameters packed in the body. 1.153 + * Only used for error diagnostics. 1.154 + */ 1.155 + protected List<ParameterImpl> children; 1.156 + 1.157 + protected Wrapped(WrapperParameter wp, SOAPVersion soapVersion, ValueGetterFactory getter) { 1.158 + super(wp.getXMLBridge(), soapVersion); 1.159 + children = wp.getWrapperChildren(); 1.160 + indices = new int[children.size()]; 1.161 + getters = new ValueGetter[children.size()]; 1.162 + for( int i=0; i<indices.length; i++ ) { 1.163 + ParameterImpl p = children.get(i); 1.164 + indices[i] = p.getIndex(); 1.165 + getters[i] = getter.get(p); 1.166 + } 1.167 + } 1.168 + 1.169 + /** 1.170 + * Packs a bunch of arguments into a {@link WrapperComposite}. 1.171 + */ 1.172 + protected WrapperComposite buildWrapperComposite(Object[] methodArgs) { 1.173 + WrapperComposite cs = new WrapperComposite(); 1.174 + cs.bridges = parameterBridges; 1.175 + cs.values = new Object[parameterBridges.length]; 1.176 + 1.177 + // fill in wrapped parameters from methodArgs 1.178 + for( int i=indices.length-1; i>=0; i-- ) { 1.179 + Object arg = getters[i].get(methodArgs[indices[i]]); 1.180 + if(arg==null) { 1.181 + throw new WebServiceException("Method Parameter: "+ 1.182 + children.get(i).getName()+" cannot be null. This is BP 1.1 R2211 violation."); 1.183 + } 1.184 + cs.values[i] = arg; 1.185 + } 1.186 + 1.187 + return cs; 1.188 + } 1.189 + } 1.190 + 1.191 + /** 1.192 + * Used to create a payload JAXB object by wrapping 1.193 + * multiple parameters into one "wrapper bean". 1.194 + */ 1.195 + final static class DocLit extends Wrapped { 1.196 + /** 1.197 + * How does each wrapped parameter binds to XML? 1.198 + */ 1.199 + private final PropertyAccessor[] accessors; 1.200 + 1.201 + /** 1.202 + * Wrapper bean. 1.203 + */ 1.204 + private final Class wrapper; 1.205 + 1.206 + /** 1.207 + * Needed to get wrapper instantiation method. 1.208 + */ 1.209 + private BindingContext bindingContext; 1.210 + private boolean dynamicWrapper; 1.211 + 1.212 + /** 1.213 + * Creates a {@link BodyBuilder} from a {@link WrapperParameter}. 1.214 + */ 1.215 + DocLit(WrapperParameter wp, SOAPVersion soapVersion, ValueGetterFactory getter) { 1.216 + super(wp, soapVersion, getter); 1.217 + bindingContext = wp.getOwner().getBindingContext(); 1.218 + wrapper = (Class)wp.getXMLBridge().getTypeInfo().type; 1.219 + dynamicWrapper = WrapperComposite.class.equals(wrapper); 1.220 + parameterBridges = new XMLBridge[children.size()]; 1.221 + accessors = new PropertyAccessor[children.size()]; 1.222 + for( int i=0; i<accessors.length; i++ ) { 1.223 + ParameterImpl p = children.get(i); 1.224 + QName name = p.getName(); 1.225 + if (dynamicWrapper) { 1.226 + parameterBridges[i] = children.get(i).getInlinedRepeatedElementBridge(); 1.227 + if (parameterBridges[i] == null) parameterBridges[i] = children.get(i).getXMLBridge(); 1.228 + } else { 1.229 + try { 1.230 + accessors[i] = p.getOwner().getBindingContext().getElementPropertyAccessor( 1.231 + wrapper, name.getNamespaceURI(), name.getLocalPart() ); 1.232 + } catch (JAXBException e) { 1.233 + throw new WebServiceException( // TODO: i18n 1.234 + wrapper+" do not have a property of the name "+name,e); 1.235 + } 1.236 + } 1.237 + } 1.238 + 1.239 + } 1.240 + 1.241 + /** 1.242 + * Packs a bunch of arguments into a {@link WrapperComposite}. 1.243 + */ 1.244 + Object build(Object[] methodArgs) { 1.245 + if (dynamicWrapper) return buildWrapperComposite(methodArgs); 1.246 + try { 1.247 + //Object bean = wrapper.newInstance(); 1.248 + Object bean = bindingContext.newWrapperInstace(wrapper); 1.249 + 1.250 + // fill in wrapped parameters from methodArgs 1.251 + for( int i=indices.length-1; i>=0; i-- ) { 1.252 + accessors[i].set(bean,getters[i].get(methodArgs[indices[i]])); 1.253 + } 1.254 + 1.255 + return bean; 1.256 + } catch (InstantiationException e) { 1.257 + // this is irrecoverable 1.258 + Error x = new InstantiationError(e.getMessage()); 1.259 + x.initCause(e); 1.260 + throw x; 1.261 + } catch (IllegalAccessException e) { 1.262 + // this is irrecoverable 1.263 + Error x = new IllegalAccessError(e.getMessage()); 1.264 + x.initCause(e); 1.265 + throw x; 1.266 + } catch (com.sun.xml.internal.ws.spi.db.DatabindingException e) { 1.267 + // this can happen when the set method throw a checked exception or something like that 1.268 + throw new WebServiceException(e); // TODO:i18n 1.269 + } 1.270 + } 1.271 + } 1.272 + 1.273 + 1.274 + /** 1.275 + * Used to create a payload JAXB object by wrapping 1.276 + * multiple parameters into a {@link WrapperComposite}. 1.277 + * 1.278 + * <p> 1.279 + * This is used for rpc/lit, as we don't have a wrapper bean for it. 1.280 + * (TODO: Why don't we have a wrapper bean for this, when doc/lit does!?) 1.281 + */ 1.282 + final static class RpcLit extends Wrapped { 1.283 + 1.284 + /** 1.285 + * Creates a {@link BodyBuilder} from a {@link WrapperParameter}. 1.286 + */ 1.287 + RpcLit(WrapperParameter wp, SOAPVersion soapVersion, ValueGetterFactory getter) { 1.288 + super(wp, soapVersion, getter); 1.289 + // we'll use CompositeStructure to pack requests 1.290 + assert wp.getTypeInfo().type==WrapperComposite.class; 1.291 + 1.292 + parameterBridges = new XMLBridge[children.size()]; 1.293 + for( int i=0; i<parameterBridges.length; i++ ) 1.294 + parameterBridges[i] = children.get(i).getXMLBridge(); 1.295 + } 1.296 + 1.297 + Object build(Object[] methodArgs) { 1.298 + return buildWrapperComposite(methodArgs); 1.299 + } 1.300 + } 1.301 +}