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: import java.util.Arrays; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: import javax.activation.*; aoqi@0: import javax.imageio.ImageIO; aoqi@0: import javax.imageio.ImageWriter; aoqi@0: import javax.imageio.stream.ImageOutputStream; aoqi@0: aoqi@0: import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; aoqi@0: aoqi@0: public class ImageDataContentHandler extends Component aoqi@0: implements DataContentHandler { aoqi@0: aoqi@0: protected static final Logger log = aoqi@0: Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, aoqi@0: "com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); aoqi@0: aoqi@0: private DataFlavor[] flavor; aoqi@0: aoqi@0: public ImageDataContentHandler() { aoqi@0: String[] mimeTypes = ImageIO.getReaderMIMETypes(); aoqi@0: flavor = new DataFlavor[mimeTypes.length]; aoqi@0: for(int i=0; i < mimeTypes.length; i++) { aoqi@0: flavor[i] = new ActivationDataFlavor( aoqi@0: java.awt.Image.class, mimeTypes[i], "Image"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns an array of DataFlavor objects indicating the flavors the aoqi@0: * data can be provided in. The array should be ordered according to aoqi@0: * preference for providing the data (from most richly descriptive to aoqi@0: * least descriptive). aoqi@0: * aoqi@0: * @return The DataFlavors. aoqi@0: */ aoqi@0: public DataFlavor[] getTransferDataFlavors() { aoqi@0: return (DataFlavor[]) Arrays.copyOf(flavor, flavor.length); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns an object which represents the data to be transferred. aoqi@0: * The class of the object returned is defined by the representation class aoqi@0: * of the flavor. aoqi@0: * aoqi@0: * @param df The DataFlavor representing the requested type. aoqi@0: * @param ds The DataSource representing the data to be converted. aoqi@0: * @return The constructed Object. aoqi@0: */ aoqi@0: public Object getTransferData(DataFlavor df, DataSource ds) aoqi@0: throws IOException { aoqi@0: for (int i=0; i < flavor.length; i++) { aoqi@0: if (flavor[i].equals(df)) { aoqi@0: return getContent(ds); aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return an object representing the data in its most preferred form. aoqi@0: * Generally this will be the form described by the first DataFlavor aoqi@0: * returned by the getTransferDataFlavors method. aoqi@0: * aoqi@0: * @param ds The DataSource representing the data to be converted. aoqi@0: * @return The constructed Object. aoqi@0: */ aoqi@0: public Object getContent(DataSource ds) throws IOException { aoqi@0: return ImageIO.read(new BufferedInputStream(ds.getInputStream())); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Convert the object to a byte stream of the specified MIME type aoqi@0: * and write it to the output stream. aoqi@0: * aoqi@0: * @param obj The object to be converted. aoqi@0: * @param mimeType The requested MIME type of the resulting byte stream. aoqi@0: * @param os The output stream into which to write the converted aoqi@0: * byte stream. aoqi@0: */ aoqi@0: aoqi@0: public void writeTo(Object obj, String type, OutputStream os) aoqi@0: throws IOException { aoqi@0: aoqi@0: try { aoqi@0: BufferedImage bufImage = null; aoqi@0: if (obj instanceof BufferedImage) { aoqi@0: bufImage = (BufferedImage)obj; aoqi@0: } else if (obj instanceof Image) { aoqi@0: bufImage = render((Image)obj); aoqi@0: } else { aoqi@0: log.log(Level.SEVERE, aoqi@0: "SAAJ0520.soap.invalid.obj.type", aoqi@0: new String[] { obj.getClass().toString() }); aoqi@0: throw new IOException( aoqi@0: "ImageDataContentHandler requires Image object, " aoqi@0: + "was given object of type " aoqi@0: + obj.getClass().toString()); aoqi@0: } aoqi@0: ImageWriter writer = null; aoqi@0: Iterator i = ImageIO.getImageWritersByMIMEType(type); aoqi@0: if (i.hasNext()) { aoqi@0: writer = (ImageWriter)i.next(); aoqi@0: } aoqi@0: if (writer != null) { aoqi@0: ImageOutputStream stream = null; aoqi@0: stream = ImageIO.createImageOutputStream(os); aoqi@0: writer.setOutput(stream); aoqi@0: writer.write(bufImage); aoqi@0: writer.dispose(); aoqi@0: stream.close(); aoqi@0: } else { aoqi@0: log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type", aoqi@0: new String[] { type }); aoqi@0: throw new IOException("Unsupported mime type:"+ type); aoqi@0: } aoqi@0: } catch (Exception e) { aoqi@0: log.severe("SAAJ0525.soap.cannot.encode.img"); aoqi@0: throw new IOException("Unable to encode the image to a stream " aoqi@0: + e.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: private BufferedImage render(Image img) throws InterruptedException { aoqi@0: aoqi@0: MediaTracker tracker = new MediaTracker(this); aoqi@0: tracker.addImage(img, 0); aoqi@0: tracker.waitForAll(); aoqi@0: BufferedImage bufImage = new BufferedImage(img.getWidth(null), aoqi@0: img.getHeight(null), BufferedImage.TYPE_INT_RGB); aoqi@0: Graphics g = bufImage.createGraphics(); aoqi@0: g.drawImage(img, 0, 0, null); aoqi@0: g.dispose(); aoqi@0: return bufImage; aoqi@0: } aoqi@0: aoqi@0: }