You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSMFFileHandler.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.stress;
  16. import static org.junit.jupiter.api.Assertions.assertNotNull;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.InputStream;
  20. import org.apache.poi.hsmf.MAPIMessage;
  21. import org.apache.poi.hsmf.datatypes.AttachmentChunks;
  22. import org.apache.poi.hsmf.datatypes.DirectoryChunk;
  23. import org.junit.jupiter.api.Test;
  24. class HSMFFileHandler extends POIFSFileHandler {
  25. @Override
  26. public void handleFile(InputStream stream, String path) throws Exception {
  27. MAPIMessage mapi = new MAPIMessage(stream);
  28. assertNotNull(mapi.getAttachmentFiles());
  29. assertNotNull(mapi.getDisplayBCC());
  30. assertNotNull(mapi.getMessageDate());
  31. AttachmentChunks[] attachments = mapi.getAttachmentFiles();
  32. for(AttachmentChunks attachment : attachments) {
  33. DirectoryChunk chunkDirectory = attachment.getAttachmentDirectory();
  34. if(chunkDirectory != null) {
  35. MAPIMessage attachmentMSG = chunkDirectory.getAsEmbeddedMessage();
  36. assertNotNull(attachmentMSG);
  37. String body = attachmentMSG.getTextBody();
  38. assertNotNull(body);
  39. }
  40. }
  41. /* => Writing isn't yet supported...
  42. // write out the file
  43. File file = TempFile.createTempFile("StressTest", ".msg");
  44. writeToFile(mapi, file);
  45. MAPIMessage read = new MAPIMessage(file.getAbsolutePath());
  46. assertNotNull(read.getAttachmentFiles());
  47. assertNotNull(read.getDisplayBCC());
  48. assertNotNull(read.getMessageDate());
  49. */
  50. // writing is not yet supported... handlePOIDocument(mapi);
  51. mapi.close();
  52. }
  53. // private void writeToFile(MAPIMessage mapi, File file)
  54. // throws FileNotFoundException, IOException {
  55. // OutputStream stream = new FileOutputStream(file);
  56. // try {
  57. // mapi.write(stream);
  58. // } finally {
  59. // stream.close();
  60. // }
  61. // }
  62. // a test-case to test this locally without executing the full TestAllFiles
  63. @Override
  64. @Test
  65. void test() throws Exception {
  66. File file = new File("test-data/hsmf/logsat.com_signatures_valid.msg");
  67. try (InputStream stream = new FileInputStream(file)) {
  68. handleFile(stream, file.getPath());
  69. }
  70. handleExtracting(file);
  71. }
  72. }