/android/titanium/thirdparty/org/apache/james/mime4j/descriptor/MaximalBodyDescriptor.java

https://github.com/mkautzmann/titanium_mobile · Java · 399 lines · 271 code · 43 blank · 85 comment · 45 complexity · 340280e1ad91ae0f18d49585737e4115 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.james.mime4j.descriptor;
  20. import java.io.StringReader;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.james.mime4j.MimeException;
  25. import org.apache.james.mime4j.field.datetime.DateTime;
  26. import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
  27. import org.apache.james.mime4j.field.datetime.parser.ParseException;
  28. import org.apache.james.mime4j.field.language.ContentLanguageParser;
  29. import org.apache.james.mime4j.field.mimeversion.MimeVersionParser;
  30. import org.apache.james.mime4j.field.structured.StructuredFieldParser;
  31. import org.apache.james.mime4j.util.MimeUtil;
  32. /**
  33. * Parses and stores values for standard MIME header values.
  34. *
  35. */
  36. public class MaximalBodyDescriptor extends DefaultBodyDescriptor implements RFC2045MimeDescriptor,
  37. RFC2183ContentDispositionDescriptor, RFC3066ContentLanguageDescriptor, RFC2557ContentLocationDescriptor, RFC1864ContentMD5Descriptor {
  38. private static final int DEFAULT_MINOR_VERSION = 0;
  39. private static final int DEFAULT_MAJOR_VERSION = 1;
  40. private boolean isMimeVersionSet;
  41. private int mimeMinorVersion;
  42. private int mimeMajorVersion;
  43. private MimeException mimeVersionException;
  44. private String contentId;
  45. private boolean isContentIdSet;
  46. private String contentDescription;
  47. private boolean isContentDescriptionSet;
  48. private String contentDispositionType;
  49. private Map contentDispositionParameters;
  50. private DateTime contentDispositionModificationDate;
  51. private MimeException contentDispositionModificationDateParseException;
  52. private DateTime contentDispositionCreationDate;
  53. private MimeException contentDispositionCreationDateParseException;
  54. private DateTime contentDispositionReadDate;
  55. private MimeException contentDispositionReadDateParseException;
  56. private long contentDispositionSize;
  57. private MimeException contentDispositionSizeParseException;
  58. private boolean isContentDispositionSet;
  59. private List contentLanguage;
  60. private MimeException contentLanguageParseException;
  61. private boolean isContentLanguageSet;
  62. private MimeException contentLocationParseException;
  63. private String contentLocation;
  64. private boolean isContentLocationSet;
  65. private String contentMD5Raw;
  66. private boolean isContentMD5Set;
  67. protected MaximalBodyDescriptor() {
  68. this(null);
  69. }
  70. public MaximalBodyDescriptor(BodyDescriptor parent) {
  71. super(parent);
  72. isMimeVersionSet = false;
  73. mimeMajorVersion = DEFAULT_MAJOR_VERSION;
  74. mimeMinorVersion = DEFAULT_MINOR_VERSION;
  75. this.contentId = null;
  76. this.isContentIdSet = false;
  77. this.contentDescription = null;
  78. this.isContentDescriptionSet = false;
  79. this.contentDispositionType = null;
  80. this.contentDispositionParameters = Collections.EMPTY_MAP;
  81. this.contentDispositionModificationDate = null;
  82. this.contentDispositionModificationDateParseException = null;
  83. this.contentDispositionCreationDate = null;
  84. this.contentDispositionCreationDateParseException = null;
  85. this.contentDispositionReadDate = null;
  86. this.contentDispositionReadDateParseException = null;
  87. this.contentDispositionSize = -1;
  88. this.contentDispositionSizeParseException = null;
  89. this.isContentDispositionSet = false;
  90. this.contentLanguage = null;
  91. this.contentLanguageParseException = null;
  92. this.isContentIdSet = false;
  93. this.contentLocation = null;
  94. this.contentLocationParseException = null;
  95. this.isContentLocationSet = false;
  96. this.contentMD5Raw = null;
  97. this.isContentMD5Set = false;
  98. }
  99. public void addField(String name, String value) {
  100. name = name.trim().toLowerCase();
  101. if (MimeUtil.MIME_HEADER_MIME_VERSION.equals(name) && !isMimeVersionSet) {
  102. parseMimeVersion(value);
  103. } else if (MimeUtil.MIME_HEADER_CONTENT_ID.equals(name) && !isContentIdSet) {
  104. parseContentId(value);
  105. } else if (MimeUtil.MIME_HEADER_CONTENT_DESCRIPTION.equals(name) && !isContentDescriptionSet) {
  106. parseContentDescription(value);
  107. } else if (MimeUtil.MIME_HEADER_CONTENT_DISPOSITION.equals(name) && !isContentDispositionSet) {
  108. parseContentDisposition(value);
  109. } else if (MimeUtil.MIME_HEADER_LANGAUGE.equals(name) && !isContentLanguageSet) {
  110. parseLanguage(value);
  111. } else if (MimeUtil.MIME_HEADER_LOCATION.equals(name) && !isContentLocationSet) {
  112. parseLocation(value);
  113. } else if (MimeUtil.MIME_HEADER_MD5.equals(name) && !isContentMD5Set) {
  114. parseMD5(value);
  115. } else {
  116. super.addField(name, value);
  117. }
  118. }
  119. private void parseMD5(String value) {
  120. isContentMD5Set = true;
  121. if (value != null) {
  122. contentMD5Raw = value.trim();
  123. }
  124. }
  125. private void parseLocation(final String value) {
  126. isContentLocationSet = true;
  127. if (value != null) {
  128. final StringReader stringReader = new StringReader(value);
  129. final StructuredFieldParser parser = new StructuredFieldParser(stringReader);
  130. parser.setFoldingPreserved(false);
  131. try {
  132. contentLocation = parser.parse();
  133. } catch (MimeException e) {
  134. contentLocationParseException = e;
  135. }
  136. }
  137. }
  138. private void parseLanguage(final String value) {
  139. isContentLanguageSet = true;
  140. if (value != null) {
  141. try {
  142. final ContentLanguageParser parser = new ContentLanguageParser(new StringReader(value));
  143. contentLanguage = parser.parse();
  144. } catch (MimeException e) {
  145. contentLanguageParseException = e;
  146. }
  147. }
  148. }
  149. private void parseContentDisposition(final String value) {
  150. isContentDispositionSet = true;
  151. contentDispositionParameters = MimeUtil.getHeaderParams(value);
  152. contentDispositionType = (String) contentDispositionParameters.get("");
  153. final String contentDispositionModificationDate
  154. = (String) contentDispositionParameters.get(MimeUtil.PARAM_MODIFICATION_DATE);
  155. if (contentDispositionModificationDate != null) {
  156. try {
  157. this.contentDispositionModificationDate = parseDate(contentDispositionModificationDate);
  158. } catch (ParseException e) {
  159. this.contentDispositionModificationDateParseException = e;
  160. }
  161. }
  162. final String contentDispositionCreationDate
  163. = (String) contentDispositionParameters.get(MimeUtil.PARAM_CREATION_DATE);
  164. if (contentDispositionCreationDate != null) {
  165. try {
  166. this.contentDispositionCreationDate = parseDate(contentDispositionCreationDate);
  167. } catch (ParseException e) {
  168. this.contentDispositionCreationDateParseException = e;
  169. }
  170. }
  171. final String contentDispositionReadDate
  172. = (String) contentDispositionParameters.get(MimeUtil.PARAM_READ_DATE);
  173. if (contentDispositionReadDate != null) {
  174. try {
  175. this.contentDispositionReadDate = parseDate(contentDispositionReadDate);
  176. } catch (ParseException e) {
  177. this.contentDispositionReadDateParseException = e;
  178. }
  179. }
  180. final String size = (String) contentDispositionParameters.get(MimeUtil.PARAM_SIZE);
  181. if (size != null) {
  182. try {
  183. contentDispositionSize = Long.parseLong(size);
  184. } catch (NumberFormatException e) {
  185. this.contentDispositionSizeParseException = (MimeException) new MimeException(e.getMessage(), e).fillInStackTrace();
  186. }
  187. }
  188. contentDispositionParameters.remove("");
  189. }
  190. private DateTime parseDate(final String date) throws ParseException {
  191. final StringReader stringReader = new StringReader(date);
  192. final DateTimeParser parser = new DateTimeParser(stringReader);
  193. DateTime result = parser.date_time();
  194. return result;
  195. }
  196. private void parseContentDescription(String value) {
  197. if (value == null) {
  198. contentDescription = "";
  199. } else {
  200. contentDescription = value.trim();
  201. }
  202. isContentDescriptionSet = true;
  203. }
  204. private void parseContentId(final String value) {
  205. if (value == null) {
  206. contentId = "";
  207. } else {
  208. contentId = value.trim();
  209. }
  210. isContentIdSet = true;
  211. }
  212. private void parseMimeVersion(String value) {
  213. final StringReader reader = new StringReader(value);
  214. final MimeVersionParser parser = new MimeVersionParser(reader);
  215. try {
  216. parser.parse();
  217. final int major = parser.getMajorVersion();
  218. if (major != MimeVersionParser.INITIAL_VERSION_VALUE) {
  219. mimeMajorVersion = major;
  220. }
  221. final int minor = parser.getMinorVersion();
  222. if (minor != MimeVersionParser.INITIAL_VERSION_VALUE) {
  223. mimeMinorVersion = minor;
  224. }
  225. } catch (MimeException e) {
  226. this.mimeVersionException = e;
  227. }
  228. isMimeVersionSet = true;
  229. }
  230. /**
  231. * @see org.apache.james.mime4j.descriptor.RFC2045MimeDescriptor#getMimeMajorVersion()
  232. */
  233. public int getMimeMajorVersion() {
  234. return mimeMajorVersion;
  235. }
  236. /**
  237. * @see org.apache.james.mime4j.descriptor.RFC2045MimeDescriptor#getMimeMinorVersion()
  238. */
  239. public int getMimeMinorVersion() {
  240. return mimeMinorVersion;
  241. }
  242. /**
  243. * @see org.apache.james.mime4j.descriptor.RFC2045MimeDescriptor#getMimeVersionParseException()
  244. */
  245. public MimeException getMimeVersionParseException() {
  246. return mimeVersionException;
  247. }
  248. /**
  249. * @see org.apache.james.mime4j.descriptor.RFC2045MimeDescriptor#getContentDescription()
  250. */
  251. public String getContentDescription() {
  252. return contentDescription;
  253. }
  254. /**
  255. * @see org.apache.james.mime4j.descriptor.RFC2045MimeDescriptor#getContentId()
  256. */
  257. public String getContentId() {
  258. return contentId;
  259. }
  260. /**
  261. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionType()
  262. */
  263. public String getContentDispositionType() {
  264. return contentDispositionType;
  265. }
  266. /**
  267. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionParameters()
  268. */
  269. public Map getContentDispositionParameters() {
  270. return contentDispositionParameters;
  271. }
  272. /**
  273. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionFilename()
  274. */
  275. public String getContentDispositionFilename() {
  276. return (String) contentDispositionParameters.get(MimeUtil.PARAM_FILENAME);
  277. }
  278. /**
  279. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionModificationDate()
  280. */
  281. public DateTime getContentDispositionModificationDate() {
  282. return contentDispositionModificationDate;
  283. }
  284. /**
  285. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionModificationDateParseException()
  286. */
  287. public MimeException getContentDispositionModificationDateParseException() {
  288. return contentDispositionModificationDateParseException;
  289. }
  290. /**
  291. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionCreationDate()
  292. */
  293. public DateTime getContentDispositionCreationDate() {
  294. return contentDispositionCreationDate;
  295. }
  296. /**
  297. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionCreationDateParseException()
  298. */
  299. public MimeException getContentDispositionCreationDateParseException() {
  300. return contentDispositionCreationDateParseException;
  301. }
  302. /**
  303. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionReadDate()
  304. */
  305. public DateTime getContentDispositionReadDate() {
  306. return contentDispositionReadDate;
  307. }
  308. /**
  309. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionReadDateParseException()
  310. */
  311. public MimeException getContentDispositionReadDateParseException() {
  312. return contentDispositionReadDateParseException;
  313. }
  314. /**
  315. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionSize()
  316. */
  317. public long getContentDispositionSize() {
  318. return contentDispositionSize;
  319. }
  320. /**
  321. * @see org.apache.james.mime4j.descriptor.RFC2183ContentDispositionDescriptor#getContentDispositionSizeParseException()
  322. */
  323. public MimeException getContentDispositionSizeParseException() {
  324. return contentDispositionSizeParseException;
  325. }
  326. /**
  327. * @see org.apache.james.mime4j.descriptor.RFC3066ContentLanguageDescriptor#getContentLanguage()
  328. */
  329. public List getContentLanguage() {
  330. return contentLanguage;
  331. }
  332. /**
  333. * @see org.apache.james.mime4j.descriptor.RFC3066ContentLanguageDescriptor#getContentLanguageParseException()
  334. */
  335. public MimeException getContentLanguageParseException() {
  336. return contentLanguageParseException;
  337. }
  338. /**
  339. * @see org.apache.james.mime4j.descriptor.RFC2557ContentLocationDescriptor#getContentLocation()
  340. */
  341. public String getContentLocation() {
  342. return contentLocation;
  343. }
  344. /**
  345. * @see org.apache.james.mime4j.descriptor.RFC2557ContentLocationDescriptor#getContentLocationParseException()
  346. */
  347. public MimeException getContentLocationParseException() {
  348. return contentLocationParseException;
  349. }
  350. /**
  351. * @see org.apache.james.mime4j.descriptor.RFC1864ContentMD5Descriptor#getContentMD5Raw()
  352. */
  353. public String getContentMD5Raw() {
  354. return contentMD5Raw;
  355. }
  356. }