InternetHeaderspublic class InternetHeaders extends Object InternetHeaders is a utility class that manages RFC822 style
headers. Given an RFC822 format message stream, it reads lines
until the blank line that indicates end of header. The input stream
is positioned at the start of the body. The lines are stored
within the object and can be extracted as either Strings or
{@link javax.mail.Header} objects.
This class is mostly intended for service providers. MimeMessage
and MimeBody use this class for holding their headers.
A note on RFC822 and MIME headers
RFC822 and MIME header fields must contain only
US-ASCII characters. If a header contains non US-ASCII characters,
it must be encoded as per the rules in RFC 2047. The MimeUtility
class provided in this package can be used to to achieve this.
Callers of the setHeader , addHeader , and
addHeaderLine methods are responsible for enforcing
the MIME requirements for the specified headers. In addition, these
header fields must be folded (wrapped) before being sent if they
exceed the line length limitation for the transport (1000 bytes for
SMTP). Received headers may have been folded. The application is
responsible for folding and unfolding headers as appropriate. |
Fields Summary |
---|
protected List | headersThe actual list of Headers, including placeholder entries.
Placeholder entries are Headers with a null value and
are never seen by clients of the InternetHeaders class.
Placeholder entries are used to keep track of the preferred
order of headers. Headers are never actually removed from
the list, they're converted into placeholder entries.
New headers are added after existing headers of the same name
(or before in the case of Received and
Return-Path headers). If no existing header
or placeholder for the header is found, new headers are
added after the special placeholder with the name ":". |
Constructors Summary |
---|
public InternetHeaders()Create an empty InternetHeaders object. Placeholder entries
are inserted to indicate the preferred order of headers.
headers = new ArrayList(40);
headers.add(new InternetHeader("Return-Path", null));
headers.add(new InternetHeader("Received", null));
headers.add(new InternetHeader("Resent-Date", null));
headers.add(new InternetHeader("Resent-From", null));
headers.add(new InternetHeader("Resent-Sender", null));
headers.add(new InternetHeader("Resent-To", null));
headers.add(new InternetHeader("Resent-Cc", null));
headers.add(new InternetHeader("Resent-Bcc", null));
headers.add(new InternetHeader("Resent-Message-Id", null));
headers.add(new InternetHeader("Date", null));
headers.add(new InternetHeader("From", null));
headers.add(new InternetHeader("Sender", null));
headers.add(new InternetHeader("Reply-To", null));
headers.add(new InternetHeader("To", null));
headers.add(new InternetHeader("Cc", null));
headers.add(new InternetHeader("Bcc", null));
headers.add(new InternetHeader("Message-Id", null));
headers.add(new InternetHeader("In-Reply-To", null));
headers.add(new InternetHeader("References", null));
headers.add(new InternetHeader("Subject", null));
headers.add(new InternetHeader("Comments", null));
headers.add(new InternetHeader("Keywords", null));
headers.add(new InternetHeader("Errors-To", null));
headers.add(new InternetHeader("MIME-Version", null));
headers.add(new InternetHeader("Content-Type", null));
headers.add(new InternetHeader("Content-Transfer-Encoding", null));
headers.add(new InternetHeader("Content-MD5", null));
headers.add(new InternetHeader(":", null));
headers.add(new InternetHeader("Content-Length", null));
headers.add(new InternetHeader("Status", null));
| public InternetHeaders(InputStream is)Read and parse the given RFC822 message stream till the
blank line separating the header from the body. The input
stream is left positioned at the start of the body. The
header lines are stored internally.
For efficiency, wrap a BufferedInputStream around the actual
input stream and pass it as the parameter.
No placeholder entries are inserted; the original order of
the headers is preserved.
headers = new ArrayList(40);
load(is);
|
Methods Summary |
---|
public void | addHeader(java.lang.String name, java.lang.String value)Add a header with the specified name and value to the header list.
The current implementation knows about the preferred order of most
well-known headers and will insert headers in that order. In
addition, it knows that Received headers should be
inserted in reverse order (newest before oldest), and that they
should appear at the beginning of the headers, preceeded only by
a possible Return-Path header.
Note that RFC822 headers can only contain US-ASCII characters.
int pos = headers.size();
boolean addReverse =
name.equalsIgnoreCase("Received") ||
name.equalsIgnoreCase("Return-Path");
if (addReverse)
pos = 0;
for (int i = headers.size() - 1; i >= 0; i--) {
InternetHeader h = (InternetHeader)headers.get(i);
if (name.equalsIgnoreCase(h.getName())) {
if (addReverse) {
pos = i;
} else {
headers.add(i + 1, new InternetHeader(name, value));
return;
}
}
// marker for default place to add new headers
if (!addReverse && h.getName().equals(":"))
pos = i;
}
headers.add(pos, new InternetHeader(name, value));
| public void | addHeaderLine(java.lang.String line)Add an RFC822 header line to the header store.
If the line starts with a space or tab (a continuation line),
add it to the last header line in the list. Otherwise,
append the new header line to the list.
Note that RFC822 headers can only contain US-ASCII characters
try {
char c = line.charAt(0);
if (c == ' " || c == '\t") {
InternetHeader h =
(InternetHeader)headers.get(headers.size() - 1);
h.line += "\r\n" + line;
} else
headers.add(new InternetHeader(line));
} catch (StringIndexOutOfBoundsException e) {
// line is empty, ignore it
return;
} catch (NoSuchElementException e) {
// XXX - vector is empty?
}
| public java.util.Enumeration | getAllHeaderLines()Return all the header lines as an Enumeration of Strings.
return (getNonMatchingHeaderLines(null));
| public java.util.Enumeration | getAllHeaders()Return all the headers as an Enumeration of
{@link javax.mail.Header} objects.
return (new matchEnum(headers, null, false, false));
| public java.lang.String[] | getHeader(java.lang.String name)Return all the values for the specified header. The
values are String objects. Returns null
if no headers with the specified name exist.
Iterator e = headers.iterator();
// XXX - should we just step through in index order?
List v = new ArrayList(); // accumulate return values
while (e.hasNext()) {
InternetHeader h = (InternetHeader)e.next();
if (name.equalsIgnoreCase(h.getName()) && h.line != null) {
v.add(h.getValue());
}
}
if (v.size() == 0)
return (null);
// convert List to an array for return
String r[] = new String[v.size()];
r = (String[])v.toArray(r);
return (r);
| public java.lang.String | getHeader(java.lang.String name, java.lang.String delimiter)Get all the headers for this header name, returned as a single
String, with headers separated by the delimiter. If the
delimiter is null , only the first header is
returned. Returns null
if no headers with the specified name exist.
String s[] = getHeader(name);
if (s == null)
return null;
if ((s.length == 1) || delimiter == null)
return s[0];
StringBuffer r = new StringBuffer(s[0]);
for (int i = 1; i < s.length; i++) {
r.append(delimiter);
r.append(s[i]);
}
return r.toString();
| public java.util.Enumeration | getMatchingHeaderLines(java.lang.String[] names)Return all matching header lines as an Enumeration of Strings.
return (new matchEnum(headers, names, true, true));
| public java.util.Enumeration | getMatchingHeaders(java.lang.String[] names)Return all matching {@link javax.mail.Header} objects.
return (new matchEnum(headers, names, true, false));
| public java.util.Enumeration | getNonMatchingHeaderLines(java.lang.String[] names)Return all non-matching header lines
return (new matchEnum(headers, names, false, true));
| public java.util.Enumeration | getNonMatchingHeaders(java.lang.String[] names)Return all non-matching {@link javax.mail.Header} objects.
return (new matchEnum(headers, names, false, false));
| public void | load(java.io.InputStream is)Read and parse the given RFC822 message stream till the
blank line separating the header from the body. Store the
header lines inside this InternetHeaders object. The order
of header lines is preserved.
Note that the header lines are added into this InternetHeaders
object, so any existing headers in this object will not be
affected. Headers are added to the end of the existing list
of headers, in order.
// Read header lines until a blank line. It is valid
// to have BodyParts with no header lines.
String line;
LineInputStream lis = new LineInputStream(is);
String prevline = null; // the previous header line, as a string
// a buffer to accumulate the header in, when we know it's needed
StringBuffer lineBuffer = new StringBuffer();
try {
//while ((line = lis.readLine()) != null) {
do {
line = lis.readLine();
if (line != null &&
(line.startsWith(" ") || line.startsWith("\t"))) {
// continuation of header
if (prevline != null) {
lineBuffer.append(prevline);
prevline = null;
}
lineBuffer.append("\r\n");
lineBuffer.append(line);
} else {
// new header
if (prevline != null)
addHeaderLine(prevline);
else if (lineBuffer.length() > 0) {
// store previous header first
addHeaderLine(lineBuffer.toString());
lineBuffer.setLength(0);
}
prevline = line;
}
} while (line != null && line.length() > 0);
} catch (IOException ioex) {
throw new MessagingException("Error in input stream", ioex);
}
| public void | removeHeader(java.lang.String name)Remove all header entries that match the given name
for (int i = 0; i < headers.size(); i++) {
InternetHeader h = (InternetHeader)headers.get(i);
if (name.equalsIgnoreCase(h.getName())) {
h.line = null;
//headers.remove(i);
//i--; // have to look at i again
}
}
| public void | setHeader(java.lang.String name, java.lang.String value)Change the first header line that matches name
to have value, adding a new header if no existing header
matches. Remove all matching headers but the first.
Note that RFC822 headers can only contain US-ASCII characters
boolean found = false;
for (int i = 0; i < headers.size(); i++) {
InternetHeader h = (InternetHeader)headers.get(i);
if (name.equalsIgnoreCase(h.getName())) {
if (!found) {
int j;
if (h.line != null && (j = h.line.indexOf(':")) >= 0) {
h.line = h.line.substring(0, j + 1) + " " + value;
// preserves capitalization, spacing
} else {
h.line = name + ": " + value;
}
found = true;
} else {
headers.remove(i);
i--; // have to look at i again
}
}
}
if (!found) {
addHeader(name, value);
}
|
|