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 21 22 package org.apache.james.core; 23 24 import javax.mail.MessagingException; 25 import javax.mail.internet.InternetHeaders; 26 27 import java.io.ByteArrayOutputStream; 28 import java.io.InputStream; 29 import java.io.OutputStream; 30 import java.io.PrintStream; 31 import java.io.Serializable; 32 import java.util.Enumeration; 33 34 import org.apache.mailet.base.RFC2822Headers; 35 36 /** 37 * This interface defines a container for mail headers. Each header must use 38 * MIME format: <pre>name: value</pre>. 39 * 40 */ 41 public class MailHeaders extends InternetHeaders implements Serializable, Cloneable { 42 43 private static final long serialVersionUID = 238748126601L; 44 45 /** 46 * No argument constructor 47 * 48 * @throws MessagingException if the super class cannot be properly instantiated 49 */ 50 public MailHeaders() throws MessagingException { 51 super(); 52 } 53 54 /** 55 * Constructor that takes an InputStream containing the contents 56 * of the set of mail headers. 57 * 58 * @param in the InputStream containing the header data 59 * 60 * @throws MessagingException if the super class cannot be properly instantiated 61 * based on the stream 62 */ 63 public MailHeaders(InputStream in) throws MessagingException { 64 super(); 65 load(in); 66 } 67 68 /** 69 * Write the headers to an output stream 70 * 71 * @param out the OutputStream to which to write the headers 72 */ 73 public void writeTo(OutputStream out) { 74 PrintStream pout; 75 if (out instanceof PrintStream) { 76 pout = (PrintStream)out; 77 } else { 78 pout = new PrintStream(out); 79 } 80 for (Enumeration e = super.getAllHeaderLines(); e.hasMoreElements(); ) { 81 pout.print((String) e.nextElement()); 82 pout.print("\r\n"); 83 } 84 // Print trailing CRLF 85 pout.print("\r\n"); 86 } 87 88 /** 89 * Generate a representation of the headers as a series of bytes. 90 * 91 * @return the byte array containing the headers 92 */ 93 public byte[] toByteArray() { 94 ByteArrayOutputStream headersBytes = new ByteArrayOutputStream(); 95 writeTo(headersBytes); 96 return headersBytes.toByteArray(); 97 } 98 99 /** 100 * Check if a particular header is present. 101 * 102 * @return true if the header is present, false otherwise 103 */ 104 public boolean isSet(String name) { 105 String[] value = super.getHeader(name); 106 return (value != null && value.length != 0); 107 } 108 109 /** 110 * If the new header is a Return-Path we get sure that we add it to the top 111 * Javamail, at least until 1.4.0 does the wrong thing if it loaded a stream with 112 * a return-path in the middle. 113 * 114 * @see javax.mail.internet.InternetHeaders#addHeader(java.lang.String, java.lang.String) 115 */ 116 public void addHeader(String arg0, String arg1) { 117 if (RFC2822Headers.RETURN_PATH.equalsIgnoreCase(arg0)) { 118 headers.add(0, new InternetHeader(arg0, arg1)); 119 } else { 120 super.addHeader(arg0, arg1); 121 } 122 } 123 124 /** 125 * If the new header is a Return-Path we get sure that we add it to the top 126 * Javamail, at least until 1.4.0 does the wrong thing if it loaded a stream with 127 * a return-path in the middle. 128 * 129 * @see javax.mail.internet.InternetHeaders#setHeader(java.lang.String, java.lang.String) 130 */ 131 public void setHeader(String arg0, String arg1) { 132 if (RFC2822Headers.RETURN_PATH.equalsIgnoreCase(arg0)) { 133 super.removeHeader(arg0); 134 } 135 super.setHeader(arg0, arg1); 136 } 137 138 protected Object clone() throws CloneNotSupportedException { 139 // TODO Auto-generated method stub 140 return super.clone(); 141 } 142 143 /** 144 * Check if all REQUIRED headers fields as specified in RFC 822 145 * are present. 146 * 147 * @return true if the headers are present, false otherwise 148 */ 149 public boolean isValid() { 150 return (isSet(RFC2822Headers.DATE) && isSet(RFC2822Headers.TO) && isSet(RFC2822Headers.FROM)); 151 } 152 }