001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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.javamail.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    import javax.imageio.stream.ImageInputStream;
036    
037    /**
038     * @version $Rev: 736909 $ $Date: 2009-01-22 23:30:17 -0500 (Thu, 22 Jan 2009) $
039     */
040    public class AbstractImageHandler implements DataContentHandler {
041        private final DataFlavor flavour;
042    
043        public AbstractImageHandler(DataFlavor flavour) {
044            this.flavour = flavour;
045        }
046    
047        public DataFlavor[] getTransferDataFlavors() {
048            return new DataFlavor[]{flavour};
049        }
050    
051        public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException, IOException {
052            return flavour.equals(dataFlavor) ? getContent(dataSource) : null;
053        }
054    
055        public Object getContent(DataSource ds) throws IOException {
056            Iterator i = ImageIO.getImageReadersByMIMEType(ds.getContentType());
057            if (!i.hasNext()) {
058                throw new UnsupportedDataTypeException("Unknown image type " + ds.getContentType());
059            }
060            ImageReader reader = (ImageReader) i.next();
061            ImageInputStream iis = ImageIO.createImageInputStream(ds.getInputStream());
062            reader.setInput(iis);
063            return reader.read(0);
064        }
065    
066        public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException {
067            Iterator i = ImageIO.getImageWritersByMIMEType(mimeType);
068            if (!i.hasNext()) {
069                throw new UnsupportedDataTypeException("Unknown image type " + mimeType);
070            }
071            ImageWriter writer = (ImageWriter) i.next();
072            writer.setOutput(os);
073    
074            if (obj instanceof RenderedImage) {
075                writer.write((RenderedImage) obj);
076            } else if (obj instanceof BufferedImage) {
077                BufferedImage buffered = (BufferedImage) obj;
078                writer.write(new IIOImage(buffered.getRaster(), null, null));
079            } else if (obj instanceof Image) {
080                Image image = (Image) obj;
081                BufferedImage buffered = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
082                Graphics2D graphics = buffered.createGraphics();
083                graphics.drawImage(image, 0, 0, null, null);
084                writer.write(new IIOImage(buffered.getRaster(), null, null));
085            } else {
086                throw new UnsupportedDataTypeException("Unknown image type " + obj.getClass().getName());
087            }
088            os.flush();
089        }
090    }