src/share/jaxws_classes/com/sun/xml/internal/ws/server/sei/MessageFiller.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/MessageFiller.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,182 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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.message.Headers;
    1.32 +import com.sun.xml.internal.ws.api.message.Message;
    1.33 +import com.sun.xml.internal.ws.message.ByteArrayAttachment;
    1.34 +import com.sun.xml.internal.ws.message.DataHandlerAttachment;
    1.35 +import com.sun.xml.internal.ws.message.JAXBAttachment;
    1.36 +import com.sun.xml.internal.ws.model.ParameterImpl;
    1.37 +import com.sun.xml.internal.ws.spi.db.XMLBridge;
    1.38 +
    1.39 +import java.io.UnsupportedEncodingException;
    1.40 +import java.net.URLEncoder;
    1.41 +import java.util.UUID;
    1.42 +import javax.activation.DataHandler;
    1.43 +import javax.xml.transform.Source;
    1.44 +import javax.xml.ws.WebServiceException;
    1.45 +import com.sun.xml.internal.ws.api.message.Attachment;
    1.46 +
    1.47 +/**
    1.48 + * Puts a non-payload message parameter to {@link Message}.
    1.49 + *
    1.50 + * <p>
    1.51 + * Instance of this class is used to handle header parameters and attachment parameters.
    1.52 + * They add things to {@link Message}.
    1.53 + *
    1.54 + * @author Kohsuke Kawaguchi
    1.55 + * @author Jitendra Kotamraju
    1.56 + */
    1.57 +public abstract class MessageFiller {
    1.58 +
    1.59 +    /**
    1.60 +     * The index of the method invocation parameters that this object looks for.
    1.61 +     */
    1.62 +    protected final int methodPos;
    1.63 +
    1.64 +    protected MessageFiller( int methodPos) {
    1.65 +        this.methodPos = methodPos;
    1.66 +    }
    1.67 +
    1.68 +    /**
    1.69 +     * Moves an argument of a method invocation into a {@link Message}.
    1.70 +     */
    1.71 +    public abstract void fillIn(Object[] methodArgs, Object returnValue, Message msg);
    1.72 +
    1.73 +    /**
    1.74 +     * Adds a parameter as an MIME attachment to {@link Message}.
    1.75 +     */
    1.76 +    public static abstract class AttachmentFiller extends MessageFiller {
    1.77 +        protected final ParameterImpl param;
    1.78 +        protected final ValueGetter getter;
    1.79 +        protected final String mimeType;
    1.80 +        private final String contentIdPart;
    1.81 +
    1.82 +        protected AttachmentFiller(ParameterImpl param, ValueGetter getter) {
    1.83 +            super(param.getIndex());
    1.84 +            this.param = param;
    1.85 +            this.getter = getter;
    1.86 +            mimeType = param.getBinding().getMimeType();
    1.87 +            try {
    1.88 +                contentIdPart = URLEncoder.encode(param.getPartName(), "UTF-8")+'=';
    1.89 +            } catch (UnsupportedEncodingException e) {
    1.90 +                throw new WebServiceException(e);
    1.91 +            }
    1.92 +        }
    1.93 +
    1.94 +        /**
    1.95 +         * Creates an MessageFiller based on the parameter type
    1.96 +         *
    1.97 +         * @param param
    1.98 +         *      runtime Parameter that abstracts the annotated java parameter
    1.99 +         * @param getter
   1.100 +         *      Gets a value from an object that represents a parameter passed
   1.101 +         *      as a method argument.
   1.102 +         */
   1.103 +        public static MessageFiller createAttachmentFiller(ParameterImpl param, ValueGetter getter) {
   1.104 +            Class type = (Class)param.getTypeInfo().type;
   1.105 +            if (DataHandler.class.isAssignableFrom(type) || Source.class.isAssignableFrom(type)) {
   1.106 +                return new DataHandlerFiller(param, getter);
   1.107 +            } else if (byte[].class==type) {
   1.108 +                return new ByteArrayFiller(param, getter);
   1.109 +            } else if(isXMLMimeType(param.getBinding().getMimeType())) {
   1.110 +                return new JAXBFiller(param, getter);
   1.111 +            } else {
   1.112 +                return new DataHandlerFiller(param, getter);
   1.113 +            }
   1.114 +        }
   1.115 +
   1.116 +        String getContentId() {
   1.117 +            return contentIdPart+UUID.randomUUID()+"@jaxws.sun.com";
   1.118 +        }
   1.119 +    }
   1.120 +
   1.121 +    private static class ByteArrayFiller extends AttachmentFiller {
   1.122 +        protected ByteArrayFiller(ParameterImpl param, ValueGetter getter) {
   1.123 +            super(param, getter);
   1.124 +        }
   1.125 +
   1.126 +        public void fillIn(Object[] methodArgs, Object returnValue, Message msg) {
   1.127 +            String contentId = getContentId();
   1.128 +            Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]);
   1.129 +            if (obj != null) {
   1.130 +                Attachment att = new ByteArrayAttachment(contentId,(byte[])obj,mimeType);
   1.131 +                msg.getAttachments().add(att);
   1.132 +            }
   1.133 +        }
   1.134 +    }
   1.135 +
   1.136 +    private static class DataHandlerFiller extends AttachmentFiller {
   1.137 +        protected DataHandlerFiller(ParameterImpl param, ValueGetter getter) {
   1.138 +            super(param, getter);
   1.139 +        }
   1.140 +
   1.141 +        public void fillIn(Object[] methodArgs, Object returnValue, Message msg) {
   1.142 +            String contentId = getContentId();
   1.143 +            Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]);
   1.144 +            DataHandler dh = (obj instanceof DataHandler) ? (DataHandler)obj : new DataHandler(obj,mimeType);
   1.145 +            Attachment att = new DataHandlerAttachment(contentId, dh);
   1.146 +            msg.getAttachments().add(att);
   1.147 +        }
   1.148 +    }
   1.149 +
   1.150 +    private static class JAXBFiller extends AttachmentFiller {
   1.151 +        protected JAXBFiller(ParameterImpl param, ValueGetter getter) {
   1.152 +            super(param, getter);
   1.153 +        }
   1.154 +
   1.155 +        public void fillIn(Object[] methodArgs, Object returnValue, Message msg) {
   1.156 +            String contentId = getContentId();
   1.157 +            Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]);
   1.158 +            Attachment att = new JAXBAttachment(contentId, obj, param.getXMLBridge(), mimeType);
   1.159 +            msg.getAttachments().add(att);
   1.160 +        }
   1.161 +    }
   1.162 +
   1.163 +    /**
   1.164 +     * Adds a parameter as an header.
   1.165 +     */
   1.166 +    public static final class Header extends MessageFiller {
   1.167 +        private final XMLBridge bridge;
   1.168 +        private final ValueGetter getter;
   1.169 +
   1.170 +        public Header(int methodPos, XMLBridge bridge, ValueGetter getter) {
   1.171 +            super(methodPos);
   1.172 +            this.bridge = bridge;
   1.173 +            this.getter = getter;
   1.174 +        }
   1.175 +
   1.176 +        public void fillIn(Object[] methodArgs, Object returnValue, Message msg) {
   1.177 +            Object value = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]);
   1.178 +            msg.getHeaders().add(Headers.create(bridge,value));
   1.179 +        }
   1.180 +    }
   1.181 +
   1.182 +    private static boolean isXMLMimeType(String mimeType){
   1.183 +        return mimeType.equals("text/xml") || mimeType.equals("application/xml");
   1.184 +    }
   1.185 +}

mercurial