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 
  33 import javax.activation.*;
  34 
  35 //import com.sun.image.codec.jpeg.*;
  36 import javax.imageio.ImageIO;
  37 
  38 /**
  39  * JAF data handler for Jpeg content
  40  *
  41  * @author Ana Lindstrom-Tamer
  42  */
  43 
  44 public class JpegDataContentHandler
  45     extends Component
  46     implements DataContentHandler {
  47     public static final String STR_SRC = "java.awt.Image";
  48 
  49     /**
  50      * Return the DataFlavors for this <code>DataContentHandler</code>
  51      * @return The DataFlavors.
  52      */
  53     @Override
  54     public DataFlavor[] getTransferDataFlavors() { // throws Exception;
  55         DataFlavor flavors[] = new DataFlavor[1];
  56 
  57         try {
  58             flavors[0] =
  59                 new ActivationDataFlavor(
  60                     Class.forName(STR_SRC),
  61                     "image/jpeg",
  62                     "JPEG");
  63         } catch (Exception e) {
  64             System.out.println(e);
  65         }
  66 
  67         return flavors;
  68     }
  69 
  70     /**
  71      * Return the Transfer Data of type DataFlavor from InputStream
  72      * @param df The DataFlavor
  73      * @param ds The DataSource
  74      * @return The constructed Object.
  75      */
  76     @Override
  77     public Object getTransferData(DataFlavor df, DataSource ds) {
  78 
  79         // this is sort of hacky, but will work for the
  80         // sake of testing...
  81         if (df.getMimeType().startsWith("image/jpeg")) {
  82             if (df.getRepresentationClass().getName().equals(STR_SRC)) {
  83                 InputStream inputStream = null;
  84                 BufferedImage jpegLoadImage = null;
  85 
  86                 try {
  87                     inputStream = ds.getInputStream();
  88                     jpegLoadImage = ImageIO.read(inputStream);
  89 
  90                 } catch (Exception e) {
  91                     System.out.println(e);
  92                 }
  93 
  94                 return jpegLoadImage;
  95             }
  96         }
  97         return null;
  98     }
  99 
 100     /**
 101      *
 102      */
 103     @Override
 104     public Object getContent(DataSource ds) { // throws Exception;
 105         InputStream inputStream = null;
 106         BufferedImage jpegLoadImage = null;
 107 
 108         try {
 109             inputStream = ds.getInputStream();
 110             jpegLoadImage = ImageIO.read(inputStream);
 111 
 112         } catch (Exception e) {
 113         }
 114 
 115         return jpegLoadImage;
 116     }
 117 
 118     /**
 119      * Construct an object from a byte stream
 120      * (similar semantically to previous method, we are deciding
 121      *  which one to support)
 122      * @param obj object to write
 123      * @param mimeType requested MIME type of the resulting byte stream
 124      * @param os OutputStream
 125      */
 126     @Override
 127     public void writeTo(Object obj, String mimeType, OutputStream os)
 128         throws IOException {
 129         if (!mimeType.equals("image/jpeg"))
 130             throw new IOException(
 131                 "Invalid content type \""
 132                     + mimeType
 133                     + "\" for ImageContentHandler");
 134 
 135         if (obj == null) {
 136             throw new IOException("Null object for ImageContentHandler");
 137         }
 138 
 139         try {
 140             BufferedImage bufImage = null;
 141             if (obj instanceof BufferedImage) {
 142                 bufImage = (BufferedImage) obj;
 143 
 144             } else {
 145                 Image img = (Image) obj;
 146                 MediaTracker tracker = new MediaTracker(this);
 147                 tracker.addImage(img, 0);
 148                 tracker.waitForAll();
 149                 if (tracker.isErrorAny()) {
 150                         throw new IOException("Error while loading image");
 151                 }
 152                 bufImage =
 153                     new BufferedImage(
 154                         img.getWidth(null),
 155                         img.getHeight(null),
 156                         BufferedImage.TYPE_INT_RGB);
 157 
 158                 Graphics g = bufImage.createGraphics();
 159                 g.drawImage(img, 0, 0, null);
 160             }
 161             ImageIO.write(bufImage, "jpeg", os);
 162 
 163         } catch (Exception ex) {
 164             throw new IOException(
 165                 "Unable to run the JPEG Encoder on a stream "
 166                     + ex.getMessage());
 167         }
 168     }
 169 }