001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.core;
019:
020: import javax.mail.MessagingException;
021: import javax.mail.internet.InternetHeaders;
022:
023: import java.io.ByteArrayOutputStream;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.io.PrintStream;
027: import java.io.Serializable;
028: import java.util.Enumeration;
029:
030: import org.apache.mailet.RFC2822Headers;
031:
032: /**
033: * This interface defines a container for mail headers. Each header must use
034: * MIME format: <pre>name: value</pre>.
035: *
036: */
037: public class MailHeaders extends InternetHeaders implements
038: Serializable, Cloneable {
039:
040: /**
041: * No argument constructor
042: *
043: * @throws MessagingException if the super class cannot be properly instantiated
044: */
045: public MailHeaders() throws MessagingException {
046: super ();
047: }
048:
049: /**
050: * Constructor that takes an InputStream containing the contents
051: * of the set of mail headers.
052: *
053: * @param in the InputStream containing the header data
054: *
055: * @throws MessagingException if the super class cannot be properly instantiated
056: * based on the stream
057: */
058: public MailHeaders(InputStream in) throws MessagingException {
059: super ();
060: load(in);
061: }
062:
063: /**
064: * Write the headers to an output stream
065: *
066: * @param writer the stream to which to write the headers
067: */
068: public void writeTo(OutputStream out) {
069: PrintStream pout;
070: if (out instanceof PrintStream) {
071: pout = (PrintStream) out;
072: } else {
073: pout = new PrintStream(out);
074: }
075: for (Enumeration e = super .getAllHeaderLines(); e
076: .hasMoreElements();) {
077: pout.print((String) e.nextElement());
078: pout.print("\r\n");
079: }
080: // Print trailing CRLF
081: pout.print("\r\n");
082: }
083:
084: /**
085: * Generate a representation of the headers as a series of bytes.
086: *
087: * @return the byte array containing the headers
088: */
089: public byte[] toByteArray() {
090: ByteArrayOutputStream headersBytes = new ByteArrayOutputStream();
091: writeTo(headersBytes);
092: return headersBytes.toByteArray();
093: }
094:
095: /**
096: * Check if a particular header is present.
097: *
098: * @return true if the header is present, false otherwise
099: */
100: public boolean isSet(String name) {
101: String[] value = super .getHeader(name);
102: return (value != null && value.length != 0);
103: }
104:
105: /**
106: * If the new header is a Return-Path we get sure that we add it to the top
107: * Javamail, at least until 1.4.0 does the wrong thing if it loaded a stream with
108: * a return-path in the middle.
109: *
110: * @see javax.mail.internet.InternetHeaders#addHeader(java.lang.String, java.lang.String)
111: */
112: public void addHeader(String arg0, String arg1) {
113: if (RFC2822Headers.RETURN_PATH.equalsIgnoreCase(arg0)) {
114: headers.add(0, new InternetHeader(arg0, arg1));
115: } else {
116: super .addHeader(arg0, arg1);
117: }
118: }
119:
120: /**
121: * If the new header is a Return-Path we get sure that we add it to the top
122: * Javamail, at least until 1.4.0 does the wrong thing if it loaded a stream with
123: * a return-path in the middle.
124: *
125: * @see javax.mail.internet.InternetHeaders#setHeader(java.lang.String, java.lang.String)
126: */
127: public void setHeader(String arg0, String arg1) {
128: if (RFC2822Headers.RETURN_PATH.equalsIgnoreCase(arg0)) {
129: super .removeHeader(arg0);
130: }
131: super .setHeader(arg0, arg1);
132: }
133:
134: protected Object clone() throws CloneNotSupportedException {
135: // TODO Auto-generated method stub
136: return super .clone();
137: }
138:
139: /**
140: * Check if all REQUIRED headers fields as specified in RFC 822
141: * are present.
142: *
143: * @return true if the headers are present, false otherwise
144: */
145: public boolean isValid() {
146: return (isSet(RFC2822Headers.DATE) && isSet(RFC2822Headers.TO) && isSet(RFC2822Headers.FROM));
147: }
148: }
|