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.

AttachmentChunks.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.hsmf.datatypes;
  16. import static org.apache.poi.hsmf.datatypes.MAPIProperty.ATTACH_CONTENT_ID;
  17. import static org.apache.poi.hsmf.datatypes.MAPIProperty.ATTACH_DATA;
  18. import static org.apache.poi.hsmf.datatypes.MAPIProperty.ATTACH_EXTENSION;
  19. import static org.apache.poi.hsmf.datatypes.MAPIProperty.ATTACH_FILENAME;
  20. import static org.apache.poi.hsmf.datatypes.MAPIProperty.ATTACH_LONG_FILENAME;
  21. import static org.apache.poi.hsmf.datatypes.MAPIProperty.ATTACH_MIME_TAG;
  22. import static org.apache.poi.hsmf.datatypes.MAPIProperty.ATTACH_RENDERING;
  23. import java.io.IOException;
  24. import java.io.Serializable;
  25. import java.util.ArrayList;
  26. import java.util.Comparator;
  27. import java.util.List;
  28. import org.apache.poi.hsmf.MAPIMessage;
  29. import org.apache.poi.util.POILogFactory;
  30. import org.apache.poi.util.POILogger;
  31. /**
  32. * Collection of convenience chunks for standard parts of the MSG file
  33. * attachment.
  34. */
  35. public class AttachmentChunks implements ChunkGroup {
  36. private static final POILogger LOG = POILogFactory.getLogger(AttachmentChunks.class);
  37. public static final String PREFIX = "__attach_version1.0_#";
  38. private ByteChunk attachData;
  39. private StringChunk attachExtension;
  40. private StringChunk attachFileName;
  41. private StringChunk attachLongFileName;
  42. private StringChunk attachMimeTag;
  43. private DirectoryChunk attachmentDirectory;
  44. private StringChunk attachContentId;
  45. /**
  46. * This is in WMF Format. You'll probably want to pass it to Apache Batik to
  47. * turn it into a SVG that you can then display.
  48. */
  49. public ByteChunk attachRenderingWMF;
  50. /**
  51. * What the POIFS name of this attachment is.
  52. */
  53. private String poifsName;
  54. /** Holds all the chunks that were found. */
  55. private List<Chunk> allChunks = new ArrayList<>();
  56. public AttachmentChunks(String poifsName) {
  57. this.poifsName = poifsName;
  58. }
  59. /**
  60. * Is this Attachment an embedded MAPI message?
  61. */
  62. public boolean isEmbeddedMessage() {
  63. return (attachmentDirectory != null);
  64. }
  65. /**
  66. * Returns the embedded MAPI message, if the attachment is an embedded
  67. * message, or null otherwise
  68. */
  69. public MAPIMessage getEmbeddedMessage() throws IOException {
  70. if (attachmentDirectory != null) {
  71. return attachmentDirectory.getAsEmbededMessage();
  72. }
  73. return null;
  74. }
  75. /**
  76. * Returns the embedded object, if the attachment is an object based
  77. * embedding (image, document etc), or null if it's an embedded message
  78. */
  79. public byte[] getEmbeddedAttachmentObject() {
  80. if (attachData != null) {
  81. return attachData.getValue();
  82. }
  83. return null;
  84. }
  85. public Chunk[] getAll() {
  86. return allChunks.toArray(new Chunk[allChunks.size()]);
  87. }
  88. @Override
  89. public Chunk[] getChunks() {
  90. return getAll();
  91. }
  92. public String getPOIFSName() {
  93. return poifsName;
  94. }
  95. /**
  96. * @return the ATTACH_DATA chunk
  97. */
  98. public ByteChunk getAttachData() {
  99. return attachData;
  100. }
  101. /**
  102. * @return the attachment extension
  103. */
  104. public StringChunk getAttachExtension() {
  105. return attachExtension;
  106. }
  107. /**
  108. * @return the attachment (short) filename
  109. */
  110. public StringChunk getAttachFileName() {
  111. return attachFileName;
  112. }
  113. /**
  114. * @return the attachment (long) filename
  115. */
  116. public StringChunk getAttachLongFileName() {
  117. return attachLongFileName;
  118. }
  119. /**
  120. * @return the attachment mimetag
  121. */
  122. public StringChunk getAttachMimeTag() {
  123. return attachMimeTag;
  124. }
  125. /**
  126. * @return the attachment directory
  127. */
  128. public DirectoryChunk getAttachmentDirectory() {
  129. return attachmentDirectory;
  130. }
  131. /**
  132. * @return the attachment preview bytes
  133. */
  134. public ByteChunk getAttachRenderingWMF() {
  135. return attachRenderingWMF;
  136. }
  137. /**
  138. * @return the attachment content ID
  139. */
  140. public StringChunk getAttachContentId() {
  141. return attachContentId;
  142. }
  143. /**
  144. * Called by the parser whenever a chunk is found.
  145. */
  146. @Override
  147. public void record(Chunk chunk) {
  148. // TODO: add further members for other properties like:
  149. // - ATTACH_ADDITIONAL_INFO
  150. // - ATTACH_CONTENT_BASE
  151. // - ATTACH_CONTENT_LOCATION
  152. // - ATTACH_DISPOSITION
  153. // - ATTACH_ENCODING
  154. // - ATTACH_FLAGS
  155. // - ATTACH_LONG_PATHNAME
  156. // - ATTACH_SIZE
  157. final int chunkId = chunk.getChunkId();
  158. if (chunkId == ATTACH_DATA.id) {
  159. if (chunk instanceof ByteChunk) {
  160. attachData = (ByteChunk) chunk;
  161. } else if (chunk instanceof DirectoryChunk) {
  162. attachmentDirectory = (DirectoryChunk) chunk;
  163. } else {
  164. LOG.log(POILogger.ERROR, "Unexpected data chunk of type " + chunk.getEntryName());
  165. }
  166. } else if (chunkId == ATTACH_EXTENSION.id) {
  167. attachExtension = (StringChunk) chunk;
  168. } else if (chunkId == ATTACH_FILENAME.id) {
  169. attachFileName = (StringChunk) chunk;
  170. } else if (chunkId == ATTACH_LONG_FILENAME.id) {
  171. attachLongFileName = (StringChunk) chunk;
  172. } else if (chunkId == ATTACH_MIME_TAG.id) {
  173. attachMimeTag = (StringChunk) chunk;
  174. } else if (chunkId == ATTACH_RENDERING.id) {
  175. attachRenderingWMF = (ByteChunk) chunk;
  176. } else if (chunkId == ATTACH_CONTENT_ID.id) {
  177. attachContentId = (StringChunk) chunk;
  178. } else {
  179. LOG.log(POILogger.WARN, "Currently unsupported attachment chunk property will be ignored. " + chunk.getEntryName());
  180. }
  181. // And add to the main list
  182. allChunks.add(chunk);
  183. }
  184. /**
  185. * Used to flag that all the chunks of the attachment have now been located.
  186. */
  187. @Override
  188. public void chunksComplete() {
  189. // Currently, we don't need to do anything special once
  190. // all the chunks have been located
  191. }
  192. /**
  193. * Orders by the attachment number.
  194. */
  195. public static class AttachmentChunksSorter
  196. implements Comparator<AttachmentChunks>, Serializable {
  197. @Override
  198. public int compare(AttachmentChunks a, AttachmentChunks b) {
  199. return a.poifsName.compareTo(b.poifsName);
  200. }
  201. }
  202. }