1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.soap;
  27 
  28 import java.io.*;
  29 import java.awt.datatransfer.DataFlavor;
  30 import javax.activation.*;
  31 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart;
  32 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentType;
  33 import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
  34 
  35 public class MultipartDataContentHandler implements DataContentHandler {
  36     private ActivationDataFlavor myDF = new ActivationDataFlavor(
  37             com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart.class,
  38             "multipart/mixed",
  39             "Multipart");
  40 
  41     /**
  42      * Return the DataFlavors for this <code>DataContentHandler</code>.
  43      *
  44      * @return The DataFlavors
  45      */
  46     public DataFlavor[] getTransferDataFlavors() { // throws Exception;
  47         return new DataFlavor[] { myDF };
  48     }
  49 
  50     /**
  51      * Return the Transfer Data of type DataFlavor from InputStream.
  52      *
  53      * @param df The DataFlavor
  54      * @param ins The InputStream corresponding to the data
  55      * @return String object
  56      */
  57     public Object getTransferData(DataFlavor df, DataSource ds) {
  58         // use myDF.equals to be sure to get ActivationDataFlavor.equals,
  59         // which properly ignores Content-Type parameters in comparison
  60         if (myDF.equals(df))
  61             return getContent(ds);
  62         else
  63             return null;
  64     }
  65 
  66     /**
  67      * Return the content.
  68      */
  69     public Object getContent(DataSource ds) {
  70         try {
  71             return new MimeMultipart(
  72                 ds, new ContentType(ds.getContentType()));
  73         } catch (Exception e) {
  74             return null;
  75         }
  76     }
  77 
  78     /**
  79      * Write the object to the output stream, using the specific MIME type.
  80      */
  81     public void writeTo(Object obj, String mimeType, OutputStream os)
  82                         throws IOException {
  83         if (obj instanceof MimeMultipart) {
  84             try {
  85                 //TODO: temporarily allow only ByteOutputStream
  86                 // Need to add writeTo(OutputStream) on MimeMultipart
  87                 ByteOutputStream baos = null;
  88                 if (os instanceof ByteOutputStream) {
  89                     baos = (ByteOutputStream)os;
  90                 } else {
  91                     throw new IOException("Input Stream expected to be a com.sun.xml.internal.messaging.saaj.util.ByteOutputStream, but found " +
  92                         os.getClass().getName());
  93                 }
  94                 ((MimeMultipart)obj).writeTo(baos);
  95             } catch (Exception e) {
  96                 throw new IOException(e.toString());
  97             }
  98         }
  99     }
 100 }