Author: nick
Date: Fri Jan  8 14:26:27 2010
New Revision: 897213

URL: http://svn.apache.org/viewvc?rev=897213&view=rev
Log:
More work on the recipient related chunks, including a helper method to do 
best-effort finding of the recipients email address

Added:
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java
Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java
    
poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/RecipientChunks.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java
    
poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java?rev=897213&r1=897212&r2=897213&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/MAPIMessage.java Fri Jan  
8 14:26:27 2010
@@ -172,6 +172,23 @@
        public String getDisplayBCC() throws ChunkNotFoundException {
                return getStringFromChunk(mainChunks.displayBCCChunk);
        }
+       
+       
+       /**
+        * Returns the recipient's email address, checking all the
+        *  likely chunks in search of it.
+        */
+       public String getRecipientEmailAddress() throws ChunkNotFoundException {
+          if(recipientChunks == null) {
+             throw new ChunkNotFoundException("No recipients section present");
+          }
+          String email = recipientChunks.getRecipientEmailAddress();
+          if(email != null) {
+             return email;
+          } else {
+             throw new ChunkNotFoundException();
+          }
+       }
 
 
        /**

Modified: 
poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java?rev=897213&r1=897212&r2=897213&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java 
(original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/ByteChunk.java 
Fri Jan  8 14:26:27 2010
@@ -61,4 +61,16 @@
    public void setValue(byte[] value) {
       this.value = value;
    }
+   
+   /**
+    * Returns the data, formatted as a string assuming it
+    *  was a non-unicode string.
+    * If your data isn't in fact stored as basically
+    *  ASCII, don't expect this to return much of any
+    *  sense.... 
+    * @return
+    */
+   public String getAs7bitString() {
+      return StringChunk.parseAs7BitData(value);
+   }
 }

Modified: 
poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/RecipientChunks.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/RecipientChunks.java?rev=897213&r1=897212&r2=897213&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/RecipientChunks.java 
(original)
+++ 
poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/RecipientChunks.java 
Fri Jan  8 14:26:27 2010
@@ -28,15 +28,62 @@
 public final class RecipientChunks implements ChunkGroup {
    public static final String PREFIX = "__recip_version1.0_#";
    
+   public static final int RECIPIENT_NAME   = 0x3001;
+   public static final int DELIVERY_TYPE    = 0x3002;
    public static final int RECIPIENT_SEARCH = 0x300B;
    public static final int RECIPIENT_EMAIL  = 0x39FE;
    
    /** TODO */
-   public ByteChunk recipientSearchChunk; 
-   /** TODO */
+   public ByteChunk recipientSearchChunk;
+   /**
+    * The "name", which could be their name if an
+    *  internal person, or their email address
+    *  if an external person
+    */
+   public StringChunk recipientNameChunk;
+   /** 
+    * The email address of the recipient, but
+    *  isn't always present...
+    */
    public StringChunk recipientEmailChunk;
+   /**
+    * Normally EX or SMTP. Will generally affect
+    *  where the email address ends up.
+    */
+   public StringChunk deliveryTypeChunk;
    
    
+   /**
+    * Tries to find their email address, in
+    *  whichever chunk holds it given the
+    *  delivery type.
+    */
+   public String getRecipientEmailAddress() {
+      if(recipientEmailChunk != null) {
+         return recipientEmailChunk.getValue();
+      }
+      // Probably in the name field
+      if(recipientNameChunk != null) {
+         String name = recipientNameChunk.getValue();
+         if(name.indexOf('@') > -1) {
+            // Strip leading and trailing quotes if needed
+            if(name.startsWith("'") && name.endsWith("'")) {
+               return name.substring(1, name.length()-1);
+            }
+            return name;
+         }
+      }
+      // Check the search chunk
+      if(recipientSearchChunk != null) {
+         String search = recipientSearchChunk.getAs7bitString();
+         if(search.indexOf("SMTP:") != -1) {
+            return search.substring(search.indexOf("SMTP:") + 5);
+         }
+      }
+      // Can't find it
+      return null;
+   }
+   
    /** Holds all the chunks that were found. */
    private List<Chunk> allChunks = new ArrayList<Chunk>();
 
@@ -56,9 +103,15 @@
          // TODO - parse
          recipientSearchChunk = (ByteChunk)chunk;
          break;
+      case RECIPIENT_NAME:
+         recipientNameChunk = (StringChunk)chunk;
+         break;
       case RECIPIENT_EMAIL:
          recipientEmailChunk = (StringChunk)chunk;
          break;
+      case DELIVERY_TYPE:
+         deliveryTypeChunk = (StringChunk)chunk;
+         break;
       }
 
       // And add to the main list

Modified: 
poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java?rev=897213&r1=897212&r2=897213&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java 
(original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hsmf/datatypes/StringChunk.java 
Fri Jan  8 14:26:27 2010
@@ -54,11 +54,7 @@
       
           switch(type) {
           case Types.ASCII_STRING:
-         try {
-            tmpValue = new String(data, "CP1252");
-         } catch (UnsupportedEncodingException e) {
-            throw new RuntimeException("Core encoding not found, JVM broken?", 
e);
-         }
+             tmpValue = parseAs7BitData(data);
          break;
           case Types.UNICODE_STRING:
              tmpValue = StringUtil.getFromUnicodeLE(data);
@@ -92,11 +88,23 @@
       
       out.write(data);
        }
-
+       
    public String getValue() {
       return this.value;
    }
        public String toString() {
                return this.value;
        }
+
+   /**
+    * Parses as non-unicode, supposedly 7 bit CP1252 data
+    *  and returns the string that that yields.
+    */
+   protected static String parseAs7BitData(byte[] data) {
+      try {
+         return new String(data, "CP1252");
+      } catch (UnsupportedEncodingException e) {
+         throw new RuntimeException("Core encoding not found, JVM broken?", e);
+      }
+   }
 }

Modified: 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java?rev=897213&r1=897212&r2=897213&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java 
(original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/AllHSMFTests.java 
Fri Jan  8 14:26:27 2010
@@ -24,19 +24,19 @@
 import org.apache.poi.hsmf.parsers.*;
 
 public final class AllHSMFTests {
+   public static Test suite() {
+      TestSuite suite = new TestSuite(AllHSMFTests.class.getName());
+      suite.addTestSuite(TestBasics.class);
+      suite.addTestSuite(TestBlankFileRead.class);
+      suite.addTestSuite(TestSimpleFileRead.class);
+      suite.addTestSuite(TestOutlook30FileRead.class);
+      suite.addTestSuite(TestFileWithAttachmentsRead.class);
 
-       public static Test suite() {
-               TestSuite suite = new TestSuite(AllHSMFTests.class.getName());
-               suite.addTestSuite(TestBlankFileRead.class);
-               suite.addTestSuite(TestSimpleFileRead.class);
-               suite.addTestSuite(TestOutlook30FileRead.class);
-               suite.addTestSuite(TestFileWithAttachmentsRead.class);
-               
       suite.addTestSuite(TestChunkData.class);
       suite.addTestSuite(TestTypes.class);
 
       suite.addTestSuite(TestPOIFSChunkParser.class);
-      
-               return suite;
-       }
+
+      return suite;
+   }
 }

Added: poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java?rev=897213&view=auto
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java 
(added)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/TestBasics.java Fri 
Jan  8 14:26:27 2010
@@ -0,0 +1,77 @@
+/* ====================================================================
+   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.hsmf;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.POIDataSamples;
+
+/**
+ * Tests to verify that we can perform basic opperations on 
+ *  a range of files
+ */
+public final class TestBasics extends TestCase {
+   private MAPIMessage simple;
+   private MAPIMessage quick;
+   private MAPIMessage outlook30;
+   private MAPIMessage attachments;
+
+       /**
+        * Initialize this test, load up the blank.msg mapi message.
+        * @throws Exception
+        */
+       public TestBasics() throws IOException {
+        POIDataSamples samples = POIDataSamples.getHSMFInstance();
+               simple = new 
MAPIMessage(samples.openResourceAsStream("simple_test_msg.msg"));
+      quick  = new MAPIMessage(samples.openResourceAsStream("quick.msg"));
+      outlook30  = new 
MAPIMessage(samples.openResourceAsStream("outlook_30_msg.msg"));
+      attachments = new 
MAPIMessage(samples.openResourceAsStream("attachment_test_msg.msg"));
+       }
+       
+       /**
+        * Can we always get the recipient's email?
+        */
+       public void testRecipientEmail() throws Exception {
+      assertEquals("[email protected]", 
simple.getRecipientEmailAddress());
+      assertEquals("[email protected]", 
quick.getRecipientEmailAddress());
+      assertEquals("[email protected]", 
outlook30.getRecipientEmailAddress());
+      assertEquals("[email protected]", 
attachments.getRecipientEmailAddress());
+       }
+       
+       /**
+        * Test subject
+        */
+       public void testSubject() throws Exception {
+      assertEquals("test message", simple.getSubject());
+      assertEquals("Test the content transformer", quick.getSubject());
+      assertEquals("IN-SPIRE servers going down for a bit, back up around 
8am", outlook30.getSubject());
+      assertEquals("test pi\u00e8ce jointe 1", attachments.getSubject());
+       }
+       
+       /**
+        * Test attachments
+        */
+       public void testAttachments() throws Exception {
+      assertEquals(0, simple.getAttachmentFiles().length);
+      assertEquals(0, quick.getAttachmentFiles().length);
+      assertEquals(0, outlook30.getAttachmentFiles().length);
+      assertEquals(2, attachments.getAttachmentFiles().length);
+       }
+}

Modified: 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java?rev=897213&r1=897212&r2=897213&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java
 (original)
+++ 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hsmf/parsers/TestPOIFSChunkParser.java
 Fri Jan  8 14:26:27 2010
@@ -105,6 +105,18 @@
       assertNotNull(msg.getRecipientDetailsChunks());
       
       assertEquals("[email protected]", 
msg.getRecipientDetailsChunks().recipientEmailChunk.getValue());
+      
+      
+      // Try both SMTP and EX files for recipient
+      assertEquals("EX", 
msg.getRecipientDetailsChunks().deliveryTypeChunk.getValue());
+      assertEquals("[email protected]", 
msg.getRecipientDetailsChunks().recipientEmailChunk.getValue());
+      
+      msg = new MAPIMessage(new POIFSFileSystem(
+            new FileInputStream(samples.getFile("simple_test_msg.msg"))
+      ));
+      assertEquals("SMTP", 
msg.getRecipientDetailsChunks().deliveryTypeChunk.getValue());
+      assertEquals(null, msg.getRecipientDetailsChunks().recipientEmailChunk);
+      assertEquals("[email protected]", 
msg.getRecipientDetailsChunks().recipientNameChunk.getValue());
    }
    
    public void testFindsNameId() throws IOException {



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to