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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     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	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,305 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, 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 +        protected Wrapped(WrapperParameter wp, SOAPVersion soapVersion) {
   1.150 +            super(wp.getXMLBridge(), soapVersion);
   1.151 +
   1.152 +            List<ParameterImpl> children = wp.getWrapperChildren();
   1.153 +
   1.154 +            indices = new int[children.size()];
   1.155 +            getters = new ValueGetter[children.size()];
   1.156 +            for( int i=0; i<indices.length; i++ ) {
   1.157 +                ParameterImpl p = children.get(i);
   1.158 +                indices[i] = p.getIndex();
   1.159 +                getters[i] = ValueGetter.get(p);
   1.160 +            }
   1.161 +        }
   1.162 +    }
   1.163 +
   1.164 +    /**
   1.165 +     * Used to create a payload JAXB object by wrapping
   1.166 +     * multiple parameters into one "wrapper bean".
   1.167 +     */
   1.168 +    public final static class DocLit extends Wrapped {
   1.169 +        /**
   1.170 +         * How does each wrapped parameter binds to XML?
   1.171 +         */
   1.172 +        private final PropertyAccessor[] accessors;
   1.173 +
   1.174 +        //private final RawAccessor retAccessor;
   1.175 +
   1.176 +        /**
   1.177 +         * Wrapper bean.
   1.178 +         */
   1.179 +        private final Class wrapper;
   1.180 +
   1.181 +        /**
   1.182 +         * Needed to get wrapper instantiation method.
   1.183 +         */
   1.184 +        private BindingContext bindingContext;
   1.185 +
   1.186 +        /**
   1.187 +         * Creates a {@link EndpointResponseMessageBuilder} from a {@link WrapperParameter}.
   1.188 +         */
   1.189 +        public DocLit(WrapperParameter wp, SOAPVersion soapVersion) {
   1.190 +            super(wp, soapVersion);
   1.191 +            bindingContext = wp.getOwner().getBindingContext();
   1.192 +
   1.193 +            wrapper = (Class)wp.getXMLBridge().getTypeInfo().type;
   1.194 +
   1.195 +            List<ParameterImpl> children = wp.getWrapperChildren();
   1.196 +
   1.197 +            accessors = new PropertyAccessor[children.size()];
   1.198 +            for( int i=0; i<accessors.length; i++ ) {
   1.199 +                ParameterImpl p = children.get(i);
   1.200 +                QName name = p.getName();
   1.201 +                try {
   1.202 +                    accessors[i] = p.getOwner().getBindingContext().getElementPropertyAccessor(
   1.203 +                        wrapper, name.getNamespaceURI(), name.getLocalPart() );
   1.204 +                } catch (JAXBException e) {
   1.205 +                    throw new WebServiceException(  // TODO: i18n
   1.206 +                        wrapper+" do not have a property of the name "+name,e);
   1.207 +                }
   1.208 +            }
   1.209 +
   1.210 +        }
   1.211 +
   1.212 +        /**
   1.213 +         * Packs a bunch of arguments into a {@link WrapperComposite}.
   1.214 +         */
   1.215 +        Object build(Object[] methodArgs, Object returnValue) {
   1.216 +            try {
   1.217 +                //Object bean = wrapper.newInstance();
   1.218 +                Object bean = bindingContext.newWrapperInstace(wrapper);
   1.219 +
   1.220 +                // fill in wrapped parameters from methodArgs
   1.221 +                for( int i=indices.length-1; i>=0; i-- ) {
   1.222 +                    if (indices[i] == -1) {
   1.223 +                        accessors[i].set(bean, returnValue);
   1.224 +                    } else {
   1.225 +                        accessors[i].set(bean,getters[i].get(methodArgs[indices[i]]));
   1.226 +                    }
   1.227 +                }
   1.228 +
   1.229 +                return bean;
   1.230 +            } catch (InstantiationException e) {
   1.231 +                // this is irrecoverable
   1.232 +                Error x = new InstantiationError(e.getMessage());
   1.233 +                x.initCause(e);
   1.234 +                throw x;
   1.235 +            } catch (IllegalAccessException e) {
   1.236 +                // this is irrecoverable
   1.237 +                Error x = new IllegalAccessError(e.getMessage());
   1.238 +                x.initCause(e);
   1.239 +                throw x;
   1.240 +            } catch (com.sun.xml.internal.ws.spi.db.DatabindingException e) {
   1.241 +                // this can happen when the set method throw a checked exception or something like that
   1.242 +                throw new WebServiceException(e);    // TODO:i18n
   1.243 +            }
   1.244 +        }
   1.245 +    }
   1.246 +
   1.247 +
   1.248 +    /**
   1.249 +     * Used to create a payload JAXB object by wrapping
   1.250 +     * multiple parameters into a {@link WrapperComposite}.
   1.251 +     *
   1.252 +     * <p>
   1.253 +     * This is used for rpc/lit, as we don't have a wrapper bean for it.
   1.254 +     * (TODO: Why don't we have a wrapper bean for this, when doc/lit does!?)
   1.255 +     */
   1.256 +    public final static class RpcLit extends Wrapped {
   1.257 +        /**
   1.258 +         * How does each wrapped parameter binds to XML?
   1.259 +         */
   1.260 +        private final XMLBridge[] parameterBridges;
   1.261 +
   1.262 +        /**
   1.263 +         * Used for error diagnostics.
   1.264 +         */
   1.265 +        private final List<ParameterImpl> children;
   1.266 +
   1.267 +        /**
   1.268 +         * Creates a {@link EndpointResponseMessageBuilder} from a {@link WrapperParameter}.
   1.269 +         */
   1.270 +        public RpcLit(WrapperParameter wp, SOAPVersion soapVersion) {
   1.271 +            super(wp, soapVersion);
   1.272 +            // we'll use CompositeStructure to pack requests
   1.273 +            assert wp.getTypeInfo().type==WrapperComposite.class;
   1.274 +
   1.275 +            this.children = wp.getWrapperChildren();
   1.276 +
   1.277 +            parameterBridges = new XMLBridge[children.size()];
   1.278 +            for( int i=0; i<parameterBridges.length; i++ )
   1.279 +                parameterBridges[i] = children.get(i).getXMLBridge();
   1.280 +        }
   1.281 +
   1.282 +        /**
   1.283 +         * Packs a bunch of arguments intoa {@link WrapperComposite}.
   1.284 +         */
   1.285 +        WrapperComposite build(Object[] methodArgs, Object returnValue) {
   1.286 +            WrapperComposite cs = new WrapperComposite();
   1.287 +            cs.bridges = parameterBridges;
   1.288 +            cs.values = new Object[parameterBridges.length];
   1.289 +
   1.290 +            // fill in wrapped parameters from methodArgs
   1.291 +            for( int i=indices.length-1; i>=0; i-- ) {
   1.292 +                Object v;
   1.293 +                if (indices[i] == -1) {
   1.294 +                    v = getters[i].get(returnValue);
   1.295 +                } else {
   1.296 +                    v = getters[i].get(methodArgs[indices[i]]);
   1.297 +                }
   1.298 +                if(v==null) {
   1.299 +                    throw new WebServiceException("Method Parameter: "+
   1.300 +                        children.get(i).getName() +" cannot be null. This is BP 1.1 R2211 violation.");
   1.301 +                }
   1.302 +                cs.values[i] = v;
   1.303 +            }
   1.304 +
   1.305 +            return cs;
   1.306 +        }
   1.307 +    }
   1.308 +}

mercurial