001: /*
002: * $Id: MultipartDataContentHandler.java,v 1.2 2007/07/16 16:41:22 ofung Exp $
003: * $Revision: 1.2 $
004: * $Date: 2007/07/16 16:41:22 $
005: */
006:
007: /*
008: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
009: *
010: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
011: *
012: * The contents of this file are subject to the terms of either the GNU
013: * General Public License Version 2 only ("GPL") or the Common Development
014: * and Distribution License("CDDL") (collectively, the "License"). You
015: * may not use this file except in compliance with the License. You can obtain
016: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
017: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
018: * language governing permissions and limitations under the License.
019: *
020: * When distributing the software, include this License Header Notice in each
021: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
022: * Sun designates this particular file as subject to the "Classpath" exception
023: * as provided by Sun in the GPL Version 2 section of the License file that
024: * accompanied this code. If applicable, add the following below the License
025: * Header, with the fields enclosed by brackets [] replaced by your own
026: * identifying information: "Portions Copyrighted [year]
027: * [name of copyright owner]"
028: *
029: * Contributor(s):
030: *
031: * If you wish your version of this file to be governed by only the CDDL or
032: * only the GPL Version 2, indicate your decision by adding "[Contributor]
033: * elects to include this software in this distribution under the [CDDL or GPL
034: * Version 2] license." If you don't indicate a single choice of license, a
035: * recipient has the option to distribute your version of this file under
036: * either the CDDL, the GPL Version 2 or to extend the choice of license to
037: * its licensees as provided above. However, if you add GPL Version 2 code
038: * and therefore, elected the GPL Version 2 license, then the option applies
039: * only if the new code is made subject to such option by the copyright
040: * holder.
041: */
042: package com.sun.xml.messaging.saaj.soap;
043:
044: import java.io.*;
045: import java.awt.datatransfer.DataFlavor;
046: import javax.activation.*;
047: import com.sun.xml.messaging.saaj.packaging.mime.internet.MimeMultipart;
048: import com.sun.xml.messaging.saaj.packaging.mime.internet.ContentType;
049: import com.sun.xml.messaging.saaj.util.ByteOutputStream;
050:
051: public class MultipartDataContentHandler implements DataContentHandler {
052: private ActivationDataFlavor myDF = new ActivationDataFlavor(
053: com.sun.xml.messaging.saaj.packaging.mime.internet.MimeMultipart.class,
054: "multipart/mixed", "Multipart");
055:
056: /**
057: * Return the DataFlavors for this <code>DataContentHandler</code>.
058: *
059: * @return The DataFlavors
060: */
061: public DataFlavor[] getTransferDataFlavors() { // throws Exception;
062: return new DataFlavor[] { myDF };
063: }
064:
065: /**
066: * Return the Transfer Data of type DataFlavor from InputStream.
067: *
068: * @param df The DataFlavor
069: * @param ins The InputStream corresponding to the data
070: * @return String object
071: */
072: public Object getTransferData(DataFlavor df, DataSource ds) {
073: // use myDF.equals to be sure to get ActivationDataFlavor.equals,
074: // which properly ignores Content-Type parameters in comparison
075: if (myDF.equals(df))
076: return getContent(ds);
077: else
078: return null;
079: }
080:
081: /**
082: * Return the content.
083: */
084: public Object getContent(DataSource ds) {
085: try {
086: return new MimeMultipart(ds, new ContentType(ds
087: .getContentType()));
088: } catch (Exception e) {
089: return null;
090: }
091: }
092:
093: /**
094: * Write the object to the output stream, using the specific MIME type.
095: */
096: public void writeTo(Object obj, String mimeType, OutputStream os)
097: throws IOException {
098: if (obj instanceof MimeMultipart) {
099: try {
100: //TODO: temporarily allow only ByteOutputStream
101: // Need to add writeTo(OutputStream) on MimeMultipart
102: ByteOutputStream baos = null;
103: if (os instanceof ByteOutputStream) {
104: baos = (ByteOutputStream) os;
105: } else {
106: throw new IOException(
107: "Input Stream expected to be a com.sun.xml.messaging.saaj.util.ByteOutputStream, but found "
108: + os.getClass().getName());
109: }
110: ((MimeMultipart) obj).writeTo(baos);
111: } catch (Exception e) {
112: throw new IOException(e.toString());
113: }
114: }
115: }
116: }
|