]> source.dussan.org Git - poi.git/commitdiff
Try (as best as possible with no test file) to extract uncompressed HMEF test when...
authorNick Burch <nick@apache.org>
Mon, 4 Jul 2016 18:06:14 +0000 (18:06 +0000)
committerNick Burch <nick@apache.org>
Mon, 4 Jul 2016 18:06:14 +0000 (18:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751361 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java
src/scratchpad/src/org/apache/poi/hsmf/datatypes/MAPIProperty.java

index 06a1cdf4c75ba55cdc7ecdf881f326b43f688ceb..e649fd154edd55b28da037844a7988e5127fac0a 100644 (file)
@@ -26,8 +26,11 @@ import java.io.OutputStream;
 
 import org.apache.poi.hmef.Attachment;
 import org.apache.poi.hmef.HMEFMessage;
+import org.apache.poi.hmef.attribute.MAPIAttribute;
 import org.apache.poi.hmef.attribute.MAPIRtfAttribute;
+import org.apache.poi.hmef.attribute.MAPIStringAttribute;
 import org.apache.poi.hsmf.datatypes.MAPIProperty;
+import org.apache.poi.hsmf.datatypes.Types;
 
 /**
  * A utility for extracting out the message body, and all attachments
@@ -77,16 +80,43 @@ public final class HMEFContentsExtractor {
      * Extracts the RTF message body to the supplied file
      */
     public void extractMessageBody(File dest) throws IOException {
+        MAPIAttribute body = getBodyAttribute();
+        if (body == null) {
+            System.err.println("No message body found, " + dest + " not created");
+            return;
+        }
+        if (body instanceof MAPIStringAttribute) {
+            String name = dest.toString();
+            if (name.endsWith(".rtf")) { 
+                name = name.substring(0, name.length()-4);
+            }
+            dest = new File(name + ".txt");
+        }
+        
         OutputStream fout = new FileOutputStream(dest);
         try {
-            extractMessageBody(fout);
+            fout.write(body.getData());
         } finally {
             fout.close();
         }
     }
     
+    protected MAPIAttribute getBodyAttribute() {
+        MAPIAttribute body = message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
+        if (body != null) return body;
+        
+        // See bug #59786 - we'd really like a test file to confirm if this
+        //  is the right properties + if this is truely general or not!
+        MAPIProperty uncompressedBody = 
+                MAPIProperty.createCustom(0x3fd9, Types.ASCII_STRING, "Uncompressed Body");
+        // Return this uncompressed one, or null if that isn't their either
+        return message.getMessageMAPIAttribute(uncompressedBody);
+    }
+    
     /**
-     * Extracts the RTF message body to the supplied stream
+     * Extracts the RTF message body to the supplied stream. If there is no
+     *  RTF message body, nothing will be written to the stream, but no
+     *  errors or exceptions will be raised.
      */
     public void extractMessageBody(OutputStream out) throws IOException {
         MAPIRtfAttribute body = (MAPIRtfAttribute)
index dc1e87f23341f1263b467f8e4f99dd837a17d2b6..ae473983f0da23a44d6878d1778829b66d1b5562 100644 (file)
@@ -40,7 +40,7 @@ import org.apache.poi.hsmf.datatypes.Types.MAPIType;
  *  by friendly name, ID and MAPI Property Name.
  * 
  * These are taken from the following MSDN resources:
- *  http://msdn.microsoft.com/en-us/library/microsoft.exchange.data.contenttypes.tnef.tnefpropertyid%28v=EXCHG.140%29.aspx
+ *  https://msdn.microsoft.com/en-us/library/microsoft.exchange.data.contenttypes.tnef.tnefpropertyid(v=exchg.150).aspx
  *  http://msdn.microsoft.com/en-us/library/ms526356%28v=exchg.10%29.aspx
  */
 public class MAPIProperty {