ohair@286: /* ohair@286: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.server.sei; ohair@286: ohair@286: import com.sun.xml.internal.ws.api.message.Headers; ohair@286: import com.sun.xml.internal.ws.api.message.Message; ohair@286: import com.sun.xml.internal.ws.message.ByteArrayAttachment; ohair@286: import com.sun.xml.internal.ws.message.DataHandlerAttachment; ohair@286: import com.sun.xml.internal.ws.message.JAXBAttachment; ohair@286: import com.sun.xml.internal.ws.model.ParameterImpl; ohair@286: import com.sun.xml.internal.ws.spi.db.XMLBridge; ohair@286: ohair@286: import java.io.UnsupportedEncodingException; ohair@286: import java.net.URLEncoder; ohair@286: import java.util.UUID; ohair@286: import javax.activation.DataHandler; ohair@286: import javax.xml.transform.Source; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: import com.sun.xml.internal.ws.api.message.Attachment; ohair@286: ohair@286: /** ohair@286: * Puts a non-payload message parameter to {@link Message}. ohair@286: * ohair@286: *

ohair@286: * Instance of this class is used to handle header parameters and attachment parameters. ohair@286: * They add things to {@link Message}. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: public abstract class MessageFiller { ohair@286: ohair@286: /** ohair@286: * The index of the method invocation parameters that this object looks for. ohair@286: */ ohair@286: protected final int methodPos; ohair@286: ohair@286: protected MessageFiller( int methodPos) { ohair@286: this.methodPos = methodPos; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Moves an argument of a method invocation into a {@link Message}. ohair@286: */ ohair@286: public abstract void fillIn(Object[] methodArgs, Object returnValue, Message msg); ohair@286: ohair@286: /** ohair@286: * Adds a parameter as an MIME attachment to {@link Message}. ohair@286: */ ohair@286: public static abstract class AttachmentFiller extends MessageFiller { ohair@286: protected final ParameterImpl param; ohair@286: protected final ValueGetter getter; ohair@286: protected final String mimeType; ohair@286: private final String contentIdPart; ohair@286: ohair@286: protected AttachmentFiller(ParameterImpl param, ValueGetter getter) { ohair@286: super(param.getIndex()); ohair@286: this.param = param; ohair@286: this.getter = getter; ohair@286: mimeType = param.getBinding().getMimeType(); ohair@286: try { ohair@286: contentIdPart = URLEncoder.encode(param.getPartName(), "UTF-8")+'='; ohair@286: } catch (UnsupportedEncodingException e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates an MessageFiller based on the parameter type ohair@286: * ohair@286: * @param param ohair@286: * runtime Parameter that abstracts the annotated java parameter ohair@286: * @param getter ohair@286: * Gets a value from an object that represents a parameter passed ohair@286: * as a method argument. ohair@286: */ ohair@286: public static MessageFiller createAttachmentFiller(ParameterImpl param, ValueGetter getter) { ohair@286: Class type = (Class)param.getTypeInfo().type; ohair@286: if (DataHandler.class.isAssignableFrom(type) || Source.class.isAssignableFrom(type)) { ohair@286: return new DataHandlerFiller(param, getter); ohair@286: } else if (byte[].class==type) { ohair@286: return new ByteArrayFiller(param, getter); ohair@286: } else if(isXMLMimeType(param.getBinding().getMimeType())) { ohair@286: return new JAXBFiller(param, getter); ohair@286: } else { ohair@286: return new DataHandlerFiller(param, getter); ohair@286: } ohair@286: } ohair@286: ohair@286: String getContentId() { ohair@286: return contentIdPart+UUID.randomUUID()+"@jaxws.sun.com"; ohair@286: } ohair@286: } ohair@286: ohair@286: private static class ByteArrayFiller extends AttachmentFiller { ohair@286: protected ByteArrayFiller(ParameterImpl param, ValueGetter getter) { ohair@286: super(param, getter); ohair@286: } ohair@286: ohair@286: public void fillIn(Object[] methodArgs, Object returnValue, Message msg) { ohair@286: String contentId = getContentId(); ohair@286: Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]); ohair@286: if (obj != null) { ohair@286: Attachment att = new ByteArrayAttachment(contentId,(byte[])obj,mimeType); ohair@286: msg.getAttachments().add(att); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: private static class DataHandlerFiller extends AttachmentFiller { ohair@286: protected DataHandlerFiller(ParameterImpl param, ValueGetter getter) { ohair@286: super(param, getter); ohair@286: } ohair@286: ohair@286: public void fillIn(Object[] methodArgs, Object returnValue, Message msg) { ohair@286: String contentId = getContentId(); ohair@286: Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]); ohair@286: DataHandler dh = (obj instanceof DataHandler) ? (DataHandler)obj : new DataHandler(obj,mimeType); ohair@286: Attachment att = new DataHandlerAttachment(contentId, dh); ohair@286: msg.getAttachments().add(att); ohair@286: } ohair@286: } ohair@286: ohair@286: private static class JAXBFiller extends AttachmentFiller { ohair@286: protected JAXBFiller(ParameterImpl param, ValueGetter getter) { ohair@286: super(param, getter); ohair@286: } ohair@286: ohair@286: public void fillIn(Object[] methodArgs, Object returnValue, Message msg) { ohair@286: String contentId = getContentId(); ohair@286: Object obj = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]); ohair@286: Attachment att = new JAXBAttachment(contentId, obj, param.getXMLBridge(), mimeType); ohair@286: msg.getAttachments().add(att); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Adds a parameter as an header. ohair@286: */ ohair@286: public static final class Header extends MessageFiller { ohair@286: private final XMLBridge bridge; ohair@286: private final ValueGetter getter; ohair@286: ohair@286: public Header(int methodPos, XMLBridge bridge, ValueGetter getter) { ohair@286: super(methodPos); ohair@286: this.bridge = bridge; ohair@286: this.getter = getter; ohair@286: } ohair@286: ohair@286: public void fillIn(Object[] methodArgs, Object returnValue, Message msg) { ohair@286: Object value = (methodPos == -1) ? returnValue : getter.get(methodArgs[methodPos]); ohair@286: msg.getHeaders().add(Headers.create(bridge,value)); ohair@286: } ohair@286: } ohair@286: ohair@286: private static boolean isXMLMimeType(String mimeType){ ohair@286: return mimeType.equals("text/xml") || mimeType.equals("application/xml"); ohair@286: } ohair@286: }