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.

HMEFMessage.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.hmef;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import org.apache.poi.hmef.attribute.MAPIAttribute;
  22. import org.apache.poi.hmef.attribute.MAPIStringAttribute;
  23. import org.apache.poi.hmef.attribute.TNEFAttribute;
  24. import org.apache.poi.hmef.attribute.TNEFMAPIAttribute;
  25. import org.apache.poi.hmef.attribute.TNEFProperty;
  26. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  27. import org.apache.poi.util.LittleEndian;
  28. /**
  29. * HMEF - Implementation of the Microsoft TNEF message
  30. * encoding format (aka winmail.dat)
  31. * See:
  32. * http://support.microsoft.com/kb/241538
  33. * http://en.wikipedia.org/wiki/Transport_Neutral_Encapsulation_Format
  34. * http://search.cpan.org/dist/Convert-TNEF/
  35. */
  36. public final class HMEFMessage {
  37. public static final int HEADER_SIGNATURE = 0x223e9f78;
  38. @SuppressWarnings("unused")
  39. private int fileId;
  40. private final List<TNEFAttribute> messageAttributes = new ArrayList<>();
  41. private final List<MAPIAttribute> mapiAttributes = new ArrayList<>();
  42. private final List<Attachment> attachments = new ArrayList<>();
  43. /**
  44. * @param inp input stream
  45. * @throws IOException
  46. * @throws RuntimeException a number of runtime exceptions can be thrown, especially if there are problems with the
  47. * input format
  48. */
  49. public HMEFMessage(InputStream inp) throws IOException {
  50. try {
  51. // Check the signature matches
  52. int sig = LittleEndian.readInt(inp);
  53. if (sig != HEADER_SIGNATURE) {
  54. throw new IllegalArgumentException(
  55. "TNEF signature not detected in file, " +
  56. "expected " + HEADER_SIGNATURE + " but got " + sig
  57. );
  58. }
  59. // Read the File ID
  60. fileId = LittleEndian.readUShort(inp);
  61. // Now begin processing the contents
  62. process(inp);
  63. } finally {
  64. inp.close();
  65. }
  66. }
  67. private void process(InputStream inp) throws IOException {
  68. int level;
  69. do {
  70. // Fetch the level
  71. level = inp.read();
  72. // Decide what to attach it to, based on the levels and IDs
  73. switch (level) {
  74. case TNEFProperty.LEVEL_MESSAGE:
  75. processMessage(inp);
  76. break;
  77. case TNEFProperty.LEVEL_ATTACHMENT:
  78. processAttachment(inp);
  79. break;
  80. // ignore trailing newline
  81. case '\r':
  82. case '\n':
  83. case TNEFProperty.LEVEL_END_OF_FILE:
  84. break;
  85. default:
  86. throw new IllegalStateException("Unhandled level " + level);
  87. }
  88. } while (level != TNEFProperty.LEVEL_END_OF_FILE);
  89. }
  90. void processMessage(InputStream inp) throws IOException {
  91. // Build the attribute
  92. TNEFAttribute attr = TNEFAttribute.create(inp);
  93. messageAttributes.add(attr);
  94. if (attr instanceof TNEFMAPIAttribute) {
  95. TNEFMAPIAttribute tnefMAPI = (TNEFMAPIAttribute) attr;
  96. mapiAttributes.addAll(tnefMAPI.getMAPIAttributes());
  97. }
  98. }
  99. void processAttachment(InputStream inp) throws IOException {
  100. // Build the attribute
  101. TNEFAttribute attr = TNEFAttribute.create(inp);
  102. // Previous attachment or a new one?
  103. if (attachments.isEmpty()
  104. || attr.getProperty() == TNEFProperty.ID_ATTACHRENDERDATA) {
  105. attachments.add(new Attachment());
  106. }
  107. // Save the attribute for it
  108. Attachment attach = attachments.get(attachments.size() - 1);
  109. attach.addAttribute(attr);
  110. }
  111. /**
  112. * Returns all HMEF/TNEF attributes of the message.
  113. * Note - In a typical message, most of the interesting properties
  114. * are stored as {@link MAPIAttribute}s - see {@link #getMessageMAPIAttributes()}
  115. */
  116. public List<TNEFAttribute> getMessageAttributes() {
  117. return Collections.unmodifiableList(messageAttributes);
  118. }
  119. /**
  120. * Returns all MAPI attributes of the message.
  121. * Note - A small number of HMEF/TNEF specific attributes normally
  122. * apply to most messages, see {@link #getMessageAttributes()}
  123. */
  124. public List<MAPIAttribute> getMessageMAPIAttributes() {
  125. return Collections.unmodifiableList(mapiAttributes);
  126. }
  127. /**
  128. * Returns all the Attachments of the message.
  129. */
  130. public List<Attachment> getAttachments() {
  131. return Collections.unmodifiableList(attachments);
  132. }
  133. /**
  134. * Return the message attribute with the given ID,
  135. * or null if there isn't one.
  136. */
  137. public TNEFAttribute getMessageAttribute(TNEFProperty id) {
  138. for (TNEFAttribute attr : messageAttributes) {
  139. if (attr.getProperty() == id) {
  140. return attr;
  141. }
  142. }
  143. return null;
  144. }
  145. /**
  146. * Return the message MAPI Attribute with the given ID,
  147. * or null if there isn't one.
  148. */
  149. public MAPIAttribute getMessageMAPIAttribute(MAPIProperty id) {
  150. for (MAPIAttribute attr : mapiAttributes) {
  151. // Because of custom properties, match on ID not literal property object
  152. if (attr.getProperty().id == id.id) {
  153. return attr;
  154. }
  155. }
  156. return null;
  157. }
  158. /**
  159. * Return the string value of the mapi property, or null
  160. * if it isn't set
  161. */
  162. private String getString(MAPIProperty id) {
  163. return MAPIStringAttribute.getAsString( getMessageMAPIAttribute(id) );
  164. }
  165. /**
  166. * Returns the Message Subject, or null if the mapi property
  167. * for this isn't set
  168. */
  169. public String getSubject() {
  170. return getString(MAPIProperty.CONVERSATION_TOPIC);
  171. }
  172. /**
  173. * Returns the Message Body, as RTF, or null if the mapi property
  174. * for this isn't set
  175. */
  176. public String getBody() {
  177. return getString(MAPIProperty.RTF_COMPRESSED);
  178. }
  179. }