src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEPart.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -27,26 +27,32 @@
 
 import java.io.File;
 import java.io.InputStream;
 import java.nio.ByteBuffer;
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Represents an attachment part in a MIME message. MIME message parsing is done
  * lazily using a pull parser, so the part may not have all the data. {@link #read}
  * and {@link #readOnce} may trigger the actual parsing the message. In fact,
  * parsing of an attachment part may be triggered by calling {@link #read} methods
- * on some other attachemnt parts. All this happens behind the scenes so the
+ * on some other attachment parts. All this happens behind the scenes so the
  * application developer need not worry about these details.
  *
- * @author Jitendra Kotamraju
+ * @author Jitendra Kotamraju, Martin Grebac
  */
 public class MIMEPart {
 
+    private static final Logger LOGGER = Logger.getLogger(MIMEPart.class.getName());
+
     private volatile InternetHeaders headers;
     private volatile String contentId;
     private String contentType;
+    private String contentTransferEncoding;
+
     volatile boolean parsed;    // part is parsed or not
     final MIMEMessage msg;
     private final DataHead dataHead;
 
     MIMEPart(MIMEMessage msg) {

@@ -67,11 +73,19 @@
      * into a object that returns InputStream for e.g DataHandler)
      *
      * @return data for the part's content
      */
     public InputStream read() {
-        return dataHead.read();
+        InputStream is = null;
+        try {
+            is = MimeUtility.decode(dataHead.read(), contentTransferEncoding);
+        } catch (DecodingException ex) { //ignore
+            if (LOGGER.isLoggable(Level.WARNING)) {
+                LOGGER.log(Level.WARNING, null, ex);
+            }
+        }
+        return is;
     }
 
     /**
      * Cleans up any resources that are held by this part (for e.g. deletes
      * the temp file that is used to serve this part's content). After

@@ -79,11 +93,10 @@
      */
     public void close() {
         dataHead.close();
     }
 
-
     /**
      * Can get the attachment part's content only once. The content
      * will be lost after the method. Content data is not be stored
      * on the file system or is not kept in the memory for the
      * following case:

@@ -93,11 +106,19 @@
      * once.
      *
      * @return data for the part's content
      */
     public InputStream readOnce() {
-        return dataHead.readOnce();
+        InputStream is = null;
+        try {
+            is = MimeUtility.decode(dataHead.readOnce(), contentTransferEncoding);
+        } catch (DecodingException ex) { //ignore
+            if (LOGGER.isLoggable(Level.WARNING)) {
+                LOGGER.log(Level.WARNING, null, ex);
+            }
+        }
+        return is;
     }
 
     public void moveTo(File f) {
         dataHead.moveTo(f);
     }

@@ -113,10 +134,22 @@
         }
         return contentId;
     }
 
     /**
+     * Returns Content-Transfer-Encoding MIME header for this attachment part
+     *
+     * @return Content-Transfer-Encoding of the part
+     */
+    public String getContentTransferEncoding() {
+        if (contentTransferEncoding == null) {
+            getHeaders();
+        }
+        return contentTransferEncoding;
+    }
+
+    /**
      * Returns Content-Type MIME header for this attachment part
      *
      * @return Content-Type of the part
      */
     public String getContentType() {

@@ -169,10 +202,12 @@
      */
     void setHeaders(InternetHeaders headers) {
         this.headers = headers;
         List<String> ct = getHeader("Content-Type");
         this.contentType = (ct == null) ? "application/octet-stream" : ct.get(0);
+        List<String> cte = getHeader("Content-Transfer-Encoding");
+        this.contentTransferEncoding = (cte == null) ? "binary" : cte.get(0);
     }
 
     /**
      * Callback to notify that there is a partial content for the part
      *

@@ -197,11 +232,19 @@
      */
     void setContentId(String cid) {
         this.contentId = cid;
     }
 
+    /**
+     * Callback to set Content-Transfer-Encoding for this part
+     * @param cte Content-Transfer-Encoding of the part
+     */
+    void setContentTransferEncoding(String cte) {
+        this.contentTransferEncoding = cte;
+    }
+
     @Override
     public String toString() {
-        return "Part="+contentId;
+        return "Part="+contentId+":"+contentTransferEncoding;
     }
 
 }