001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.activation.handlers;
018    
019    import java.awt.Graphics2D;
020    import java.awt.Image;
021    import java.awt.datatransfer.DataFlavor;
022    import java.awt.datatransfer.UnsupportedFlavorException;
023    import java.awt.image.BufferedImage;
024    import java.awt.image.RenderedImage;
025    import java.io.IOException;
026    import java.io.OutputStream;
027    import java.util.Iterator;
028    import javax.activation.DataContentHandler;
029    import javax.activation.DataSource;
030    import javax.activation.UnsupportedDataTypeException;
031    import javax.imageio.IIOImage;
032    import javax.imageio.ImageIO;
033    import javax.imageio.ImageReader;
034    import javax.imageio.ImageWriter;
035    
036    /**
037     * @version $Rev: 356022 $ $Date: 2005-12-11 12:58:34 -0800 (Sun, 11 Dec 2005) $
038     */
039    public class AbstractImageHandler implements DataContentHandler {
040        private final DataFlavor flavour;
041    
042        public AbstractImageHandler(DataFlavor flavour) {
043            this.flavour = flavour;
044        }
045    
046        public DataFlavor[] getTransferDataFlavors() {
047            return new DataFlavor[]{flavour};
048        }
049    
050        public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException, IOException {
051            return flavour.equals(dataFlavor) ? getContent(dataSource) : null;
052        }
053    
054        public Object getContent(DataSource ds) throws IOException {
055            Iterator i = ImageIO.getImageReadersByMIMEType(ds.getContentType());
056            if (!i.hasNext()) {
057                throw new UnsupportedDataTypeException();
058            }
059            ImageReader reader = (ImageReader) i.next();
060            return reader.read(0);
061        }
062    
063        public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException {
064            Iterator i = ImageIO.getImageWritersByMIMEType(mimeType);
065            if (!i.hasNext()) {
066                throw new UnsupportedDataTypeException();
067            }
068            ImageWriter writer = (ImageWriter) i.next();
069            writer.setOutput(os);
070    
071            if (obj instanceof RenderedImage) {
072                writer.write((RenderedImage) obj);
073            } else if (obj instanceof BufferedImage) {
074                BufferedImage buffered = (BufferedImage) obj;
075                writer.write(new IIOImage(buffered.getRaster(), null, null));
076            } else if (obj instanceof Image) {
077                Image image = (Image) obj;
078                BufferedImage buffered = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
079                Graphics2D graphics = buffered.createGraphics();
080                graphics.drawImage(image, 0, 0, null, null);
081                writer.write(new IIOImage(buffered.getRaster(), null, null));
082            } else {
083                throw new UnsupportedDataTypeException();
084            }
085            os.flush();
086        }
087    }