src/share/classes/com/sun/xml/internal/messaging/saaj/soap/ImageDataContentHandler.java

Mon, 04 May 2009 21:10:41 -0700

author
tbell
date
Mon, 04 May 2009 21:10:41 -0700
changeset 50
42dfec6871f6
parent 45
31822b475baa
child 78
860b95cc8d1d
permissions
-rw-r--r--

6658158: Mutable statics in SAAJ (findbugs)
6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs)
Reviewed-by: darcy

duke@1 1 /*
tbell@45 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
tbell@45 25 /*
tbell@45 26 * $Id: ImageDataContentHandler.java,v 1.1.1.1.2.1 2007/11/27 07:19:28 kumarjayanti Exp $
tbell@45 27 * $Revision: 1.1.1.1.2.1 $
tbell@45 28 * $Date: 2007/11/27 07:19:28 $
tbell@45 29 */
tbell@45 30
tbell@45 31
duke@1 32 package com.sun.xml.internal.messaging.saaj.soap;
duke@1 33
duke@1 34 import java.awt.*;
duke@1 35 import java.awt.datatransfer.DataFlavor;
duke@1 36 import java.awt.image.BufferedImage;
duke@1 37 import java.io.*;
duke@1 38 import java.util.Iterator;
duke@1 39 import java.util.logging.Level;
duke@1 40 import java.util.logging.Logger;
duke@1 41
duke@1 42 import javax.activation.*;
duke@1 43 import javax.imageio.ImageIO;
duke@1 44 import javax.imageio.ImageWriter;
duke@1 45 import javax.imageio.stream.ImageOutputStream;
duke@1 46
duke@1 47 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
duke@1 48
duke@1 49 public class ImageDataContentHandler extends Component
duke@1 50 implements DataContentHandler {
duke@1 51
tbell@50 52 protected static final Logger log =
duke@1 53 Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
duke@1 54 "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
duke@1 55
duke@1 56 private DataFlavor[] flavor;
duke@1 57
duke@1 58 public ImageDataContentHandler() {
duke@1 59 String[] mimeTypes = ImageIO.getReaderMIMETypes();
duke@1 60 flavor = new DataFlavor[mimeTypes.length];
duke@1 61 for(int i=0; i < mimeTypes.length; i++) {
duke@1 62 flavor[i] = new ActivationDataFlavor(
duke@1 63 java.awt.Image.class, mimeTypes[i], "Image");
duke@1 64 }
duke@1 65 }
duke@1 66
duke@1 67 /**
duke@1 68 * Returns an array of DataFlavor objects indicating the flavors the
duke@1 69 * data can be provided in. The array should be ordered according to
duke@1 70 * preference for providing the data (from most richly descriptive to
duke@1 71 * least descriptive).
duke@1 72 *
duke@1 73 * @return The DataFlavors.
duke@1 74 */
duke@1 75 public DataFlavor[] getTransferDataFlavors() {
duke@1 76 return flavor;
duke@1 77 }
duke@1 78
duke@1 79 /**
duke@1 80 * Returns an object which represents the data to be transferred.
duke@1 81 * The class of the object returned is defined by the representation class
duke@1 82 * of the flavor.
duke@1 83 *
duke@1 84 * @param df The DataFlavor representing the requested type.
duke@1 85 * @param ds The DataSource representing the data to be converted.
duke@1 86 * @return The constructed Object.
duke@1 87 */
duke@1 88 public Object getTransferData(DataFlavor df, DataSource ds)
duke@1 89 throws IOException {
duke@1 90 for (int i=0; i < flavor.length; i++) {
duke@1 91 if (flavor[i].equals(df)) {
duke@1 92 return getContent(ds);
duke@1 93 }
duke@1 94 }
duke@1 95 return null;
duke@1 96 }
duke@1 97
duke@1 98 /**
duke@1 99 * Return an object representing the data in its most preferred form.
duke@1 100 * Generally this will be the form described by the first DataFlavor
duke@1 101 * returned by the <code>getTransferDataFlavors</code> method.
duke@1 102 *
duke@1 103 * @param ds The DataSource representing the data to be converted.
duke@1 104 * @return The constructed Object.
duke@1 105 */
duke@1 106 public Object getContent(DataSource ds) throws IOException {
duke@1 107 return ImageIO.read(new BufferedInputStream(ds.getInputStream()));
duke@1 108 }
duke@1 109
duke@1 110 /**
duke@1 111 * Convert the object to a byte stream of the specified MIME type
duke@1 112 * and write it to the output stream.
duke@1 113 *
duke@1 114 * @param obj The object to be converted.
duke@1 115 * @param mimeType The requested MIME type of the resulting byte stream.
duke@1 116 * @param os The output stream into which to write the converted
duke@1 117 * byte stream.
duke@1 118 */
duke@1 119
duke@1 120 public void writeTo(Object obj, String type, OutputStream os)
duke@1 121 throws IOException {
duke@1 122
duke@1 123 try {
duke@1 124 BufferedImage bufImage = null;
duke@1 125 if (obj instanceof BufferedImage) {
duke@1 126 bufImage = (BufferedImage)obj;
duke@1 127 } else if (obj instanceof Image) {
duke@1 128 bufImage = render((Image)obj);
duke@1 129 } else {
duke@1 130 log.log(Level.SEVERE,
duke@1 131 "SAAJ0520.soap.invalid.obj.type",
duke@1 132 new String[] { obj.getClass().toString() });
duke@1 133 throw new IOException(
duke@1 134 "ImageDataContentHandler requires Image object, "
duke@1 135 + "was given object of type "
duke@1 136 + obj.getClass().toString());
duke@1 137 }
duke@1 138 ImageWriter writer = null;
duke@1 139 Iterator i = ImageIO.getImageWritersByMIMEType(type);
duke@1 140 if (i.hasNext()) {
duke@1 141 writer = (ImageWriter)i.next();
duke@1 142 }
duke@1 143 if (writer != null) {
duke@1 144 ImageOutputStream stream = null;
duke@1 145 stream = ImageIO.createImageOutputStream(os);
duke@1 146 writer.setOutput(stream);
duke@1 147 writer.write(bufImage);
tbell@45 148 writer.dispose();
duke@1 149 stream.close();
duke@1 150 } else {
duke@1 151 log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type",
duke@1 152 new String[] { type });
duke@1 153 throw new IOException("Unsupported mime type:"+ type);
duke@1 154 }
duke@1 155 } catch (Exception e) {
duke@1 156 log.severe("SAAJ0525.soap.cannot.encode.img");
duke@1 157 throw new IOException("Unable to encode the image to a stream "
duke@1 158 + e.getMessage());
duke@1 159 }
duke@1 160 }
duke@1 161
duke@1 162
duke@1 163 private BufferedImage render(Image img) throws InterruptedException {
duke@1 164
duke@1 165 MediaTracker tracker = new MediaTracker(this);
duke@1 166 tracker.addImage(img, 0);
duke@1 167 tracker.waitForAll();
duke@1 168 BufferedImage bufImage = new BufferedImage(img.getWidth(null),
duke@1 169 img.getHeight(null), BufferedImage.TYPE_INT_RGB);
duke@1 170 Graphics g = bufImage.createGraphics();
duke@1 171 g.drawImage(img, 0, 0, null);
duke@1 172 g.dispose();
duke@1 173 return bufImage;
duke@1 174 }
duke@1 175
duke@1 176 }

mercurial