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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.AttributeID;
  21. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  22. import org.apache.poi.util.LittleEndian;
  23. /**
  24. * HMEF - Implementation of the Microsoft TNEF message
  25. * encoding format (aka winmail.dat)
  26. * See:
  27. * http://support.microsoft.com/kb/241538
  28. * http://en.wikipedia.org/wiki/Transport_Neutral_Encapsulation_Format
  29. * http://search.cpan.org/dist/Convert-TNEF/
  30. */
  31. public final class HMEFMessage {
  32. public static final long HEADER_SIGNATURE = 0x223e9f78;
  33. private int fileId;
  34. private List<Attribute> messageAttributes = new ArrayList<Attribute>();
  35. private List<MAPIAttribute> mapiAttributes = new ArrayList<MAPIAttribute>();
  36. private List<Attachment> attachments = new ArrayList<Attachment>();
  37. public HMEFMessage(InputStream inp) throws IOException {
  38. // Check the signature matches
  39. long sig = LittleEndian.readInt(inp);
  40. if(sig != HEADER_SIGNATURE) {
  41. throw new IllegalArgumentException(
  42. "TNEF signature not detected in file, " +
  43. "expected " + HEADER_SIGNATURE + " but got " + sig
  44. );
  45. }
  46. // Read the File ID
  47. fileId = LittleEndian.readUShort(inp);
  48. // Now begin processing the contents
  49. process(inp, 0);
  50. // Finally expand out the MAPI Attributes
  51. for(Attribute attr : messageAttributes) {
  52. if(attr.getId() == Attribute.ID_MAPIPROPERTIES) {
  53. mapiAttributes.addAll(
  54. MAPIAttribute.create(attr)
  55. );
  56. }
  57. }
  58. for(Attachment attachment : attachments) {
  59. for(Attribute attr : attachment.getAttributes()) {
  60. if(attr.getId() == Attribute.ID_MAPIPROPERTIES) {
  61. attachment.getMAPIAttributes().addAll(
  62. MAPIAttribute.create(attr)
  63. );
  64. }
  65. }
  66. }
  67. }
  68. private void process(InputStream inp, int lastLevel) throws IOException {
  69. // Fetch the level
  70. int level = inp.read();
  71. if(level == Attribute.LEVEL_END_OF_FILE) {
  72. return;
  73. }
  74. // Build the attribute
  75. Attribute attr = new Attribute(inp);
  76. // Decide what to attach it to, based on the levels and IDs
  77. if(level == Attribute.LEVEL_MESSAGE) {
  78. messageAttributes.add(attr);
  79. } else if(level == Attribute.LEVEL_ATTACHMENT) {
  80. // Previous attachment or a new one?
  81. if(attachments.size() == 0 || attr.getId() == Attribute.ID_ATTACHRENDERDATA) {
  82. attachments.add(new Attachment());
  83. }
  84. // Save the attribute for it
  85. attachments.get(attachments.size()-1).addAttribute(attr);
  86. } else {
  87. throw new IllegalStateException("Unhandled level " + level);
  88. }
  89. // Handle the next one down
  90. process(inp, level);
  91. }
  92. /**
  93. * Returns all HMEF/TNEF attributes of the message.
  94. * Note - In a typical message, most of the interesting properties
  95. * are stored as {@link MAPIAttribute}s - see {@link #getMessageMAPIAttributes()}
  96. */
  97. public List<Attribute> getMessageAttributes() {
  98. return messageAttributes;
  99. }
  100. /**
  101. * Returns all MAPI attributes of the message.
  102. * Note - A small number of HMEF/TNEF specific attributes normally
  103. * apply to most messages, see {@link #getMessageAttributes()}
  104. */
  105. public List<MAPIAttribute> getMessageMAPIAttributes() {
  106. return mapiAttributes;
  107. }
  108. /**
  109. * Returns all the Attachments of the message.
  110. */
  111. public List<Attachment> getAttachments() {
  112. return attachments;
  113. }
  114. /**
  115. * Return the message attribute with the given ID,
  116. * or null if there isn't one.
  117. */
  118. public Attribute getMessageAttribute(AttributeID id) {
  119. for(Attribute attr : messageAttributes) {
  120. if(attr.getId() == id) {
  121. return attr;
  122. }
  123. }
  124. return null;
  125. }
  126. /**
  127. * Return the message MAPI Attribute with the given ID,
  128. * or null if there isn't one.
  129. */
  130. public MAPIAttribute getMessageMAPIAttribute(MAPIProperty id) {
  131. for(MAPIAttribute attr : mapiAttributes) {
  132. if(attr.getProperty() == id) {
  133. return attr;
  134. }
  135. }
  136. return null;
  137. }
  138. }