You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2016/07/03 23:13:37 UTC

svn commit: r1751180 - in /poi/trunk/src/scratchpad: src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java testcases/org/apache/poi/hmef/extractor/ testcases/org/apache/poi/hmef/extractor/TestHMEFContentsExtractor.java

Author: onealj
Date: Sun Jul  3 23:13:37 2016
New Revision: 1751180

URL: http://svn.apache.org/viewvc?rev=1751180&view=rev
Log:
bug 59786: fix NPE from winmail.dat files if message body is null

Added:
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hmef/extractor/
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hmef/extractor/TestHMEFContentsExtractor.java   (with props)
Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java   (contents, props changed)

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java?rev=1751180&r1=1751179&r2=1751180&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java Sun Jul  3 23:13:37 2016
@@ -1,18 +1,18 @@
 /* ====================================================================
-   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.
+    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.poi.hmef.extractor;
@@ -34,8 +34,11 @@ import org.apache.poi.hsmf.datatypes.MAP
  *  from a HMEF/TNEF/winmail.dat file
  */
 public final class HMEFContentsExtractor {
-    public static void main(String[] args) throws Exception {
-        if (args.length < 2) {
+    /**
+     * Usage: HMEFContentsExtractor &lt;filename&gt; &lt;output dir&gt;
+     */
+    public static void main(String[] args) throws IOException {
+        if(args.length < 2) {
             System.err.println("Use:");
             System.err.println("  HMEFContentsExtractor <filename> <output dir>");
             System.err.println("");
@@ -45,11 +48,14 @@ public final class HMEFContentsExtractor
             System.exit(2);
         }
         
-        HMEFContentsExtractor ext = new HMEFContentsExtractor(new File(args[0]));
+        final String filename = args[0];
+        final String outputDir = args[1];
         
-        File dir = new File(args[1]);
+        HMEFContentsExtractor ext = new HMEFContentsExtractor(new File(filename));
+        
+        File dir = new File(outputDir);
         File rtf = new File(dir, "message.rtf");
-        if (! dir.exists()) {
+        if(! dir.exists()) {
             throw new FileNotFoundException("Output directory " + dir.getName() + " not found");
         }
         
@@ -59,7 +65,7 @@ public final class HMEFContentsExtractor
         System.out.println("Extraction completed");
     }
     
-    private HMEFMessage message;
+    private final HMEFMessage message;
     public HMEFContentsExtractor(File filename) throws IOException {
         this(new HMEFMessage(new FileInputStream(filename)));
     }
@@ -73,14 +79,23 @@ public final class HMEFContentsExtractor
     public void extractMessageBody(File dest) throws IOException {
         OutputStream fout = new FileOutputStream(dest);
         try {
-            MAPIRtfAttribute body = (MAPIRtfAttribute)
-                    message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
-            fout.write(body.getData());
+            extractMessageBody(fout);
         } finally {
             fout.close();
         }
     }
     
+    /**
+     * Extracts the RTF message body to the supplied stream
+     */
+    public void extractMessageBody(OutputStream out) throws IOException {
+        MAPIRtfAttribute body = (MAPIRtfAttribute)
+                message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
+        if (body != null) {
+            out.write(body.getData());
+        }
+    }
+    
     /**
      * Extracts all the message attachments to the supplied directory
      */

Propchange: poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: poi/trunk/src/scratchpad/testcases/org/apache/poi/hmef/extractor/TestHMEFContentsExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hmef/extractor/TestHMEFContentsExtractor.java?rev=1751180&view=auto
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hmef/extractor/TestHMEFContentsExtractor.java (added)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hmef/extractor/TestHMEFContentsExtractor.java Sun Jul  3 23:13:37 2016
@@ -0,0 +1,78 @@
+/* ====================================================================
+   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.poi.hmef.extractor;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertArrayEquals;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.util.TempFile;
+import org.junit.Test;
+
+public class TestHMEFContentsExtractor {
+    @Test
+    public void TestMain() throws IOException {
+        POIDataSamples samples = POIDataSamples.getHMEFInstance();
+        File message = samples.getFile("quick-winmail.dat");
+        File outputDirectory = TempFile.createTempDirectory("quick-winmail-main");
+        String[] args = new String[] { message.getAbsolutePath(), outputDirectory.getAbsolutePath() };
+        HMEFContentsExtractor.main(args);
+        
+        String[] contents = new String[] {
+                "message.rtf", // from extractMessageBody
+                "quick.txt", "quick.pdf", "quick.xml", "quick.doc", "quick.html" // from extractAttachments
+        };
+        
+        for (String filename : contents) {
+            File f = new File(outputDirectory, filename);
+            assertTrue(f + " does not exist", f.exists());
+        }
+
+        outputDirectory.delete();
+    }
+    
+    @Test
+    public void TestExtractMessageBody_OutputStream() throws IOException {
+        POIDataSamples samples = POIDataSamples.getHMEFInstance();
+        File winmailTNEFFile = samples.getFile("quick-winmail.dat");
+        HMEFContentsExtractor extractor = new HMEFContentsExtractor(winmailTNEFFile);
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        extractor.extractMessageBody(out);
+        assertTrue(out.size() > 0);
+        byte[] expectedMagic = new byte[] { '{', '\\', 'r', 't', 'f' };
+        byte[] magic = Arrays.copyOf(out.toByteArray(), 5);
+        assertArrayEquals("RTF magic number", expectedMagic, magic);
+        out.close();
+    }
+    
+    @Test
+    public void TestExtractMessageBody_File() throws IOException {
+        POIDataSamples samples = POIDataSamples.getHMEFInstance();
+        File winmailTNEFFile = samples.getFile("quick-winmail.dat");
+        HMEFContentsExtractor extractor = new HMEFContentsExtractor(winmailTNEFFile);
+        File rtf = TempFile.createTempFile("quick-winmail-message-body", ".rtf");
+        assertTrue(rtf.delete());
+        extractor.extractMessageBody(rtf);
+        assertTrue("RTF message body is empty", rtf.length() > 0);
+    }
+}

Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hmef/extractor/TestHMEFContentsExtractor.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org