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

Fri, 23 May 2014 16:25:43 +0200

author
mkos
date
Fri, 23 May 2014 16:25:43 +0200
changeset 545
ce46e4af2b1d
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8043129: JAF initialisation in SAAJ clashing with the one in javax.mail
Reviewed-by: chegar

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

mercurial