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

     1 /*
     2  * Copyright 2005-2006 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    25 /*
    26  * $Id: ImageDataContentHandler.java,v 1.1.1.1.2.1 2007/11/27 07:19:28 kumarjayanti Exp $
    27  * $Revision: 1.1.1.1.2.1 $
    28  * $Date: 2007/11/27 07:19:28 $
    29  */
    32 package com.sun.xml.internal.messaging.saaj.soap;
    34 import java.awt.*;
    35 import java.awt.datatransfer.DataFlavor;
    36 import java.awt.image.BufferedImage;
    37 import java.io.*;
    38 import java.util.Iterator;
    39 import java.util.logging.Level;
    40 import java.util.logging.Logger;
    42 import javax.activation.*;
    43 import javax.imageio.ImageIO;
    44 import javax.imageio.ImageWriter;
    45 import javax.imageio.stream.ImageOutputStream;
    47 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
    49 public class ImageDataContentHandler extends Component
    50     implements DataContentHandler {
    52     protected static final Logger log =
    53         Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
    54                          "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
    56     private DataFlavor[] flavor;
    58     public ImageDataContentHandler() {
    59         String[] mimeTypes = ImageIO.getReaderMIMETypes();
    60         flavor = new DataFlavor[mimeTypes.length];
    61         for(int i=0; i < mimeTypes.length; i++) {
    62             flavor[i] = new ActivationDataFlavor(
    63                 java.awt.Image.class, mimeTypes[i], "Image");
    64         }
    65     }
    67     /**
    68      * Returns an array of DataFlavor objects indicating the flavors the
    69      * data can be provided in. The array should be ordered according to
    70      * preference for providing the data (from most richly descriptive to
    71      * least descriptive).
    72      *
    73      * @return The DataFlavors.
    74      */
    75     public DataFlavor[] getTransferDataFlavors() {
    76         return flavor;
    77     }
    79     /**
    80      * Returns an object which represents the data to be transferred.
    81      * The class of the object returned is defined by the representation class
    82      * of the flavor.
    83      *
    84      * @param df The DataFlavor representing the requested type.
    85      * @param ds The DataSource representing the data to be converted.
    86      * @return The constructed Object.
    87      */
    88     public Object getTransferData(DataFlavor df, DataSource ds)
    89         throws IOException {
    90         for (int i=0; i < flavor.length; i++) {
    91             if (flavor[i].equals(df)) {
    92                 return getContent(ds);
    93             }
    94         }
    95         return null;
    96     }
    98     /**
    99      * Return an object representing the data in its most preferred form.
   100      * Generally this will be the form described by the first DataFlavor
   101      * returned by the <code>getTransferDataFlavors</code> method.
   102      *
   103      * @param ds The DataSource representing the data to be converted.
   104      * @return The constructed Object.
   105      */
   106     public Object getContent(DataSource ds) throws IOException {
   107         return ImageIO.read(new BufferedInputStream(ds.getInputStream()));
   108     }
   110     /**
   111      * Convert the object to a byte stream of the specified MIME type
   112      * and write it to the output stream.
   113      *
   114      * @param obj   The object to be converted.
   115      * @param mimeType  The requested MIME type of the resulting byte stream.
   116      * @param os    The output stream into which to write the converted
   117      *          byte stream.
   118      */
   120     public void writeTo(Object obj, String type, OutputStream os)
   121         throws IOException {
   123         try {
   124             BufferedImage bufImage = null;
   125             if (obj instanceof BufferedImage) {
   126                 bufImage = (BufferedImage)obj;
   127             } else if (obj instanceof Image) {
   128                 bufImage = render((Image)obj);
   129             } else {
   130                 log.log(Level.SEVERE,
   131                     "SAAJ0520.soap.invalid.obj.type",
   132                     new String[] { obj.getClass().toString() });
   133                 throw new IOException(
   134                     "ImageDataContentHandler requires Image object, "
   135                     + "was given object of type "
   136                     + obj.getClass().toString());
   137             }
   138             ImageWriter writer = null;
   139             Iterator i = ImageIO.getImageWritersByMIMEType(type);
   140             if (i.hasNext()) {
   141                 writer = (ImageWriter)i.next();
   142             }
   143             if (writer != null) {
   144                 ImageOutputStream stream = null;
   145                 stream = ImageIO.createImageOutputStream(os);
   146                 writer.setOutput(stream);
   147                 writer.write(bufImage);
   148                 writer.dispose();
   149                 stream.close();
   150             } else {
   151                 log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type",
   152                     new String[] { type });
   153                 throw new IOException("Unsupported mime type:"+ type);
   154             }
   155         } catch (Exception e) {
   156             log.severe("SAAJ0525.soap.cannot.encode.img");
   157             throw new IOException("Unable to encode the image to a stream "
   158                 + e.getMessage());
   159         }
   160     }
   163     private BufferedImage render(Image img) throws InterruptedException {
   165         MediaTracker tracker = new MediaTracker(this);
   166         tracker.addImage(img, 0);
   167         tracker.waitForAll();
   168         BufferedImage bufImage = new BufferedImage(img.getWidth(null),
   169             img.getHeight(null), BufferedImage.TYPE_INT_RGB);
   170         Graphics g = bufImage.createGraphics();
   171         g.drawImage(img, 0, 0, null);
   172         g.dispose();
   173         return bufImage;
   174     }
   176 }

mercurial