001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.client.sei;
038:
039: import com.sun.xml.bind.api.Bridge;
040: import com.sun.xml.ws.api.message.Attachment;
041: import com.sun.xml.ws.api.message.Headers;
042: import com.sun.xml.ws.api.message.Message;
043: import com.sun.xml.ws.message.ByteArrayAttachment;
044: import com.sun.xml.ws.message.DataHandlerAttachment;
045: import com.sun.xml.ws.message.JAXBAttachment;
046: import com.sun.xml.ws.model.ParameterImpl;
047: import java.io.UnsupportedEncodingException;
048: import java.net.URLEncoder;
049: import java.util.UUID;
050: import javax.activation.DataHandler;
051: import javax.xml.transform.Source;
052: import javax.xml.ws.WebServiceException;
053:
054: /**
055: * Puts a non-payload message parameter to {@link Message}.
056: *
057: * <p>
058: * Instance of this class is used to handle header parameters and attachment parameters.
059: * They add things to {@link Message}.
060: *
061: * @see BodyBuilder
062: * @author Kohsuke Kawaguchi
063: * @author Jitendra Kotamraju
064: */
065: abstract class MessageFiller {
066:
067: /**
068: * The index of the method invocation parameters that this object looks for.
069: */
070: protected final int methodPos;
071:
072: protected MessageFiller(int methodPos) {
073: this .methodPos = methodPos;
074: }
075:
076: /**
077: * Moves an argument of a method invocation into a {@link Message}.
078: */
079: abstract void fillIn(Object[] methodArgs, Message msg);
080:
081: /**
082: * Adds a parameter as an MIME attachment to {@link Message}.
083: */
084: static abstract class AttachmentFiller extends MessageFiller {
085: protected final ParameterImpl param;
086: protected final ValueGetter getter;
087: protected final String mimeType;
088: private final String contentIdPart;
089:
090: protected AttachmentFiller(ParameterImpl param,
091: ValueGetter getter) {
092: super (param.getIndex());
093: this .param = param;
094: this .getter = getter;
095: mimeType = param.getBinding().getMimeType();
096: try {
097: contentIdPart = URLEncoder.encode(param.getPartName(),
098: "UTF-8") + '=';
099: } catch (UnsupportedEncodingException e) {
100: throw new WebServiceException(e);
101: }
102: }
103:
104: /**
105: * Creates an MessageFiller based on the parameter type
106: *
107: * @param param
108: * runtime Parameter that abstracts the annotated java parameter
109: * @param getter
110: * Gets a value from an object that represents a parameter passed
111: * as a method argument.
112: */
113: public static MessageFiller createAttachmentFiller(
114: ParameterImpl param, ValueGetter getter) {
115: Class type = (Class) param.getTypeReference().type;
116: if (DataHandler.class.isAssignableFrom(type)
117: || Source.class.isAssignableFrom(type)) {
118: return new DataHandlerFiller(param, getter);
119: } else if (byte[].class == type) {
120: return new ByteArrayFiller(param, getter);
121: } else if (isXMLMimeType(param.getBinding().getMimeType())) {
122: return new JAXBFiller(param, getter);
123: } else {
124: return new DataHandlerFiller(param, getter);
125: }
126: }
127:
128: String getContentId() {
129: return contentIdPart + UUID.randomUUID() + "@jaxws.sun.com";
130: }
131: }
132:
133: private static class ByteArrayFiller extends AttachmentFiller {
134: protected ByteArrayFiller(ParameterImpl param,
135: ValueGetter getter) {
136: super (param, getter);
137: }
138:
139: void fillIn(Object[] methodArgs, Message msg) {
140: String contentId = getContentId();
141: Object obj = getter.get(methodArgs[methodPos]);
142: Attachment att = new ByteArrayAttachment(contentId,
143: (byte[]) obj, mimeType);
144: msg.getAttachments().add(att);
145: }
146: }
147:
148: private static class DataHandlerFiller extends AttachmentFiller {
149: protected DataHandlerFiller(ParameterImpl param,
150: ValueGetter getter) {
151: super (param, getter);
152: }
153:
154: void fillIn(Object[] methodArgs, Message msg) {
155: String contentId = getContentId();
156: Object obj = getter.get(methodArgs[methodPos]);
157: DataHandler dh = (obj instanceof DataHandler) ? (DataHandler) obj
158: : new DataHandler(obj, mimeType);
159: Attachment att = new DataHandlerAttachment(contentId, dh);
160: msg.getAttachments().add(att);
161: }
162: }
163:
164: private static class JAXBFiller extends AttachmentFiller {
165: protected JAXBFiller(ParameterImpl param, ValueGetter getter) {
166: super (param, getter);
167: }
168:
169: void fillIn(Object[] methodArgs, Message msg) {
170: String contentId = getContentId();
171: Object obj = getter.get(methodArgs[methodPos]);
172: Attachment att = new JAXBAttachment(contentId, obj, param
173: .getBridge(), mimeType);
174: msg.getAttachments().add(att);
175: }
176: }
177:
178: /**
179: * Adds a parameter as an header.
180: */
181: static final class Header extends MessageFiller {
182: private final Bridge bridge;
183: private final ValueGetter getter;
184:
185: protected Header(int methodPos, Bridge bridge,
186: ValueGetter getter) {
187: super (methodPos);
188: this .bridge = bridge;
189: this .getter = getter;
190: }
191:
192: void fillIn(Object[] methodArgs, Message msg) {
193: Object value = getter.get(methodArgs[methodPos]);
194: msg.getHeaders().add(Headers.create(bridge, value));
195: }
196: }
197:
198: private static boolean isXMLMimeType(String mimeType) {
199: return (mimeType.equals("text/xml") || mimeType
200: .equals("application/xml"));
201: }
202:
203: }
|