ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.messaging.saaj.soap; ohair@286: ohair@286: import java.awt.*; ohair@286: import java.awt.datatransfer.DataFlavor; ohair@286: import java.awt.image.BufferedImage; ohair@286: import java.io.*; ohair@286: import java.util.Arrays; ohair@286: import java.util.Iterator; ohair@286: import java.util.logging.Level; ohair@286: import java.util.logging.Logger; ohair@286: ohair@286: import javax.activation.*; ohair@286: import javax.imageio.ImageIO; ohair@286: import javax.imageio.ImageWriter; ohair@286: import javax.imageio.stream.ImageOutputStream; ohair@286: ohair@286: import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; ohair@286: ohair@286: public class ImageDataContentHandler extends Component ohair@286: implements DataContentHandler { ohair@286: ohair@286: protected static final Logger log = ohair@286: Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, ohair@286: "com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); ohair@286: ohair@286: private DataFlavor[] flavor; ohair@286: ohair@286: public ImageDataContentHandler() { ohair@286: String[] mimeTypes = ImageIO.getReaderMIMETypes(); ohair@286: flavor = new DataFlavor[mimeTypes.length]; ohair@286: for(int i=0; i < mimeTypes.length; i++) { ohair@286: flavor[i] = new ActivationDataFlavor( ohair@286: java.awt.Image.class, mimeTypes[i], "Image"); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns an array of DataFlavor objects indicating the flavors the ohair@286: * data can be provided in. The array should be ordered according to ohair@286: * preference for providing the data (from most richly descriptive to ohair@286: * least descriptive). ohair@286: * ohair@286: * @return The DataFlavors. ohair@286: */ ohair@286: public DataFlavor[] getTransferDataFlavors() { ohair@286: return (DataFlavor[]) Arrays.copyOf(flavor, flavor.length); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Returns an object which represents the data to be transferred. ohair@286: * The class of the object returned is defined by the representation class ohair@286: * of the flavor. ohair@286: * ohair@286: * @param df The DataFlavor representing the requested type. ohair@286: * @param ds The DataSource representing the data to be converted. ohair@286: * @return The constructed Object. ohair@286: */ ohair@286: public Object getTransferData(DataFlavor df, DataSource ds) ohair@286: throws IOException { ohair@286: for (int i=0; i < flavor.length; i++) { ohair@286: if (flavor[i].equals(df)) { ohair@286: return getContent(ds); ohair@286: } ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return an object representing the data in its most preferred form. ohair@286: * Generally this will be the form described by the first DataFlavor ohair@286: * returned by the getTransferDataFlavors method. ohair@286: * ohair@286: * @param ds The DataSource representing the data to be converted. ohair@286: * @return The constructed Object. ohair@286: */ ohair@286: public Object getContent(DataSource ds) throws IOException { ohair@286: return ImageIO.read(new BufferedInputStream(ds.getInputStream())); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Convert the object to a byte stream of the specified MIME type ohair@286: * and write it to the output stream. ohair@286: * ohair@286: * @param obj The object to be converted. ohair@286: * @param mimeType The requested MIME type of the resulting byte stream. ohair@286: * @param os The output stream into which to write the converted ohair@286: * byte stream. ohair@286: */ ohair@286: ohair@286: public void writeTo(Object obj, String type, OutputStream os) ohair@286: throws IOException { ohair@286: ohair@286: try { ohair@286: BufferedImage bufImage = null; ohair@286: if (obj instanceof BufferedImage) { ohair@286: bufImage = (BufferedImage)obj; ohair@286: } else if (obj instanceof Image) { ohair@286: bufImage = render((Image)obj); ohair@286: } else { ohair@286: log.log(Level.SEVERE, ohair@286: "SAAJ0520.soap.invalid.obj.type", ohair@286: new String[] { obj.getClass().toString() }); ohair@286: throw new IOException( ohair@286: "ImageDataContentHandler requires Image object, " ohair@286: + "was given object of type " ohair@286: + obj.getClass().toString()); ohair@286: } ohair@286: ImageWriter writer = null; ohair@286: Iterator i = ImageIO.getImageWritersByMIMEType(type); ohair@286: if (i.hasNext()) { ohair@286: writer = (ImageWriter)i.next(); ohair@286: } ohair@286: if (writer != null) { ohair@286: ImageOutputStream stream = null; ohair@286: stream = ImageIO.createImageOutputStream(os); ohair@286: writer.setOutput(stream); ohair@286: writer.write(bufImage); ohair@286: writer.dispose(); ohair@286: stream.close(); ohair@286: } else { ohair@286: log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type", ohair@286: new String[] { type }); ohair@286: throw new IOException("Unsupported mime type:"+ type); ohair@286: } ohair@286: } catch (Exception e) { ohair@286: log.severe("SAAJ0525.soap.cannot.encode.img"); ohair@286: throw new IOException("Unable to encode the image to a stream " ohair@286: + e.getMessage()); ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: private BufferedImage render(Image img) throws InterruptedException { ohair@286: ohair@286: MediaTracker tracker = new MediaTracker(this); ohair@286: tracker.addImage(img, 0); ohair@286: tracker.waitForAll(); ohair@286: BufferedImage bufImage = new BufferedImage(img.getWidth(null), ohair@286: img.getHeight(null), BufferedImage.TYPE_INT_RGB); ohair@286: Graphics g = bufImage.createGraphics(); ohair@286: g.drawImage(img, 0, 0, null); ohair@286: g.dispose(); ohair@286: return bufImage; ohair@286: } ohair@286: ohair@286: }