You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2010/05/18 22:08:47 UTC

svn commit: r945859 - in /cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment: AttachmentImpl.java AttachmentSerializer.java AttachmentUtil.java ImageDataContentHandler.java

Author: dkulp
Date: Tue May 18 20:08:46 2010
New Revision: 945859

URL: http://svn.apache.org/viewvc?rev=945859&view=rev
Log:
Register a DataContentHandler for images to make them work a bit more
reliably depending on the mail/activation version found on the classpath

Added:
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/ImageDataContentHandler.java   (with props)
Modified:
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentImpl.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentImpl.java?rev=945859&r1=945858&r2=945859&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentImpl.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentImpl.java Tue May 18 20:08:46 2010
@@ -41,6 +41,7 @@ public class AttachmentImpl implements A
     public AttachmentImpl(String idParam, DataHandler handlerParam) {
         this.id = idParam;
         this.dataHandler = handlerParam;
+        this.dataHandler.setCommandMap(AttachmentUtil.getCommandMap());
     }
 
     public String getId() {
@@ -53,6 +54,7 @@ public class AttachmentImpl implements A
 
     public void setDataHandler(DataHandler dataHandler) {
         this.dataHandler = dataHandler;
+        this.dataHandler.setCommandMap(AttachmentUtil.getCommandMap());
     }
 
     public void setHeader(String name, String value) {

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java?rev=945859&r1=945858&r2=945859&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java Tue May 18 20:08:46 2010
@@ -30,12 +30,11 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.activation.DataHandler;
 import org.apache.cxf.message.Attachment;
 import org.apache.cxf.message.Message;
 
-
 public class AttachmentSerializer {
-
     private Message message;
     private String bodyBoundary;
     private OutputStream out;
@@ -221,11 +220,14 @@ public class AttachmentSerializer {
                     headers = Collections.emptyMap();
                 }
                 
-                writeHeaders(a.getDataHandler().getContentType(), a.getId(),
+                
+                DataHandler handler = a.getDataHandler();
+                handler.setCommandMap(AttachmentUtil.getCommandMap());
+                
+                writeHeaders(handler.getContentType(), a.getId(),
                              headers, writer);
                 out.write(writer.getBuffer().toString().getBytes(encoding));
-                
-                a.getDataHandler().writeTo(out);
+                handler.writeTo(out);
             }
         }
         StringWriter writer = new StringWriter();                
@@ -244,5 +246,5 @@ public class AttachmentSerializer {
     public void setXop(boolean xop) {
         this.xop = xop;
     }
-    
+
 }

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java?rev=945859&r1=945858&r2=945859&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java (original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java Tue May 18 20:08:46 2010
@@ -37,9 +37,11 @@ import java.util.Map;
 import java.util.Random;
 import java.util.UUID;
 
+import javax.activation.CommandMap;
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
 import javax.activation.FileDataSource;
+import javax.activation.MailcapCommandMap;
 import javax.activation.URLDataSource;
 import javax.mail.Header;
 import javax.mail.internet.InternetHeaders;
@@ -56,11 +58,22 @@ public final class AttachmentUtil {
     private static final String ATT_UUID = UUID.randomUUID().toString();
     
     private static final Random BOUND_RANDOM = new Random();
-    
+    private static final MailcapCommandMap COMMAND_MAP = new MailcapCommandMap();
+
+
     private AttachmentUtil() {
 
     }
+    
+    static {
+        COMMAND_MAP.addMailcap("image/*;;x-java-content-handler=" 
+                               + ImageDataContentHandler.class.getName());
+    }
 
+    public static CommandMap getCommandMap() {
+        return COMMAND_MAP;
+    }
+    
     /**
      * @param ns
      * @return

Added: cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/ImageDataContentHandler.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/ImageDataContentHandler.java?rev=945859&view=auto
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/ImageDataContentHandler.java (added)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/ImageDataContentHandler.java Tue May 18 20:08:46 2010
@@ -0,0 +1,117 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.attachment;
+
+import java.awt.Component;
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.MediaTracker;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Iterator;
+
+import javax.activation.ActivationDataFlavor;
+import javax.activation.DataContentHandler;
+import javax.activation.DataSource;
+import javax.imageio.ImageIO;
+import javax.imageio.ImageWriter;
+import javax.imageio.stream.ImageOutputStream;
+
+/**
+ * 
+ */
+public class ImageDataContentHandler implements DataContentHandler {
+    private static final DataFlavor[] FLAVORS;
+    static {
+        String[] types = ImageIO.getReaderMIMETypes();
+        FLAVORS = new DataFlavor[types.length];
+        int i = 0;
+        for (String type : types) {
+            FLAVORS[i++] = new ActivationDataFlavor(Image.class, type, "Image");
+        }
+    }
+
+    public Object getContent(DataSource ds) throws IOException {
+        return ImageIO.read(ds.getInputStream());
+    }
+
+    public Object getTransferData(DataFlavor df, DataSource ds) throws UnsupportedFlavorException,
+        IOException {
+        for (DataFlavor f : FLAVORS) {
+            if (f.equals(df)) {
+                return getContent(ds);
+            }
+        }
+        return null;
+    }
+
+    public DataFlavor[] getTransferDataFlavors() {
+        return FLAVORS;
+    }
+
+    public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException {
+        Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(mimeType);
+        if (writers.hasNext()) {
+            ImageWriter writer = writers.next();
+            
+            BufferedImage bimg = convertToBufferedImage((Image)obj);
+            ImageOutputStream out = ImageIO.createImageOutputStream(os); 
+            writer.setOutput(out);
+            writer.write(bimg);
+            writer.dispose();
+            out.flush();
+            out.close();
+        } else {
+            throw new IOException("Attachment type not spported " + obj.getClass());                    
+        }
+
+    }
+    private static BufferedImage convertToBufferedImage(Image image) throws IOException {
+        if (image instanceof BufferedImage) {
+            return (BufferedImage)image;
+        }
+        
+        // Wait until the image is completely loaded
+        MediaTracker tracker = new MediaTracker(new Component() { });
+        tracker.addImage(image, 0);
+        try {
+            tracker.waitForAll();
+        } catch (InterruptedException e) {
+            IOException ioe = new IOException(e.getMessage());
+            ioe.initCause(e);
+            throw ioe;
+        }
+        
+        // Create a BufferedImage so we can write it out later
+        BufferedImage bufImage = new BufferedImage(
+                image.getWidth(null),
+                image.getHeight(null),
+                BufferedImage.TYPE_INT_ARGB);
+
+        Graphics g = bufImage.createGraphics();
+        g.drawImage(image, 0, 0, null);
+        g.dispose();
+        return bufImage;
+    }
+
+}

Propchange: cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/ImageDataContentHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/ImageDataContentHandler.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date