001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: ImageDataContentHandler.java,v 1.2 2007/07/16 16:41:21 ofung Exp $
022: * $Revision: 1.2 $
023: * $Date: 2007/07/16 16:41:21 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package com.sun.xml.messaging.saaj.soap;
062:
063: import java.awt.*;
064: import java.awt.datatransfer.DataFlavor;
065: import java.awt.image.BufferedImage;
066: import java.io.*;
067: import java.util.Iterator;
068: import java.util.logging.Level;
069: import java.util.logging.Logger;
070:
071: import javax.activation.*;
072: import javax.imageio.ImageIO;
073: import javax.imageio.ImageWriter;
074: import javax.imageio.stream.ImageOutputStream;
075:
076: import com.sun.xml.messaging.saaj.util.LogDomainConstants;
077:
078: public class ImageDataContentHandler extends Component implements
079: DataContentHandler {
080:
081: protected static Logger log = Logger.getLogger(
082: LogDomainConstants.SOAP_DOMAIN,
083: "com.sun.xml.messaging.saaj.soap.LocalStrings");
084:
085: private DataFlavor[] flavor;
086:
087: public ImageDataContentHandler() {
088: String[] mimeTypes = ImageIO.getReaderMIMETypes();
089: flavor = new DataFlavor[mimeTypes.length];
090: for (int i = 0; i < mimeTypes.length; i++) {
091: flavor[i] = new ActivationDataFlavor(java.awt.Image.class,
092: mimeTypes[i], "Image");
093: }
094: }
095:
096: /**
097: * Returns an array of DataFlavor objects indicating the flavors the
098: * data can be provided in. The array should be ordered according to
099: * preference for providing the data (from most richly descriptive to
100: * least descriptive).
101: *
102: * @return The DataFlavors.
103: */
104: public DataFlavor[] getTransferDataFlavors() {
105: return flavor;
106: }
107:
108: /**
109: * Returns an object which represents the data to be transferred.
110: * The class of the object returned is defined by the representation class
111: * of the flavor.
112: *
113: * @param df The DataFlavor representing the requested type.
114: * @param ds The DataSource representing the data to be converted.
115: * @return The constructed Object.
116: */
117: public Object getTransferData(DataFlavor df, DataSource ds)
118: throws IOException {
119: for (int i = 0; i < flavor.length; i++) {
120: if (flavor[i].equals(df)) {
121: return getContent(ds);
122: }
123: }
124: return null;
125: }
126:
127: /**
128: * Return an object representing the data in its most preferred form.
129: * Generally this will be the form described by the first DataFlavor
130: * returned by the <code>getTransferDataFlavors</code> method.
131: *
132: * @param ds The DataSource representing the data to be converted.
133: * @return The constructed Object.
134: */
135: public Object getContent(DataSource ds) throws IOException {
136: return ImageIO
137: .read(new BufferedInputStream(ds.getInputStream()));
138: }
139:
140: /**
141: * Convert the object to a byte stream of the specified MIME type
142: * and write it to the output stream.
143: *
144: * @param obj The object to be converted.
145: * @param mimeType The requested MIME type of the resulting byte stream.
146: * @param os The output stream into which to write the converted
147: * byte stream.
148: */
149:
150: public void writeTo(Object obj, String type, OutputStream os)
151: throws IOException {
152:
153: try {
154: BufferedImage bufImage = null;
155: if (obj instanceof BufferedImage) {
156: bufImage = (BufferedImage) obj;
157: } else if (obj instanceof Image) {
158: bufImage = render((Image) obj);
159: } else {
160: log.log(Level.SEVERE, "SAAJ0520.soap.invalid.obj.type",
161: new String[] { obj.getClass().toString() });
162: throw new IOException(
163: "ImageDataContentHandler requires Image object, "
164: + "was given object of type "
165: + obj.getClass().toString());
166: }
167: ImageWriter writer = null;
168: Iterator i = ImageIO.getImageWritersByMIMEType(type);
169: if (i.hasNext()) {
170: writer = (ImageWriter) i.next();
171: }
172: if (writer != null) {
173: ImageOutputStream stream = null;
174: stream = ImageIO.createImageOutputStream(os);
175: writer.setOutput(stream);
176: writer.write(bufImage);
177: stream.close();
178: } else {
179: log.log(Level.SEVERE,
180: "SAAJ0526.soap.unsupported.mime.type",
181: new String[] { type });
182: throw new IOException("Unsupported mime type:" + type);
183: }
184: } catch (Exception e) {
185: log.severe("SAAJ0525.soap.cannot.encode.img");
186: throw new IOException(
187: "Unable to encode the image to a stream "
188: + e.getMessage());
189: }
190: }
191:
192: private BufferedImage render(Image img) throws InterruptedException {
193:
194: MediaTracker tracker = new MediaTracker(this );
195: tracker.addImage(img, 0);
196: tracker.waitForAll();
197: BufferedImage bufImage = new BufferedImage(img.getWidth(null),
198: img.getHeight(null), BufferedImage.TYPE_INT_RGB);
199: Graphics g = bufImage.createGraphics();
200: g.drawImage(img, 0, 0, null);
201: g.dispose();
202: return bufImage;
203: }
204:
205: }
|