src/share/jaxws_classes/com/sun/xml/internal/ws/server/sei/EndpointResponseMessageBuilder.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/server/sei/EndpointResponseMessageBuilder.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,318 @@
     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.server.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 Jitendra Kotamraju
    1.53 + */
    1.54 +public abstract class EndpointResponseMessageBuilder {
    1.55 +    public abstract Message createMessage(Object[] methodArgs, Object returnValue);
    1.56 +
    1.57 +    public static final EndpointResponseMessageBuilder EMPTY_SOAP11 = new Empty(SOAPVersion.SOAP_11);
    1.58 +    public static final EndpointResponseMessageBuilder EMPTY_SOAP12 = new Empty(SOAPVersion.SOAP_12);
    1.59 +
    1.60 +    private static final class Empty extends EndpointResponseMessageBuilder {
    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 +        public Message createMessage(Object[] methodArgs, Object returnValue) {
    1.68 +            return Messages.createEmpty(soapVersion);
    1.69 +        }
    1.70 +    }
    1.71 +
    1.72 +    /**
    1.73 +     * Base class for those {@link EndpointResponseMessageBuilder}s that build a {@link Message}
    1.74 +     * from JAXB objects.
    1.75 +     */
    1.76 +    private static abstract class JAXB extends EndpointResponseMessageBuilder {
    1.77 +        /**
    1.78 +         * This object determines the binding of the object returned
    1.79 +         * from {@link #createMessage(Object[], 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 +        public final Message createMessage(Object[] methodArgs, Object returnValue) {
    1.91 +            return JAXBMessage.create( bridge, build(methodArgs, returnValue), 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, Object returnValue);
    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 +    public 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 EndpointResponseMessageBuilder} from a bare parameter.
   1.114 +         */
   1.115 +        public Bare(ParameterImpl p, SOAPVersion soapVersion) {
   1.116 +            super(p.getXMLBridge(), soapVersion);
   1.117 +            this.methodPos = p.getIndex();
   1.118 +            this.getter = ValueGetter.get(p);
   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, Object returnValue) {
   1.125 +            if (methodPos == -1) {
   1.126 +                return returnValue;
   1.127 +            }
   1.128 +            return getter.get(methodArgs[methodPos]);
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +
   1.133 +    /**
   1.134 +     * Used to handle a 'wrapper' style request.
   1.135 +     * Common part of rpc/lit and doc/lit.
   1.136 +     */
   1.137 +    abstract static class Wrapped extends JAXB {
   1.138 +
   1.139 +        /**
   1.140 +         * Where in the method argument list do they come from?
   1.141 +         */
   1.142 +        protected final int[] indices;
   1.143 +
   1.144 +        /**
   1.145 +         * Abstracts away the {@link Holder} handling when touching method arguments.
   1.146 +         */
   1.147 +        protected final ValueGetter[] getters;
   1.148 +
   1.149 +        /**
   1.150 +         * How does each wrapped parameter binds to XML?
   1.151 +         */
   1.152 +        protected XMLBridge[] parameterBridges;
   1.153 +
   1.154 +        /**
   1.155 +         * Used for error diagnostics.
   1.156 +         */
   1.157 +        protected List<ParameterImpl> children;
   1.158 +
   1.159 +        protected Wrapped(WrapperParameter wp, SOAPVersion soapVersion) {
   1.160 +            super(wp.getXMLBridge(), soapVersion);
   1.161 +
   1.162 +            children = wp.getWrapperChildren();
   1.163 +
   1.164 +            indices = new int[children.size()];
   1.165 +            getters = new ValueGetter[children.size()];
   1.166 +            for( int i=0; i<indices.length; i++ ) {
   1.167 +                ParameterImpl p = children.get(i);
   1.168 +                indices[i] = p.getIndex();
   1.169 +                getters[i] = ValueGetter.get(p);
   1.170 +            }
   1.171 +        }
   1.172 +
   1.173 +        /**
   1.174 +         * Packs a bunch of arguments intoa {@link WrapperComposite}.
   1.175 +         */
   1.176 +        WrapperComposite buildWrapperComposite(Object[] methodArgs, Object returnValue) {
   1.177 +            WrapperComposite cs = new WrapperComposite();
   1.178 +            cs.bridges = parameterBridges;
   1.179 +            cs.values = new Object[parameterBridges.length];
   1.180 +
   1.181 +            // fill in wrapped parameters from methodArgs
   1.182 +            for( int i=indices.length-1; i>=0; i-- ) {
   1.183 +                Object v;
   1.184 +                if (indices[i] == -1) {
   1.185 +                    v = getters[i].get(returnValue);
   1.186 +                } else {
   1.187 +                    v = getters[i].get(methodArgs[indices[i]]);
   1.188 +                }
   1.189 +                if(v==null) {
   1.190 +                    throw new WebServiceException("Method Parameter: "+
   1.191 +                        children.get(i).getName() +" cannot be null. This is BP 1.1 R2211 violation.");
   1.192 +                }
   1.193 +                cs.values[i] = v;
   1.194 +            }
   1.195 +
   1.196 +            return cs;
   1.197 +        }
   1.198 +    }
   1.199 +
   1.200 +    /**
   1.201 +     * Used to create a payload JAXB object by wrapping
   1.202 +     * multiple parameters into one "wrapper bean".
   1.203 +     */
   1.204 +    public final static class DocLit extends Wrapped {
   1.205 +        /**
   1.206 +         * How does each wrapped parameter binds to XML?
   1.207 +         */
   1.208 +        private final PropertyAccessor[] accessors;
   1.209 +
   1.210 +        //private final RawAccessor retAccessor;
   1.211 +
   1.212 +        /**
   1.213 +         * Wrapper bean.
   1.214 +         */
   1.215 +        private final Class wrapper;
   1.216 +        private boolean dynamicWrapper;
   1.217 +
   1.218 +        /**
   1.219 +         * Needed to get wrapper instantiation method.
   1.220 +         */
   1.221 +        private BindingContext bindingContext;
   1.222 +
   1.223 +        /**
   1.224 +         * Creates a {@link EndpointResponseMessageBuilder} from a {@link WrapperParameter}.
   1.225 +         */
   1.226 +        public DocLit(WrapperParameter wp, SOAPVersion soapVersion) {
   1.227 +            super(wp, soapVersion);
   1.228 +            bindingContext = wp.getOwner().getBindingContext();
   1.229 +            wrapper = (Class)wp.getXMLBridge().getTypeInfo().type;
   1.230 +            dynamicWrapper = WrapperComposite.class.equals(wrapper);
   1.231 +            children = wp.getWrapperChildren();
   1.232 +            parameterBridges = new XMLBridge[children.size()];
   1.233 +            accessors = new PropertyAccessor[children.size()];
   1.234 +            for( int i=0; i<accessors.length; i++ ) {
   1.235 +                ParameterImpl p = children.get(i);
   1.236 +                QName name = p.getName();
   1.237 +                if (dynamicWrapper) {
   1.238 +                    parameterBridges[i] = children.get(i).getInlinedRepeatedElementBridge();
   1.239 +                    if (parameterBridges[i] == null) parameterBridges[i] = children.get(i).getXMLBridge();
   1.240 +                } else {
   1.241 +                    try {
   1.242 +                        accessors[i] = (dynamicWrapper) ? null :
   1.243 +                            p.getOwner().getBindingContext().getElementPropertyAccessor(
   1.244 +                            wrapper, name.getNamespaceURI(), name.getLocalPart() );
   1.245 +                    } catch (JAXBException e) {
   1.246 +                        throw new WebServiceException(  // TODO: i18n
   1.247 +                            wrapper+" do not have a property of the name "+name,e);
   1.248 +                    }
   1.249 +                }
   1.250 +            }
   1.251 +
   1.252 +        }
   1.253 +
   1.254 +        /**
   1.255 +         * Packs a bunch of arguments into a {@link WrapperComposite}.
   1.256 +         */
   1.257 +        Object build(Object[] methodArgs, Object returnValue) {
   1.258 +            if (dynamicWrapper) return buildWrapperComposite(methodArgs, returnValue);
   1.259 +            try {
   1.260 +                //Object bean = wrapper.newInstance();
   1.261 +                Object bean = bindingContext.newWrapperInstace(wrapper);
   1.262 +
   1.263 +                // fill in wrapped parameters from methodArgs
   1.264 +                for( int i=indices.length-1; i>=0; i-- ) {
   1.265 +                    if (indices[i] == -1) {
   1.266 +                        accessors[i].set(bean, returnValue);
   1.267 +                    } else {
   1.268 +                        accessors[i].set(bean,getters[i].get(methodArgs[indices[i]]));
   1.269 +                    }
   1.270 +                }
   1.271 +
   1.272 +                return bean;
   1.273 +            } catch (InstantiationException e) {
   1.274 +                // this is irrecoverable
   1.275 +                Error x = new InstantiationError(e.getMessage());
   1.276 +                x.initCause(e);
   1.277 +                throw x;
   1.278 +            } catch (IllegalAccessException e) {
   1.279 +                // this is irrecoverable
   1.280 +                Error x = new IllegalAccessError(e.getMessage());
   1.281 +                x.initCause(e);
   1.282 +                throw x;
   1.283 +            } catch (com.sun.xml.internal.ws.spi.db.DatabindingException e) {
   1.284 +                // this can happen when the set method throw a checked exception or something like that
   1.285 +                throw new WebServiceException(e);    // TODO:i18n
   1.286 +            }
   1.287 +        }
   1.288 +    }
   1.289 +
   1.290 +
   1.291 +    /**
   1.292 +     * Used to create a payload JAXB object by wrapping
   1.293 +     * multiple parameters into a {@link WrapperComposite}.
   1.294 +     *
   1.295 +     * <p>
   1.296 +     * This is used for rpc/lit, as we don't have a wrapper bean for it.
   1.297 +     * (TODO: Why don't we have a wrapper bean for this, when doc/lit does!?)
   1.298 +     */
   1.299 +    public final static class RpcLit extends Wrapped {
   1.300 +
   1.301 +        /**
   1.302 +         * Creates a {@link EndpointResponseMessageBuilder} from a {@link WrapperParameter}.
   1.303 +         */
   1.304 +        public RpcLit(WrapperParameter wp, SOAPVersion soapVersion) {
   1.305 +            super(wp, soapVersion);
   1.306 +            // we'll use CompositeStructure to pack requests
   1.307 +            assert wp.getTypeInfo().type==WrapperComposite.class;
   1.308 +
   1.309 +            parameterBridges = new XMLBridge[children.size()];
   1.310 +            for( int i=0; i<parameterBridges.length; i++ )
   1.311 +                parameterBridges[i] = children.get(i).getXMLBridge();
   1.312 +        }
   1.313 +
   1.314 +        /**
   1.315 +         * Packs a bunch of arguments intoa {@link WrapperComposite}.
   1.316 +         */
   1.317 +        Object build(Object[] methodArgs, Object returnValue) {
   1.318 +            return buildWrapperComposite(methodArgs, returnValue);
   1.319 +        }
   1.320 +    }
   1.321 +}

mercurial