[6785] | 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 | |
---|
| 20 | package org.apache.james.mime4j.message; |
---|
| 21 | |
---|
| 22 | import java.util.Arrays; |
---|
| 23 | import java.util.Collection; |
---|
| 24 | import java.util.Collections; |
---|
| 25 | import java.util.Date; |
---|
| 26 | import java.util.TimeZone; |
---|
| 27 | |
---|
| 28 | import org.apache.james.mime4j.dom.Header; |
---|
| 29 | import org.apache.james.mime4j.dom.Message; |
---|
| 30 | import org.apache.james.mime4j.dom.address.Address; |
---|
| 31 | import org.apache.james.mime4j.dom.address.AddressList; |
---|
| 32 | import org.apache.james.mime4j.dom.address.Mailbox; |
---|
| 33 | import org.apache.james.mime4j.dom.address.MailboxList; |
---|
| 34 | import org.apache.james.mime4j.dom.field.AddressListField; |
---|
| 35 | import org.apache.james.mime4j.dom.field.DateTimeField; |
---|
| 36 | import org.apache.james.mime4j.dom.field.Field; |
---|
| 37 | import org.apache.james.mime4j.dom.field.FieldName; |
---|
| 38 | import org.apache.james.mime4j.dom.field.MailboxField; |
---|
| 39 | import org.apache.james.mime4j.dom.field.MailboxListField; |
---|
| 40 | import org.apache.james.mime4j.dom.field.UnstructuredField; |
---|
| 41 | |
---|
| 42 | public abstract class AbstractMessage extends AbstractEntity implements Message { |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * Returns the value of the <i>Message-ID</i> header field of this message |
---|
| 46 | * or <code>null</code> if it is not present. |
---|
| 47 | * |
---|
| 48 | * @return the identifier of this message. |
---|
| 49 | */ |
---|
| 50 | public String getMessageId() { |
---|
| 51 | Field field = obtainField(FieldName.MESSAGE_ID); |
---|
| 52 | if (field == null) |
---|
| 53 | return null; |
---|
| 54 | |
---|
| 55 | return field.getBody(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Creates and sets a new <i>Message-ID</i> header field for this message. |
---|
| 60 | * A <code>Header</code> is created if this message does not already have |
---|
| 61 | * one. |
---|
| 62 | * |
---|
| 63 | * @param hostname |
---|
| 64 | * host name to be included in the identifier or |
---|
| 65 | * <code>null</code> if no host name should be included. |
---|
| 66 | */ |
---|
| 67 | public void createMessageId(String hostname) { |
---|
| 68 | Header header = obtainHeader(); |
---|
| 69 | |
---|
| 70 | header.setField(newMessageId(hostname)); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | protected abstract Field newMessageId(String hostname); |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Returns the (decoded) value of the <i>Subject</i> header field of this |
---|
| 77 | * message or <code>null</code> if it is not present. |
---|
| 78 | * |
---|
| 79 | * @return the subject of this message. |
---|
| 80 | */ |
---|
| 81 | public String getSubject() { |
---|
| 82 | UnstructuredField field = obtainField(FieldName.SUBJECT); |
---|
| 83 | if (field == null) |
---|
| 84 | return null; |
---|
| 85 | |
---|
| 86 | return field.getValue(); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Sets the <i>Subject</i> header field for this message. The specified |
---|
| 91 | * string may contain non-ASCII characters, in which case it gets encoded as |
---|
| 92 | * an 'encoded-word' automatically. A <code>Header</code> is created if |
---|
| 93 | * this message does not already have one. |
---|
| 94 | * |
---|
| 95 | * @param subject |
---|
| 96 | * subject to set or <code>null</code> to remove the subject |
---|
| 97 | * header field. |
---|
| 98 | */ |
---|
| 99 | public void setSubject(String subject) { |
---|
| 100 | Header header = obtainHeader(); |
---|
| 101 | |
---|
| 102 | if (subject == null) { |
---|
| 103 | header.removeFields(FieldName.SUBJECT); |
---|
| 104 | } else { |
---|
| 105 | header.setField(newSubject(subject)); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * Returns the value of the <i>Date</i> header field of this message as |
---|
| 111 | * <code>Date</code> object or <code>null</code> if it is not present. |
---|
| 112 | * |
---|
| 113 | * @return the date of this message. |
---|
| 114 | */ |
---|
| 115 | public Date getDate() { |
---|
| 116 | DateTimeField dateField = obtainField(FieldName.DATE); |
---|
| 117 | if (dateField == null) |
---|
| 118 | return null; |
---|
| 119 | |
---|
| 120 | return dateField.getDate(); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * Sets the <i>Date</i> header field for this message. This method uses the |
---|
| 125 | * default <code>TimeZone</code> of this host to encode the specified |
---|
| 126 | * <code>Date</code> object into a string. |
---|
| 127 | * |
---|
| 128 | * @param date |
---|
| 129 | * date to set or <code>null</code> to remove the date header |
---|
| 130 | * field. |
---|
| 131 | */ |
---|
| 132 | public void setDate(Date date) { |
---|
| 133 | setDate(date, null); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * Sets the <i>Date</i> header field for this message. The specified |
---|
| 138 | * <code>TimeZone</code> is used to encode the specified <code>Date</code> |
---|
| 139 | * object into a string. |
---|
| 140 | * |
---|
| 141 | * @param date |
---|
| 142 | * date to set or <code>null</code> to remove the date header |
---|
| 143 | * field. |
---|
| 144 | * @param zone |
---|
| 145 | * a time zone. |
---|
| 146 | */ |
---|
| 147 | public void setDate(Date date, TimeZone zone) { |
---|
| 148 | Header header = obtainHeader(); |
---|
| 149 | |
---|
| 150 | if (date == null) { |
---|
| 151 | header.removeFields(FieldName.DATE); |
---|
| 152 | } else { |
---|
| 153 | header.setField(newDate(date, zone)); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * Returns the value of the <i>Sender</i> header field of this message as |
---|
| 159 | * <code>Mailbox</code> object or <code>null</code> if it is not |
---|
| 160 | * present. |
---|
| 161 | * |
---|
| 162 | * @return the sender of this message. |
---|
| 163 | */ |
---|
| 164 | public Mailbox getSender() { |
---|
| 165 | return getMailbox(FieldName.SENDER); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * Sets the <i>Sender</i> header field of this message to the specified |
---|
| 170 | * mailbox address. |
---|
| 171 | * |
---|
| 172 | * @param sender |
---|
| 173 | * address to set or <code>null</code> to remove the header |
---|
| 174 | * field. |
---|
| 175 | */ |
---|
| 176 | public void setSender(Mailbox sender) { |
---|
| 177 | setMailbox(FieldName.SENDER, sender); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Returns the value of the <i>From</i> header field of this message as |
---|
| 182 | * <code>MailboxList</code> object or <code>null</code> if it is not |
---|
| 183 | * present. |
---|
| 184 | * |
---|
| 185 | * @return value of the from field of this message. |
---|
| 186 | */ |
---|
| 187 | public MailboxList getFrom() { |
---|
| 188 | return getMailboxList(FieldName.FROM); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Sets the <i>From</i> header field of this message to the specified |
---|
| 193 | * mailbox address. |
---|
| 194 | * |
---|
| 195 | * @param from |
---|
| 196 | * address to set or <code>null</code> to remove the header |
---|
| 197 | * field. |
---|
| 198 | */ |
---|
| 199 | public void setFrom(Mailbox from) { |
---|
| 200 | setMailboxList(FieldName.FROM, from); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Sets the <i>From</i> header field of this message to the specified |
---|
| 205 | * mailbox addresses. |
---|
| 206 | * |
---|
| 207 | * @param from |
---|
| 208 | * addresses to set or <code>null</code> or no arguments to |
---|
| 209 | * remove the header field. |
---|
| 210 | */ |
---|
| 211 | public void setFrom(Mailbox... from) { |
---|
| 212 | setMailboxList(FieldName.FROM, from); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Sets the <i>From</i> header field of this message to the specified |
---|
| 217 | * mailbox addresses. |
---|
| 218 | * |
---|
| 219 | * @param from |
---|
| 220 | * addresses to set or <code>null</code> or an empty collection |
---|
| 221 | * to remove the header field. |
---|
| 222 | */ |
---|
| 223 | public void setFrom(Collection<Mailbox> from) { |
---|
| 224 | setMailboxList(FieldName.FROM, from); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * Returns the value of the <i>To</i> header field of this message as |
---|
| 229 | * <code>AddressList</code> object or <code>null</code> if it is not |
---|
| 230 | * present. |
---|
| 231 | * |
---|
| 232 | * @return value of the to field of this message. |
---|
| 233 | */ |
---|
| 234 | public AddressList getTo() { |
---|
| 235 | return getAddressList(FieldName.TO); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * Sets the <i>To</i> header field of this message to the specified |
---|
| 240 | * address. |
---|
| 241 | * |
---|
| 242 | * @param to |
---|
| 243 | * address to set or <code>null</code> to remove the header |
---|
| 244 | * field. |
---|
| 245 | */ |
---|
| 246 | public void setTo(Address to) { |
---|
| 247 | setAddressList(FieldName.TO, to); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | * Sets the <i>To</i> header field of this message to the specified |
---|
| 252 | * addresses. |
---|
| 253 | * |
---|
| 254 | * @param to |
---|
| 255 | * addresses to set or <code>null</code> or no arguments to |
---|
| 256 | * remove the header field. |
---|
| 257 | */ |
---|
| 258 | public void setTo(Address... to) { |
---|
| 259 | setAddressList(FieldName.TO, to); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * Sets the <i>To</i> header field of this message to the specified |
---|
| 264 | * addresses. |
---|
| 265 | * |
---|
| 266 | * @param to |
---|
| 267 | * addresses to set or <code>null</code> or an empty collection |
---|
| 268 | * to remove the header field. |
---|
| 269 | */ |
---|
| 270 | public void setTo(Collection<Address> to) { |
---|
| 271 | setAddressList(FieldName.TO, to); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | * Returns the value of the <i>Cc</i> header field of this message as |
---|
| 276 | * <code>AddressList</code> object or <code>null</code> if it is not |
---|
| 277 | * present. |
---|
| 278 | * |
---|
| 279 | * @return value of the cc field of this message. |
---|
| 280 | */ |
---|
| 281 | public AddressList getCc() { |
---|
| 282 | return getAddressList(FieldName.CC); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | /** |
---|
| 286 | * Sets the <i>Cc</i> header field of this message to the specified |
---|
| 287 | * address. |
---|
| 288 | * |
---|
| 289 | * @param cc |
---|
| 290 | * address to set or <code>null</code> to remove the header |
---|
| 291 | * field. |
---|
| 292 | */ |
---|
| 293 | public void setCc(Address cc) { |
---|
| 294 | setAddressList(FieldName.CC, cc); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /** |
---|
| 298 | * Sets the <i>Cc</i> header field of this message to the specified |
---|
| 299 | * addresses. |
---|
| 300 | * |
---|
| 301 | * @param cc |
---|
| 302 | * addresses to set or <code>null</code> or no arguments to |
---|
| 303 | * remove the header field. |
---|
| 304 | */ |
---|
| 305 | public void setCc(Address... cc) { |
---|
| 306 | setAddressList(FieldName.CC, cc); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | * Sets the <i>Cc</i> header field of this message to the specified |
---|
| 311 | * addresses. |
---|
| 312 | * |
---|
| 313 | * @param cc |
---|
| 314 | * addresses to set or <code>null</code> or an empty collection |
---|
| 315 | * to remove the header field. |
---|
| 316 | */ |
---|
| 317 | public void setCc(Collection<Address> cc) { |
---|
| 318 | setAddressList(FieldName.CC, cc); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | /** |
---|
| 322 | * Returns the value of the <i>Bcc</i> header field of this message as |
---|
| 323 | * <code>AddressList</code> object or <code>null</code> if it is not |
---|
| 324 | * present. |
---|
| 325 | * |
---|
| 326 | * @return value of the bcc field of this message. |
---|
| 327 | */ |
---|
| 328 | public AddressList getBcc() { |
---|
| 329 | return getAddressList(FieldName.BCC); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | /** |
---|
| 333 | * Sets the <i>Bcc</i> header field of this message to the specified |
---|
| 334 | * address. |
---|
| 335 | * |
---|
| 336 | * @param bcc |
---|
| 337 | * address to set or <code>null</code> to remove the header |
---|
| 338 | * field. |
---|
| 339 | */ |
---|
| 340 | public void setBcc(Address bcc) { |
---|
| 341 | setAddressList(FieldName.BCC, bcc); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | /** |
---|
| 345 | * Sets the <i>Bcc</i> header field of this message to the specified |
---|
| 346 | * addresses. |
---|
| 347 | * |
---|
| 348 | * @param bcc |
---|
| 349 | * addresses to set or <code>null</code> or no arguments to |
---|
| 350 | * remove the header field. |
---|
| 351 | */ |
---|
| 352 | public void setBcc(Address... bcc) { |
---|
| 353 | setAddressList(FieldName.BCC, bcc); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | /** |
---|
| 357 | * Sets the <i>Bcc</i> header field of this message to the specified |
---|
| 358 | * addresses. |
---|
| 359 | * |
---|
| 360 | * @param bcc |
---|
| 361 | * addresses to set or <code>null</code> or an empty collection |
---|
| 362 | * to remove the header field. |
---|
| 363 | */ |
---|
| 364 | public void setBcc(Collection<Address> bcc) { |
---|
| 365 | setAddressList(FieldName.BCC, bcc); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | /** |
---|
| 369 | * Returns the value of the <i>Reply-To</i> header field of this message as |
---|
| 370 | * <code>AddressList</code> object or <code>null</code> if it is not |
---|
| 371 | * present. |
---|
| 372 | * |
---|
| 373 | * @return value of the reply to field of this message. |
---|
| 374 | */ |
---|
| 375 | public AddressList getReplyTo() { |
---|
| 376 | return getAddressList(FieldName.REPLY_TO); |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | /** |
---|
| 380 | * Sets the <i>Reply-To</i> header field of this message to the specified |
---|
| 381 | * address. |
---|
| 382 | * |
---|
| 383 | * @param replyTo |
---|
| 384 | * address to set or <code>null</code> to remove the header |
---|
| 385 | * field. |
---|
| 386 | */ |
---|
| 387 | public void setReplyTo(Address replyTo) { |
---|
| 388 | setAddressList(FieldName.REPLY_TO, replyTo); |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | /** |
---|
| 392 | * Sets the <i>Reply-To</i> header field of this message to the specified |
---|
| 393 | * addresses. |
---|
| 394 | * |
---|
| 395 | * @param replyTo |
---|
| 396 | * addresses to set or <code>null</code> or no arguments to |
---|
| 397 | * remove the header field. |
---|
| 398 | */ |
---|
| 399 | public void setReplyTo(Address... replyTo) { |
---|
| 400 | setAddressList(FieldName.REPLY_TO, replyTo); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | /** |
---|
| 404 | * Sets the <i>Reply-To</i> header field of this message to the specified |
---|
| 405 | * addresses. |
---|
| 406 | * |
---|
| 407 | * @param replyTo |
---|
| 408 | * addresses to set or <code>null</code> or an empty collection |
---|
| 409 | * to remove the header field. |
---|
| 410 | */ |
---|
| 411 | public void setReplyTo(Collection<Address> replyTo) { |
---|
| 412 | setAddressList(FieldName.REPLY_TO, replyTo); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | private Mailbox getMailbox(String fieldName) { |
---|
| 416 | MailboxField field = obtainField(fieldName); |
---|
| 417 | if (field == null) |
---|
| 418 | return null; |
---|
| 419 | |
---|
| 420 | return field.getMailbox(); |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | private void setMailbox(String fieldName, Mailbox mailbox) { |
---|
| 424 | Header header = obtainHeader(); |
---|
| 425 | |
---|
| 426 | if (mailbox == null) { |
---|
| 427 | header.removeFields(fieldName); |
---|
| 428 | } else { |
---|
| 429 | header.setField(newMailbox(fieldName, mailbox)); |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | private MailboxList getMailboxList(String fieldName) { |
---|
| 434 | MailboxListField field = obtainField(fieldName); |
---|
| 435 | if (field == null) |
---|
| 436 | return null; |
---|
| 437 | |
---|
| 438 | return field.getMailboxList(); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | private void setMailboxList(String fieldName, Mailbox mailbox) { |
---|
| 442 | setMailboxList(fieldName, mailbox == null ? null : Collections |
---|
| 443 | .singleton(mailbox)); |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | private void setMailboxList(String fieldName, Mailbox... mailboxes) { |
---|
| 447 | setMailboxList(fieldName, mailboxes == null ? null : Arrays |
---|
| 448 | .asList(mailboxes)); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | private void setMailboxList(String fieldName, Collection<Mailbox> mailboxes) { |
---|
| 452 | Header header = obtainHeader(); |
---|
| 453 | |
---|
| 454 | if (mailboxes == null || mailboxes.isEmpty()) { |
---|
| 455 | header.removeFields(fieldName); |
---|
| 456 | } else { |
---|
| 457 | header.setField(newMailboxList(fieldName, mailboxes)); |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | private AddressList getAddressList(String fieldName) { |
---|
| 462 | AddressListField field = obtainField(fieldName); |
---|
| 463 | if (field == null) |
---|
| 464 | return null; |
---|
| 465 | |
---|
| 466 | return field.getAddressList(); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | private void setAddressList(String fieldName, Address address) { |
---|
| 470 | setAddressList(fieldName, address == null ? null : Collections |
---|
| 471 | .singleton(address)); |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | private void setAddressList(String fieldName, Address... addresses) { |
---|
| 475 | setAddressList(fieldName, addresses == null ? null : Arrays |
---|
| 476 | .asList(addresses)); |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | private void setAddressList(String fieldName, Collection<Address> addresses) { |
---|
| 480 | Header header = obtainHeader(); |
---|
| 481 | |
---|
| 482 | if (addresses == null || addresses.isEmpty()) { |
---|
| 483 | header.removeFields(fieldName); |
---|
| 484 | } else { |
---|
| 485 | header.setField(newAddressList(fieldName, addresses)); |
---|
| 486 | } |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | protected abstract AddressListField newAddressList(String fieldName, Collection<Address> addresses); |
---|
| 490 | |
---|
| 491 | protected abstract UnstructuredField newSubject(String subject); |
---|
| 492 | |
---|
| 493 | protected abstract DateTimeField newDate(Date date, TimeZone zone); |
---|
| 494 | |
---|
| 495 | protected abstract MailboxField newMailbox(String fieldName, Mailbox mailbox); |
---|
| 496 | |
---|
| 497 | protected abstract MailboxListField newMailboxList(String fieldName, Collection<Mailbox> mailboxes); |
---|
| 498 | |
---|
| 499 | |
---|
| 500 | } |
---|