1 /* 2 * Copyright (c) 1997, 2012, 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 public DataFlavor[] getTransferDataFlavors() { 71 return (DataFlavor[]) Arrays.copyOf(flavor, flavor.length); 72 } 73 74 /** 75 * Returns an object which represents the data to be transferred. 76 * The class of the object returned is defined by the representation class 77 * of the flavor. 78 * 79 * @param df The DataFlavor representing the requested type. 80 * @param ds The DataSource representing the data to be converted. 81 * @return The constructed Object. 82 */ 83 public Object getTransferData(DataFlavor df, DataSource ds) 84 throws IOException { 85 for (int i=0; i < flavor.length; i++) { 86 if (flavor[i].equals(df)) { 87 return getContent(ds); 88 } 89 } 90 return null; 91 } 92 93 /** 94 * Return an object representing the data in its most preferred form. 95 * Generally this will be the form described by the first DataFlavor 96 * returned by the <code>getTransferDataFlavors</code> method. 97 * 98 * @param ds The DataSource representing the data to be converted. 99 * @return The constructed Object. 100 */ 101 public Object getContent(DataSource ds) throws IOException { 102 return ImageIO.read(new BufferedInputStream(ds.getInputStream())); 103 } 104 105 /** 106 * Convert the object to a byte stream of the specified MIME type 107 * and write it to the output stream. 108 * 109 * @param obj The object to be converted. 110 * @param mimeType The requested MIME type of the resulting byte stream. 111 * @param os The output stream into which to write the converted 112 * byte stream. 113 */ 114 115 public void writeTo(Object obj, String type, OutputStream os) 116 throws IOException { 117 118 try { 119 BufferedImage bufImage = null; 120 if (obj instanceof BufferedImage) { 121 bufImage = (BufferedImage)obj; 122 } else if (obj instanceof Image) { 123 bufImage = render((Image)obj); 124 } else { 125 log.log(Level.SEVERE, 126 "SAAJ0520.soap.invalid.obj.type", 127 new String[] { obj.getClass().toString() }); 128 throw new IOException( 129 "ImageDataContentHandler requires Image object, " 130 + "was given object of type " 131 + obj.getClass().toString()); 132 } 133 ImageWriter writer = null; 134 Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(type); 135 if (i.hasNext()) { 136 writer = i.next(); 137 } 138 if (writer != null) { 139 ImageOutputStream stream = null; 140 stream = ImageIO.createImageOutputStream(os); 141 writer.setOutput(stream); 142 writer.write(bufImage); 143 writer.dispose(); 144 stream.close(); 145 } else { 146 log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type", 147 new String[] { type }); 148 throw new IOException("Unsupported mime type:"+ type); 149 } 150 } catch (Exception e) { 151 log.severe("SAAJ0525.soap.cannot.encode.img"); 152 throw new IOException("Unable to encode the image to a stream " 153 + e.getMessage()); 154 } 155 } 156 157 158 private BufferedImage render(Image img) throws InterruptedException { 159 160 MediaTracker tracker = new MediaTracker(this); 161 tracker.addImage(img, 0); 162 tracker.waitForAll(); 163 BufferedImage bufImage = new BufferedImage(img.getWidth(null), 164 img.getHeight(null), BufferedImage.TYPE_INT_RGB); 165 Graphics g = bufImage.createGraphics(); 166 g.drawImage(img, 0, 0, null); 167 g.dispose(); 168 return bufImage; 169 } 170 171 }