1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/ImageDataContentHandler.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,171 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.messaging.saaj.soap; 1.30 + 1.31 +import java.awt.*; 1.32 +import java.awt.datatransfer.DataFlavor; 1.33 +import java.awt.image.BufferedImage; 1.34 +import java.io.*; 1.35 +import java.util.Arrays; 1.36 +import java.util.Iterator; 1.37 +import java.util.logging.Level; 1.38 +import java.util.logging.Logger; 1.39 + 1.40 +import javax.activation.*; 1.41 +import javax.imageio.ImageIO; 1.42 +import javax.imageio.ImageWriter; 1.43 +import javax.imageio.stream.ImageOutputStream; 1.44 + 1.45 +import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; 1.46 + 1.47 +public class ImageDataContentHandler extends Component 1.48 + implements DataContentHandler { 1.49 + 1.50 + protected static final Logger log = 1.51 + Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, 1.52 + "com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); 1.53 + 1.54 + private DataFlavor[] flavor; 1.55 + 1.56 + public ImageDataContentHandler() { 1.57 + String[] mimeTypes = ImageIO.getReaderMIMETypes(); 1.58 + flavor = new DataFlavor[mimeTypes.length]; 1.59 + for(int i=0; i < mimeTypes.length; i++) { 1.60 + flavor[i] = new ActivationDataFlavor( 1.61 + java.awt.Image.class, mimeTypes[i], "Image"); 1.62 + } 1.63 + } 1.64 + 1.65 + /** 1.66 + * Returns an array of DataFlavor objects indicating the flavors the 1.67 + * data can be provided in. The array should be ordered according to 1.68 + * preference for providing the data (from most richly descriptive to 1.69 + * least descriptive). 1.70 + * 1.71 + * @return The DataFlavors. 1.72 + */ 1.73 + public DataFlavor[] getTransferDataFlavors() { 1.74 + return (DataFlavor[]) Arrays.copyOf(flavor, flavor.length); 1.75 + } 1.76 + 1.77 + /** 1.78 + * Returns an object which represents the data to be transferred. 1.79 + * The class of the object returned is defined by the representation class 1.80 + * of the flavor. 1.81 + * 1.82 + * @param df The DataFlavor representing the requested type. 1.83 + * @param ds The DataSource representing the data to be converted. 1.84 + * @return The constructed Object. 1.85 + */ 1.86 + public Object getTransferData(DataFlavor df, DataSource ds) 1.87 + throws IOException { 1.88 + for (int i=0; i < flavor.length; i++) { 1.89 + if (flavor[i].equals(df)) { 1.90 + return getContent(ds); 1.91 + } 1.92 + } 1.93 + return null; 1.94 + } 1.95 + 1.96 + /** 1.97 + * Return an object representing the data in its most preferred form. 1.98 + * Generally this will be the form described by the first DataFlavor 1.99 + * returned by the <code>getTransferDataFlavors</code> method. 1.100 + * 1.101 + * @param ds The DataSource representing the data to be converted. 1.102 + * @return The constructed Object. 1.103 + */ 1.104 + public Object getContent(DataSource ds) throws IOException { 1.105 + return ImageIO.read(new BufferedInputStream(ds.getInputStream())); 1.106 + } 1.107 + 1.108 + /** 1.109 + * Convert the object to a byte stream of the specified MIME type 1.110 + * and write it to the output stream. 1.111 + * 1.112 + * @param obj The object to be converted. 1.113 + * @param mimeType The requested MIME type of the resulting byte stream. 1.114 + * @param os The output stream into which to write the converted 1.115 + * byte stream. 1.116 + */ 1.117 + 1.118 + public void writeTo(Object obj, String type, OutputStream os) 1.119 + throws IOException { 1.120 + 1.121 + try { 1.122 + BufferedImage bufImage = null; 1.123 + if (obj instanceof BufferedImage) { 1.124 + bufImage = (BufferedImage)obj; 1.125 + } else if (obj instanceof Image) { 1.126 + bufImage = render((Image)obj); 1.127 + } else { 1.128 + log.log(Level.SEVERE, 1.129 + "SAAJ0520.soap.invalid.obj.type", 1.130 + new String[] { obj.getClass().toString() }); 1.131 + throw new IOException( 1.132 + "ImageDataContentHandler requires Image object, " 1.133 + + "was given object of type " 1.134 + + obj.getClass().toString()); 1.135 + } 1.136 + ImageWriter writer = null; 1.137 + Iterator i = ImageIO.getImageWritersByMIMEType(type); 1.138 + if (i.hasNext()) { 1.139 + writer = (ImageWriter)i.next(); 1.140 + } 1.141 + if (writer != null) { 1.142 + ImageOutputStream stream = null; 1.143 + stream = ImageIO.createImageOutputStream(os); 1.144 + writer.setOutput(stream); 1.145 + writer.write(bufImage); 1.146 + writer.dispose(); 1.147 + stream.close(); 1.148 + } else { 1.149 + log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type", 1.150 + new String[] { type }); 1.151 + throw new IOException("Unsupported mime type:"+ type); 1.152 + } 1.153 + } catch (Exception e) { 1.154 + log.severe("SAAJ0525.soap.cannot.encode.img"); 1.155 + throw new IOException("Unable to encode the image to a stream " 1.156 + + e.getMessage()); 1.157 + } 1.158 + } 1.159 + 1.160 + 1.161 + private BufferedImage render(Image img) throws InterruptedException { 1.162 + 1.163 + MediaTracker tracker = new MediaTracker(this); 1.164 + tracker.addImage(img, 0); 1.165 + tracker.waitForAll(); 1.166 + BufferedImage bufImage = new BufferedImage(img.getWidth(null), 1.167 + img.getHeight(null), BufferedImage.TYPE_INT_RGB); 1.168 + Graphics g = bufImage.createGraphics(); 1.169 + g.drawImage(img, 0, 0, null); 1.170 + g.dispose(); 1.171 + return bufImage; 1.172 + } 1.173 + 1.174 +}