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 6.1KB

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