blob: 8f063f155c9c30c1ee88b63173260a53738397f3 [file] [log] [blame]
Kelly O'Hairbc113af2012-03-06 16:09:35 -08001/*
Roman Grigoriadi14967722017-02-16 13:14:39 +03002 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
Kelly O'Hairbc113af2012-03-06 16:09:35 -08003 * 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
26package com.sun.xml.internal.messaging.saaj.soap;
27
28import java.awt.*;
29import java.awt.datatransfer.DataFlavor;
30import java.awt.image.BufferedImage;
31import java.io.*;
32import java.util.Arrays;
33import java.util.Iterator;
34import java.util.logging.Level;
35import java.util.logging.Logger;
36
37import javax.activation.*;
38import javax.imageio.ImageIO;
39import javax.imageio.ImageWriter;
40import javax.imageio.stream.ImageOutputStream;
41
42import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
43
44public 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 */
Roman Grigoriadi14967722017-02-16 13:14:39 +030070 @Override
Kelly O'Hairbc113af2012-03-06 16:09:35 -080071 public DataFlavor[] getTransferDataFlavors() {
Roman Grigoriadi14967722017-02-16 13:14:39 +030072 return Arrays.copyOf(flavor, flavor.length);
Kelly O'Hairbc113af2012-03-06 16:09:35 -080073 }
74
75 /**
76 * Returns an object which represents the data to be transferred.
77 * The class of the object returned is defined by the representation class
78 * of the flavor.
79 *
80 * @param df The DataFlavor representing the requested type.
81 * @param ds The DataSource representing the data to be converted.
82 * @return The constructed Object.
83 */
Roman Grigoriadi14967722017-02-16 13:14:39 +030084 @Override
Kelly O'Hairbc113af2012-03-06 16:09:35 -080085 public Object getTransferData(DataFlavor df, DataSource ds)
86 throws IOException {
87 for (int i=0; i < flavor.length; i++) {
88 if (flavor[i].equals(df)) {
89 return getContent(ds);
90 }
91 }
92 return null;
93 }
94
95 /**
96 * Return an object representing the data in its most preferred form.
97 * Generally this will be the form described by the first DataFlavor
98 * returned by the <code>getTransferDataFlavors</code> method.
99 *
100 * @param ds The DataSource representing the data to be converted.
101 * @return The constructed Object.
102 */
Roman Grigoriadi14967722017-02-16 13:14:39 +0300103 @Override
Kelly O'Hairbc113af2012-03-06 16:09:35 -0800104 public Object getContent(DataSource ds) throws IOException {
105 return ImageIO.read(new BufferedInputStream(ds.getInputStream()));
106 }
107
108 /**
109 * Convert the object to a byte stream of the specified MIME type
110 * and write it to the output stream.
111 *
112 * @param obj The object to be converted.
Roman Grigoriadi14967722017-02-16 13:14:39 +0300113 * @param type The requested MIME type of the resulting byte stream.
Kelly O'Hairbc113af2012-03-06 16:09:35 -0800114 * @param os The output stream into which to write the converted
115 * byte stream.
116 */
Roman Grigoriadi14967722017-02-16 13:14:39 +0300117 @Override
Kelly O'Hairbc113af2012-03-06 16:09:35 -0800118 public void writeTo(Object obj, String type, OutputStream os)
119 throws IOException {
120
121 try {
122 BufferedImage bufImage = null;
123 if (obj instanceof BufferedImage) {
124 bufImage = (BufferedImage)obj;
125 } else if (obj instanceof Image) {
126 bufImage = render((Image)obj);
127 } else {
128 log.log(Level.SEVERE,
129 "SAAJ0520.soap.invalid.obj.type",
130 new String[] { obj.getClass().toString() });
131 throw new IOException(
132 "ImageDataContentHandler requires Image object, "
133 + "was given object of type "
134 + obj.getClass().toString());
135 }
136 ImageWriter writer = null;
Miroslav Kos096249e2015-10-30 10:34:46 +0100137 Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(type);
Kelly O'Hairbc113af2012-03-06 16:09:35 -0800138 if (i.hasNext()) {
Miroslav Kos096249e2015-10-30 10:34:46 +0100139 writer = i.next();
Kelly O'Hairbc113af2012-03-06 16:09:35 -0800140 }
141 if (writer != null) {
142 ImageOutputStream stream = null;
143 stream = ImageIO.createImageOutputStream(os);
144 writer.setOutput(stream);
145 writer.write(bufImage);
146 writer.dispose();
147 stream.close();
148 } else {
149 log.log(Level.SEVERE, "SAAJ0526.soap.unsupported.mime.type",
150 new String[] { type });
151 throw new IOException("Unsupported mime type:"+ type);
152 }
153 } catch (Exception e) {
154 log.severe("SAAJ0525.soap.cannot.encode.img");
155 throw new IOException("Unable to encode the image to a stream "
156 + e.getMessage());
157 }
158 }
159
160
161 private BufferedImage render(Image img) throws InterruptedException {
162
163 MediaTracker tracker = new MediaTracker(this);
164 tracker.addImage(img, 0);
165 tracker.waitForAll();
166 BufferedImage bufImage = new BufferedImage(img.getWidth(null),
167 img.getHeight(null), BufferedImage.TYPE_INT_RGB);
168 Graphics g = bufImage.createGraphics();
169 g.drawImage(img, 0, 0, null);
170 g.dispose();
171 return bufImage;
172 }
173
174}