325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.client.sei;
325N/A
325N/Aimport com.sun.xml.internal.bind.api.Bridge;
325N/Aimport com.sun.xml.internal.ws.api.message.Attachment;
325N/Aimport com.sun.xml.internal.ws.api.message.Headers;
325N/Aimport com.sun.xml.internal.ws.api.message.Message;
325N/Aimport com.sun.xml.internal.ws.message.ByteArrayAttachment;
325N/Aimport com.sun.xml.internal.ws.message.DataHandlerAttachment;
325N/Aimport com.sun.xml.internal.ws.message.JAXBAttachment;
325N/Aimport com.sun.xml.internal.ws.model.ParameterImpl;
325N/Aimport java.io.UnsupportedEncodingException;
325N/Aimport java.net.URLEncoder;
325N/Aimport java.util.UUID;
325N/Aimport javax.activation.DataHandler;
325N/Aimport javax.xml.transform.Source;
325N/Aimport javax.xml.ws.WebServiceException;
325N/A
325N/A/**
325N/A * Puts a non-payload message parameter to {@link Message}.
325N/A *
325N/A * <p>
325N/A * Instance of this class is used to handle header parameters and attachment parameters.
325N/A * They add things to {@link Message}.
325N/A *
325N/A * @see BodyBuilder
325N/A * @author Kohsuke Kawaguchi
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Aabstract class MessageFiller {
325N/A
325N/A /**
325N/A * The index of the method invocation parameters that this object looks for.
325N/A */
325N/A protected final int methodPos;
325N/A
325N/A protected MessageFiller( int methodPos) {
325N/A this.methodPos = methodPos;
325N/A }
325N/A
325N/A /**
325N/A * Moves an argument of a method invocation into a {@link Message}.
325N/A */
325N/A abstract void fillIn(Object[] methodArgs, Message msg);
325N/A
325N/A /**
325N/A * Adds a parameter as an MIME attachment to {@link Message}.
325N/A */
325N/A static abstract class AttachmentFiller extends MessageFiller {
325N/A protected final ParameterImpl param;
325N/A protected final ValueGetter getter;
325N/A protected final String mimeType;
325N/A private final String contentIdPart;
325N/A
325N/A protected AttachmentFiller(ParameterImpl param, ValueGetter getter) {
325N/A super(param.getIndex());
325N/A this.param = param;
325N/A this.getter = getter;
325N/A mimeType = param.getBinding().getMimeType();
325N/A try {
325N/A contentIdPart = URLEncoder.encode(param.getPartName(), "UTF-8")+'=';
325N/A } catch (UnsupportedEncodingException e) {
325N/A throw new WebServiceException(e);
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Creates an MessageFiller based on the parameter type
325N/A *
325N/A * @param param
325N/A * runtime Parameter that abstracts the annotated java parameter
325N/A * @param getter
325N/A * Gets a value from an object that represents a parameter passed
325N/A * as a method argument.
325N/A */
325N/A public static MessageFiller createAttachmentFiller(ParameterImpl param, ValueGetter getter) {
325N/A Class type = (Class)param.getTypeReference().type;
325N/A if (DataHandler.class.isAssignableFrom(type) || Source.class.isAssignableFrom(type)) {
325N/A return new DataHandlerFiller(param, getter);
325N/A } else if (byte[].class==type) {
325N/A return new ByteArrayFiller(param, getter);
325N/A } else if(isXMLMimeType(param.getBinding().getMimeType())) {
325N/A return new JAXBFiller(param, getter);
325N/A } else {
325N/A return new DataHandlerFiller(param, getter);
325N/A }
325N/A }
325N/A
325N/A String getContentId() {
325N/A return contentIdPart+UUID.randomUUID()+"@jaxws.sun.com";
325N/A }
325N/A }
325N/A
325N/A private static class ByteArrayFiller extends AttachmentFiller {
325N/A protected ByteArrayFiller(ParameterImpl param, ValueGetter getter) {
325N/A super(param, getter);
325N/A }
325N/A void fillIn(Object[] methodArgs, Message msg) {
325N/A String contentId = getContentId();
325N/A Object obj = getter.get(methodArgs[methodPos]);
325N/A Attachment att = new ByteArrayAttachment(contentId,(byte[])obj,mimeType);
325N/A msg.getAttachments().add(att);
325N/A }
325N/A }
325N/A
325N/A private static class DataHandlerFiller extends AttachmentFiller {
325N/A protected DataHandlerFiller(ParameterImpl param, ValueGetter getter) {
325N/A super(param, getter);
325N/A }
325N/A void fillIn(Object[] methodArgs, Message msg) {
325N/A String contentId = getContentId();
325N/A Object obj = getter.get(methodArgs[methodPos]);
325N/A DataHandler dh = (obj instanceof DataHandler) ? (DataHandler)obj : new DataHandler(obj,mimeType);
325N/A Attachment att = new DataHandlerAttachment(contentId, dh);
325N/A msg.getAttachments().add(att);
325N/A }
325N/A }
325N/A
325N/A private static class JAXBFiller extends AttachmentFiller {
325N/A protected JAXBFiller(ParameterImpl param, ValueGetter getter) {
325N/A super(param, getter);
325N/A }
325N/A void fillIn(Object[] methodArgs, Message msg) {
325N/A String contentId = getContentId();
325N/A Object obj = getter.get(methodArgs[methodPos]);
325N/A Attachment att = new JAXBAttachment(contentId, obj, param.getBridge(), mimeType);
325N/A msg.getAttachments().add(att);
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Adds a parameter as an header.
325N/A */
325N/A static final class Header extends MessageFiller {
325N/A private final Bridge bridge;
325N/A private final ValueGetter getter;
325N/A
325N/A protected Header(int methodPos, Bridge bridge, ValueGetter getter) {
325N/A super(methodPos);
325N/A this.bridge = bridge;
325N/A this.getter = getter;
325N/A }
325N/A
325N/A void fillIn(Object[] methodArgs, Message msg) {
325N/A Object value = getter.get(methodArgs[methodPos]);
325N/A msg.getHeaders().add(Headers.create(bridge,value));
325N/A }
325N/A }
325N/A
325N/A private static boolean isXMLMimeType(String mimeType){
325N/A return (mimeType.equals("text/xml") || mimeType.equals("application/xml"));
325N/A }
325N/A
325N/A}