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.1 2007/09/18 02:33:22 jitu Exp $
022: * $Revision: 1.1 $
023: * $Date: 2007/09/18 02:33:22 $
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.ws.encoding;
062:
063: import javax.activation.DataContentHandler;
064: import javax.activation.ActivationDataFlavor;
065: import javax.activation.DataSource;
066: import javax.imageio.ImageIO;
067: import javax.imageio.ImageWriter;
068: import javax.imageio.stream.ImageOutputStream;
069: import java.awt.*;
070: import java.awt.image.BufferedImage;
071: import java.awt.datatransfer.DataFlavor;
072: import java.util.logging.Logger;
073: import java.util.logging.Level;
074: import java.util.Iterator;
075: import java.io.IOException;
076: import java.io.BufferedInputStream;
077: import java.io.OutputStream;
078:
079: /**
080: * @author Jitendra Kotamraju
081: */
082:
083: public class ImageDataContentHandler extends Component implements
084: DataContentHandler {
085:
086: private static final Logger log = Logger
087: .getLogger(ImageDataContentHandler.class.getName());
088: private final DataFlavor[] flavor;
089:
090: public ImageDataContentHandler() {
091: String[] mimeTypes = ImageIO.getReaderMIMETypes();
092: flavor = new DataFlavor[mimeTypes.length];
093: for (int i = 0; i < mimeTypes.length; i++) {
094: flavor[i] = new ActivationDataFlavor(Image.class,
095: mimeTypes[i], "Image");
096: }
097: }
098:
099: /**
100: * Returns an array of DataFlavor objects indicating the flavors the
101: * data can be provided in. The array should be ordered according to
102: * preference for providing the data (from most richly descriptive to
103: * least descriptive).
104: *
105: * @return The DataFlavors.
106: */
107: public DataFlavor[] getTransferDataFlavors() {
108: return flavor;
109: }
110:
111: /**
112: * Returns an object which represents the data to be transferred.
113: * The class of the object returned is defined by the representation class
114: * of the flavor.
115: *
116: * @param df The DataFlavor representing the requested type.
117: * @param ds The DataSource representing the data to be converted.
118: * @return The constructed Object.
119: */
120: public Object getTransferData(DataFlavor df, DataSource ds)
121: throws IOException {
122: for (DataFlavor aFlavor : flavor) {
123: if (aFlavor.equals(df)) {
124: return getContent(ds);
125: }
126: }
127: return null;
128: }
129:
130: /**
131: * Return an object representing the data in its most preferred form.
132: * Generally this will be the form described by the first DataFlavor
133: * returned by the <code>getTransferDataFlavors</code> method.
134: *
135: * @param ds The DataSource representing the data to be converted.
136: * @return The constructed Object.
137: */
138: public Object getContent(DataSource ds) throws IOException {
139: return ImageIO
140: .read(new BufferedInputStream(ds.getInputStream()));
141: }
142:
143: /**
144: * Convert the object to a byte stream of the specified MIME type
145: * and write it to the output stream.
146: *
147: * @param obj The object to be converted.
148: * @param type The requested MIME type of the resulting byte stream.
149: * @param os The output stream into which to write the converted
150: * byte stream.
151: */
152:
153: public void writeTo(Object obj, String type, OutputStream os)
154: throws IOException {
155:
156: try {
157: BufferedImage bufImage;
158: if (obj instanceof BufferedImage) {
159: bufImage = (BufferedImage) obj;
160: } else if (obj instanceof Image) {
161: bufImage = render((Image) obj);
162: } else {
163: throw new IOException(
164: "ImageDataContentHandler requires Image object, "
165: + "was given object of type "
166: + obj.getClass().toString());
167: }
168: ImageWriter writer = null;
169: Iterator i = ImageIO.getImageWritersByMIMEType(type);
170: if (i.hasNext()) {
171: writer = (ImageWriter) i.next();
172: }
173: if (writer != null) {
174: ImageOutputStream stream = ImageIO
175: .createImageOutputStream(os);
176: writer.setOutput(stream);
177: writer.write(bufImage);
178: writer.dispose();
179: stream.close();
180: } else {
181: throw new IOException("Unsupported mime type:" + type);
182: }
183: } catch (Exception e) {
184: throw new IOException(
185: "Unable to encode the image to a stream "
186: + e.getMessage());
187: }
188: }
189:
190: private BufferedImage render(Image img) throws InterruptedException {
191:
192: MediaTracker tracker = new MediaTracker(this );
193: tracker.addImage(img, 0);
194: tracker.waitForAll();
195: BufferedImage bufImage = new BufferedImage(img.getWidth(null),
196: img.getHeight(null), BufferedImage.TYPE_INT_RGB);
197: Graphics g = bufImage.createGraphics();
198: g.drawImage(img, 0, 0, null);
199: g.dispose();
200: return bufImage;
201: }
202:
203: }
|