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.

MessageSubmissionChunk.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.nio.charset.Charset;
  20. import java.util.Calendar;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import org.apache.poi.hsmf.datatypes.Types.MAPIType;
  24. import org.apache.poi.util.IOUtils;
  25. import org.apache.poi.util.LocaleUtil;
  26. import org.apache.poi.util.POILogFactory;
  27. import org.apache.poi.util.POILogger;
  28. /**
  29. * A Chunk that holds the details given back by the server at submission time.
  30. * This includes the date the message was given to the server, and an ID that's
  31. * used if you want to cancel a message or similar
  32. */
  33. public class MessageSubmissionChunk extends Chunk {
  34. private static final POILogger LOG = POILogFactory.getLogger(MessageSubmissionChunk.class);
  35. private String rawId;
  36. private Calendar date;
  37. private static final Pattern datePatern = Pattern
  38. .compile("(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)Z?");
  39. /**
  40. * Creates a Byte Chunk.
  41. */
  42. public MessageSubmissionChunk(String namePrefix, int chunkId,
  43. MAPIType type) {
  44. super(namePrefix, chunkId, type);
  45. }
  46. /**
  47. * Create a Byte Chunk, with the specified type.
  48. */
  49. public MessageSubmissionChunk(int chunkId, MAPIType type) {
  50. super(chunkId, type);
  51. }
  52. @Override
  53. public void readValue(InputStream value) throws IOException {
  54. // Stored in the file as us-ascii
  55. byte[] data = IOUtils.toByteArray(value);
  56. rawId = new String(data, Charset.forName("ASCII"));
  57. // Now process the date
  58. String[] parts = rawId.split(";");
  59. for (String part : parts) {
  60. if (part.startsWith("l=")) {
  61. // Format of this bit appears to be l=<id>-<time>-<number>
  62. // ID may contain hyphens.
  63. String dateS = null;
  64. final int numberPartBegin = part.lastIndexOf('-');
  65. if (numberPartBegin != -1) {
  66. final int datePartBegin = part.lastIndexOf('-',
  67. numberPartBegin - 1);
  68. if (datePartBegin != -1 &&
  69. // cannot extract date if only one hyphen is in the
  70. // string...
  71. numberPartBegin > datePartBegin) {
  72. dateS = part.substring(datePartBegin + 1,
  73. numberPartBegin);
  74. }
  75. }
  76. if (dateS != null) {
  77. // Should be yymmddhhmmssZ
  78. Matcher m = datePatern.matcher(dateS);
  79. if (m.matches()) {
  80. date = LocaleUtil.getLocaleCalendar();
  81. // work around issues with dates like 1989, which appear as "89" here
  82. int year = Integer.parseInt(m.group(1));
  83. date.set(Calendar.YEAR, year + (year > 80 ? 1900 : 2000));
  84. // Java is 0 based
  85. date.set(Calendar.MONTH, Integer.parseInt(m.group(2)) - 1);
  86. date.set(Calendar.DATE, Integer.parseInt(m.group(3)));
  87. date.set(Calendar.HOUR_OF_DAY,
  88. Integer.parseInt(m.group(4)));
  89. date.set(Calendar.MINUTE, Integer.parseInt(m.group(5)));
  90. date.set(Calendar.SECOND, Integer.parseInt(m.group(6)));
  91. date.clear(Calendar.MILLISECOND);
  92. } else {
  93. LOG.log(POILogger.WARN,
  94. "Warning - unable to make sense of date "
  95. + dateS);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. @Override
  102. public void writeValue(OutputStream out) throws IOException {
  103. byte[] data = rawId.getBytes(Charset.forName("ASCII"));
  104. out.write(data);
  105. }
  106. /**
  107. * @return the date that the server accepted the message, as found from the
  108. * message ID it generated.
  109. *
  110. */
  111. public Calendar getAcceptedAtTime() {
  112. return date;
  113. }
  114. /**
  115. * @return the full ID that the server generated when it accepted the
  116. * message.
  117. */
  118. public String getSubmissionId() {
  119. return rawId;
  120. }
  121. }