1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE file.
7 */
8 package org.apache.james.core;
9
10 import javax.mail.MessagingException;
11 import javax.mail.internet.InternetHeaders;
12 import java.io.*;
13 import java.util.Enumeration;
14
15 import org.apache.james.util.RFC2822Headers;
16
17 /***
18 * This interface defines a container for mail headers. Each header must use
19 * MIME format: <pre>name: value</pre>.
20 *
21 * @author Federico Barbieri <scoobie@systemy.it>
22 */
23 public class MailHeaders extends InternetHeaders implements Serializable, Cloneable {
24
25 /***
26 * No argument constructor
27 *
28 * @throws MessagingException if the super class cannot be properly instantiated
29 */
30 public MailHeaders() throws MessagingException {
31 super();
32 }
33
34 /***
35 * Constructor that takes an InputStream containing the contents
36 * of the set of mail headers.
37 *
38 * @param in the InputStream containing the header data
39 *
40 * @throws MessagingException if the super class cannot be properly instantiated
41 * based on the stream
42 */
43 public MailHeaders(InputStream in) throws MessagingException {
44 super(in);
45 }
46
47 // TODO: Overloading error. This is extremely dangerous, as the overloaded call
48 // does not behave like an overridden call. Specifically, the choice of
49 // which method to invoke is made at compile time, not at runtime.
50 // Potentially very, very bad if the behaviors diverge.
51
52 /***
53 * Write the headers to an PrintStream
54 *
55 * @param writer the stream to which to write the headers
56 */
57 public void writeTo(PrintStream writer) {
58 for (Enumeration e = super.getAllHeaderLines(); e.hasMoreElements(); ) {
59 writer.println((String) e.nextElement());
60 }
61 writer.println("");
62 }
63
64 /***
65 * Write the headers to an output stream
66 *
67 * @param out the stream to which to write the headers
68 */
69 public void writeTo(OutputStream out) {
70 writeTo(new PrintStream(out));
71 }
72
73 /***
74 * Generate a representation of the headers as a series of bytes.
75 *
76 * @return the byte array containing the headers
77 */
78 public byte[] toByteArray() {
79 ByteArrayOutputStream headersBytes = new ByteArrayOutputStream();
80 writeTo(headersBytes);
81 return headersBytes.toByteArray();
82 }
83
84 /***
85 * Check if a particular header is present.
86 *
87 * @return true if the header is present, false otherwise
88 */
89 public boolean isSet(String name) {
90 String[] value = super.getHeader(name);
91 return (value != null && value.length != 0);
92 }
93
94 /***
95 * Check if all REQUIRED headers fields as specified in RFC 822
96 * are present.
97 *
98 * @return true if the headers are present, false otherwise
99 */
100 public boolean isValid() {
101 return (isSet(RFC2822Headers.DATE) && isSet(RFC2822Headers.TO) && isSet(RFC2822Headers.FROM));
102 }
103 }
This page was automatically generated by Maven