aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.messaging.saaj.soap; aoqi@0: aoqi@0: import java.awt.*; aoqi@0: import java.awt.datatransfer.DataFlavor; aoqi@0: import java.awt.image.BufferedImage; aoqi@0: import java.io.*; aoqi@0: aoqi@0: import javax.activation.*; aoqi@0: aoqi@0: //import com.sun.image.codec.jpeg.*; aoqi@0: import javax.imageio.ImageIO; aoqi@0: aoqi@0: /** aoqi@0: * JAF data handler for Jpeg content aoqi@0: * aoqi@0: * @author Ana Lindstrom-Tamer aoqi@0: */ aoqi@0: aoqi@0: public class JpegDataContentHandler aoqi@0: extends Component aoqi@0: implements DataContentHandler { aoqi@0: public static final String STR_SRC = "java.awt.Image"; aoqi@0: aoqi@0: /** aoqi@0: * return the DataFlavors for this DataContentHandler aoqi@0: * @return The DataFlavors. aoqi@0: */ aoqi@0: public DataFlavor[] getTransferDataFlavors() { // throws Exception; aoqi@0: DataFlavor flavors[] = new DataFlavor[1]; aoqi@0: aoqi@0: try { aoqi@0: flavors[0] = aoqi@0: new ActivationDataFlavor( aoqi@0: Class.forName(STR_SRC), aoqi@0: "image/jpeg", aoqi@0: "JPEG"); aoqi@0: } catch (Exception e) { aoqi@0: System.out.println(e); aoqi@0: } aoqi@0: aoqi@0: return flavors; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * return the Transfer Data of type DataFlavor from InputStream aoqi@0: * @param df The DataFlavor. aoqi@0: * @param ins The InputStream corresponding to the data. aoqi@0: * @return The constructed Object. aoqi@0: */ aoqi@0: public Object getTransferData(DataFlavor df, DataSource ds) { aoqi@0: aoqi@0: // this is sort of hacky, but will work for the aoqi@0: // sake of testing... aoqi@0: if (df.getMimeType().startsWith("image/jpeg")) { aoqi@0: if (df.getRepresentationClass().getName().equals(STR_SRC)) { aoqi@0: InputStream inputStream = null; aoqi@0: BufferedImage jpegLoadImage = null; aoqi@0: aoqi@0: try { aoqi@0: inputStream = ds.getInputStream(); aoqi@0: jpegLoadImage = ImageIO.read(inputStream); aoqi@0: aoqi@0: } catch (Exception e) { aoqi@0: System.out.println(e); aoqi@0: } aoqi@0: aoqi@0: return jpegLoadImage; aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * aoqi@0: */ aoqi@0: public Object getContent(DataSource ds) { // throws Exception; aoqi@0: InputStream inputStream = null; aoqi@0: BufferedImage jpegLoadImage = null; aoqi@0: aoqi@0: try { aoqi@0: inputStream = ds.getInputStream(); aoqi@0: jpegLoadImage = ImageIO.read(inputStream); aoqi@0: aoqi@0: } catch (Exception e) { aoqi@0: } aoqi@0: aoqi@0: return (Image) jpegLoadImage; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * construct an object from a byte stream aoqi@0: * (similar semantically to previous method, we are deciding aoqi@0: * which one to support) aoqi@0: */ aoqi@0: public void writeTo(Object obj, String mimeType, OutputStream os) aoqi@0: throws IOException { aoqi@0: if (!mimeType.equals("image/jpeg")) aoqi@0: throw new IOException( aoqi@0: "Invalid content type \"" aoqi@0: + mimeType aoqi@0: + "\" for ImageContentHandler"); aoqi@0: aoqi@0: if (obj == null) { aoqi@0: throw new IOException("Null object for ImageContentHandler"); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: BufferedImage bufImage = null; aoqi@0: if (obj instanceof BufferedImage) { aoqi@0: bufImage = (BufferedImage) obj; aoqi@0: aoqi@0: } else { aoqi@0: Image img = (Image) obj; aoqi@0: MediaTracker tracker = new MediaTracker(this); aoqi@0: tracker.addImage(img, 0); aoqi@0: tracker.waitForAll(); aoqi@0: if (tracker.isErrorAny()) { aoqi@0: throw new IOException("Error while loading image"); aoqi@0: } aoqi@0: bufImage = aoqi@0: new BufferedImage( aoqi@0: img.getWidth(null), aoqi@0: img.getHeight(null), aoqi@0: BufferedImage.TYPE_INT_RGB); aoqi@0: aoqi@0: Graphics g = bufImage.createGraphics(); aoqi@0: g.drawImage(img, 0, 0, null); aoqi@0: } aoqi@0: ImageIO.write(bufImage, "jpeg", os); aoqi@0: aoqi@0: } catch (Exception ex) { aoqi@0: throw new IOException( aoqi@0: "Unable to run the JPEG Encoder on a stream " aoqi@0: + ex.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: }