Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, 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.server.sei; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.xml.internal.ws.api.message.Headers; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.api.message.Message; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.message.ByteArrayAttachment; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.message.DataHandlerAttachment; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.message.JAXBAttachment; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.model.ParameterImpl; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.spi.db.XMLBridge; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import java.io.UnsupportedEncodingException; |
aoqi@0 | 37 | import java.net.URLEncoder; |
aoqi@0 | 38 | import java.util.UUID; |
aoqi@0 | 39 | import javax.activation.DataHandler; |
aoqi@0 | 40 | import javax.xml.transform.Source; |
aoqi@0 | 41 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 42 | import com.sun.xml.internal.ws.api.message.Attachment; |
aoqi@0 | 43 | |
aoqi@0 | 44 | /** |
aoqi@0 | 45 | * Puts a non-payload message parameter to {@link Message}. |
aoqi@0 | 46 | * |
aoqi@0 | 47 | * <p> |
aoqi@0 | 48 | * Instance of this class is used to handle header parameters and attachment parameters. |
aoqi@0 | 49 | * They add things to {@link Message}. |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * @author Kohsuke Kawaguchi |
aoqi@0 | 52 | * @author Jitendra Kotamraju |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | public abstract class MessageFiller { |
aoqi@0 | 55 | |
aoqi@0 | 56 | /** |
aoqi@0 | 57 | * The index of the method invocation parameters that this object looks for. |
aoqi@0 | 58 | */ |
aoqi@0 | 59 | protected final int methodPos; |
aoqi@0 | 60 | |
aoqi@0 | 61 | protected MessageFiller( int methodPos) { |
aoqi@0 | 62 | this.methodPos = methodPos; |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | /** |
aoqi@0 | 66 | * Moves an argument of a method invocation into a {@link Message}. |
aoqi@0 | 67 | */ |
aoqi@0 | 68 | public abstract void fillIn(Object[] methodArgs, Object returnValue, Message msg); |
aoqi@0 | 69 | |
aoqi@0 | 70 | /** |
aoqi@0 | 71 | * Adds a parameter as an MIME attachment to {@link Message}. |
aoqi@0 | 72 | */ |
aoqi@0 | 73 | public static abstract class AttachmentFiller extends MessageFiller { |
aoqi@0 | 74 | protected final ParameterImpl param; |
aoqi@0 | 75 | protected final ValueGetter getter; |
aoqi@0 | 76 | protected final String mimeType; |
aoqi@0 | 77 | private final String contentIdPart; |
aoqi@0 | 78 | |
aoqi@0 | 79 | protected AttachmentFiller(ParameterImpl param, ValueGetter getter) { |
aoqi@0 | 80 | super(param.getIndex()); |
aoqi@0 | 81 | this.param = param; |
aoqi@0 | 82 | this.getter = getter; |
aoqi@0 | 83 | mimeType = param.getBinding().getMimeType(); |
aoqi@0 | 84 | try { |
aoqi@0 | 85 | contentIdPart = URLEncoder.encode(param.getPartName(), "UTF-8")+'='; |
aoqi@0 | 86 | } catch (UnsupportedEncodingException e) { |
aoqi@0 | 87 | throw new WebServiceException(e); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | /** |
aoqi@0 | 92 | * Creates an MessageFiller based on the parameter type |
aoqi@0 | 93 | * |
aoqi@0 | 94 | * @param param |
aoqi@0 | 95 | * runtime Parameter that abstracts the annotated java parameter |
aoqi@0 | 96 | * @param getter |
aoqi@0 | 97 | * Gets a value from an object that represents a parameter passed |
aoqi@0 | 98 | * as a method argument. |
aoqi@0 | 99 | */ |
aoqi@0 | 100 | public static MessageFiller createAttachmentFiller(ParameterImpl param, ValueGetter getter) { |
aoqi@0 | 101 | Class type = (Class)param.getTypeInfo().type; |
aoqi@0 | 102 | if (DataHandler.class.isAssignableFrom(type) || Source.class.isAssignableFrom(type)) { |
aoqi@0 | 103 | return new DataHandlerFiller(param, getter); |
aoqi@0 | 104 | } else if (byte[].class==type) { |
aoqi@0 | 105 | return new ByteArrayFiller(param, getter); |
aoqi@0 | 106 | } else if(isXMLMimeType(param.getBinding().getMimeType())) { |
aoqi@0 | 107 | return new JAXBFiller(param, getter); |
aoqi@0 | 108 | } else { |
aoqi@0 | 109 | return new DataHandlerFiller(param, getter); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | String getContentId() { |
aoqi@0 | 114 | return contentIdPart+UUID.randomUUID()+"@jaxws.sun.com"; |
aoqi@0 | 115 | } |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | private static class ByteArrayFiller extends AttachmentFiller { |
aoqi@0 | 119 | protected ByteArrayFiller(ParameterImpl param, ValueGetter getter) { |
aoqi@0 | 120 | super(param, getter); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | public void fillIn(Object[] methodArgs, Object returnValue, Message msg) { |
aoqi@0 | 124 | String contentId = getContentId(); |
aoqi@0 | 125 | Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]); |
aoqi@0 | 126 | if (obj != null) { |
aoqi@0 | 127 | Attachment att = new ByteArrayAttachment(contentId,(byte[])obj,mimeType); |
aoqi@0 | 128 | msg.getAttachments().add(att); |
aoqi@0 | 129 | } |
aoqi@0 | 130 | } |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | private static class DataHandlerFiller extends AttachmentFiller { |
aoqi@0 | 134 | protected DataHandlerFiller(ParameterImpl param, ValueGetter getter) { |
aoqi@0 | 135 | super(param, getter); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | public void fillIn(Object[] methodArgs, Object returnValue, Message msg) { |
aoqi@0 | 139 | String contentId = getContentId(); |
aoqi@0 | 140 | Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]); |
aoqi@0 | 141 | DataHandler dh = (obj instanceof DataHandler) ? (DataHandler)obj : new DataHandler(obj,mimeType); |
aoqi@0 | 142 | Attachment att = new DataHandlerAttachment(contentId, dh); |
aoqi@0 | 143 | msg.getAttachments().add(att); |
aoqi@0 | 144 | } |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | private static class JAXBFiller extends AttachmentFiller { |
aoqi@0 | 148 | protected JAXBFiller(ParameterImpl param, ValueGetter getter) { |
aoqi@0 | 149 | super(param, getter); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | public void fillIn(Object[] methodArgs, Object returnValue, Message msg) { |
aoqi@0 | 153 | String contentId = getContentId(); |
aoqi@0 | 154 | Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]); |
aoqi@0 | 155 | Attachment att = new JAXBAttachment(contentId, obj, param.getXMLBridge(), mimeType); |
aoqi@0 | 156 | msg.getAttachments().add(att); |
aoqi@0 | 157 | } |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | /** |
aoqi@0 | 161 | * Adds a parameter as an header. |
aoqi@0 | 162 | */ |
aoqi@0 | 163 | public static final class Header extends MessageFiller { |
aoqi@0 | 164 | private final XMLBridge bridge; |
aoqi@0 | 165 | private final ValueGetter getter; |
aoqi@0 | 166 | |
aoqi@0 | 167 | public Header(int methodPos, XMLBridge bridge, ValueGetter getter) { |
aoqi@0 | 168 | super(methodPos); |
aoqi@0 | 169 | this.bridge = bridge; |
aoqi@0 | 170 | this.getter = getter; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | |
aoqi@0 | 173 | public void fillIn(Object[] methodArgs, Object returnValue, Message msg) { |
aoqi@0 | 174 | Object value = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]); |
aoqi@0 | 175 | msg.getHeaders().add(Headers.create(bridge,value)); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | private static boolean isXMLMimeType(String mimeType){ |
aoqi@0 | 180 | return mimeType.equals("text/xml") || mimeType.equals("application/xml"); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | } |