KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > core > MailHeaders


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.core;
19
20 import javax.mail.MessagingException JavaDoc;
21 import javax.mail.internet.InternetHeaders JavaDoc;
22 import java.io.*;
23 import java.util.Enumeration JavaDoc;
24
25 import org.apache.james.util.RFC2822Headers;
26
27 /**
28  * This interface defines a container for mail headers. Each header must use
29  * MIME format: <pre>name: value</pre>.
30  *
31  */

32 public class MailHeaders extends InternetHeaders JavaDoc implements Serializable, Cloneable JavaDoc {
33
34     /**
35      * No argument constructor
36      *
37      * @throws MessagingException if the super class cannot be properly instantiated
38      */

39     public MailHeaders() throws MessagingException JavaDoc {
40         super();
41     }
42
43     /**
44      * Constructor that takes an InputStream containing the contents
45      * of the set of mail headers.
46      *
47      * @param in the InputStream containing the header data
48      *
49      * @throws MessagingException if the super class cannot be properly instantiated
50      * based on the stream
51      */

52     public MailHeaders(InputStream in) throws MessagingException JavaDoc {
53         super(in);
54     }
55
56     /**
57      * Write the headers to an output stream
58      *
59      * @param writer the stream to which to write the headers
60      */

61     public void writeTo(OutputStream out) {
62         PrintStream pout;
63         if (out instanceof PrintStream) {
64             pout = (PrintStream)out;
65         } else {
66             pout = new PrintStream(out);
67         }
68         for (Enumeration JavaDoc e = super.getAllHeaderLines(); e.hasMoreElements(); ) {
69             pout.print((String JavaDoc) e.nextElement());
70             pout.print("\r\n");
71         }
72         // Print trailing CRLF
73
pout.print("\r\n");
74     }
75
76     /**
77      * Generate a representation of the headers as a series of bytes.
78      *
79      * @return the byte array containing the headers
80      */

81     public byte[] toByteArray() {
82         ByteArrayOutputStream headersBytes = new ByteArrayOutputStream();
83         writeTo(headersBytes);
84         return headersBytes.toByteArray();
85     }
86
87     /**
88      * Check if a particular header is present.
89      *
90      * @return true if the header is present, false otherwise
91      */

92     public boolean isSet(String JavaDoc name) {
93         String JavaDoc[] value = super.getHeader(name);
94         return (value != null && value.length != 0);
95     }
96
97     /**
98      * Check if all REQUIRED headers fields as specified in RFC 822
99      * are present.
100      *
101      * @return true if the headers are present, false otherwise
102      */

103     public boolean isValid() {
104         return (isSet(RFC2822Headers.DATE) && isSet(RFC2822Headers.TO) && isSet(RFC2822Headers.FROM));
105     }
106 }
107
Popular Tags