1 /*
   2  * Copyright (c) 1997, 2017, 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.awt.*;
  29 import java.awt.datatransfer.DataFlavor;
  30 import java.awt.image.BufferedImage;
  31 import java.io.*;
  32 import java.util.Arrays;
  33 import java.util.Iterator;
  34 import java.util.logging.Level;
  35 import java.util.logging.Logger;
  36 
  37 import javax.activation.*;
  38 import javax.imageio.ImageIO;
  39 import javax.imageio.ImageWriter;
  40 import javax.imageio.stream.ImageOutputStream;
  41 
  42 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
  43 
  44 public class ImageDataContentHandler extends Component
  45     implements DataContentHandler {
  46 
  47     protected static final Logger log =
  48         Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
  49                          "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
  50 
  51     private DataFlavor[] flavor;
  52 
  53     public ImageDataContentHandler() {
  54         String[] mimeTypes = ImageIO.getReaderMIMETypes();
  55         flavor = new DataFlavor[mimeTypes.length];
  56         for(int i=0; i < mimeTypes.length; i++) {
  57             flavor[i] = new ActivationDataFlavor(
  58                 java.awt.Image.class, mimeTypes[i], "Image");
  59         }
  60     }
  61 
  62     /**
  63      * Returns an array of DataFlavor objects indicating the flavors the
  64      * data can be provided in. The array should be ordered according to
  65      * preference for providing the data (from most richly descriptive to
  66      * least descriptive).
  67      *
  68      * @return The DataFlavors.
  69      */
  70     @Override
  71     public DataFlavor[] getTransferDataFlavors() {
  72         return Arrays.copyOf(flavor, flavor.length);
  73     }
  74 
  75     /**
  76      * Returns an object which represents the data to be transferred.
  77      * The class of the object returned is defined by the representation class
  78      * of the flavor.
  79      *
  80      * @param df The DataFlavor representing the requested type.
  81      * @param ds The DataSource representing the data to be converted.
  82      * @return The constructed Object.
  83      */
  84     @Override
  85     public Object getTransferData(DataFlavor df, DataSource ds)
  86         throws IOException {
  87         for (int i=0; i < flavor.length; i++) {
  88             if (flavor[i].equals(df)) {
  89                 return getContent(ds);
  90             }
  91         }
  92         return null;
  93     }
  94 
  95     /**
  96      * Return an object representing the data in its most preferred form.
  97      * Generally this will be the form described by the first DataFlavor
  98      * returned by the <code>getTransferDataFlavors</code> method.
  99      *
 100      * @param ds The DataSource representing the data to be converted.
 101      * @return The constructed Object.
 102      */
 103     @Override
 104     public Object getContent(DataSource ds) throws IOException {
 105         return ImageIO.read(new BufferedInputStream(ds.getInputStream()));
 106     }
 107 
 108     /**
 109      * Convert the object to a byte stream of the specified MIME type
 110      * and write it to the output stream.
 111      *
 112      * @param obj   The object to be converted.
 113      * @param type  The requested MIME type of the resulting byte stream.
 114      * @param os    The output stream into which to write the converted
 115      *          byte stream.
 116      */
 117     @Override
 118     public void writeTo(Object obj, String type, OutputStream os)
 119         throws IOException {
 120 
 121         try {
 122             BufferedImage bufImage = null;
 123             if (obj instanceof BufferedImage) {
 124                 bufImage = (BufferedImage)obj;
 125             } else if (obj instanceof Image) {
 126                 bufImage = render((Image)obj);
 127             } else {
 128                 log.log(Level.SEVERE,
 129                     "SAAJ0520.soap.invalid.obj.type",
 130                     new String[] { obj.getClass().toString() });
 131                 throw new IOException(
 132                     "ImageDataContentHandler requires Image object, "
 133                     + "was given object of type "
 134                     + obj.getClass().toString());
 135             }
 136             ImageWriter writer = null;
 137             Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(type);
 138             if (i.hasNext()) {
 139                 writer = i.next();
 140             }
 141             if (writer != null) {
 142                 ImageOutputStream stream = null;
 143                 stream = ImageIO.createImageOutputStream(os);
 144                 writer.setOutput(stream);
 145                 writer.write(bufImage);
 146                 writer.dispose();
 147                 stream.close();
 148             } else {
 149                 log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type",
 150                     new String[] { type });
 151                 throw new IOException("Unsupported mime type:"+ type);
 152             }
 153         } catch (Exception e) {
 154             log.severe("SAAJ0525.soap.cannot.encode.img");
 155             throw new IOException("Unable to encode the image to a stream "
 156                 + e.getMessage());
 157         }
 158     }
 159 
 160 
 161     private BufferedImage render(Image img) throws InterruptedException {
 162 
 163         MediaTracker tracker = new MediaTracker(this);
 164         tracker.addImage(img, 0);
 165         tracker.waitForAll();
 166         BufferedImage bufImage = new BufferedImage(img.getWidth(null),
 167             img.getHeight(null), BufferedImage.TYPE_INT_RGB);
 168         Graphics g = bufImage.createGraphics();
 169         g.drawImage(img, 0, 0, null);
 170         g.dispose();
 171         return bufImage;
 172     }
 173 
 174 }