Class InternetHeaders
- java.lang.Object
-
- javax.mail.internet.InternetHeaders
-
public class InternetHeaders extends java.lang.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 orHeader
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 headersRFC822 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
, andaddHeaderLine
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.The current implementation supports the System property
mail.mime.ignorewhitespacelines
, which if set to true will cause a line containing only whitespace to be considered a blank line terminating the header.- See Also:
MimeUtility
-
-
Constructor Summary
Constructors Constructor Description InternetHeaders()
Create an empty InternetHeaders object.InternetHeaders(java.io.InputStream is)
Read and parse the given RFC822 message stream till the blank line separating the header from the body.InternetHeaders(java.io.InputStream is, boolean allowutf8)
Read and parse the given RFC822 message stream till the blank line separating the header from the body.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addHeader(java.lang.String name, java.lang.String value)
Add a header with the specified name and value to the header list.void
addHeaderLine(java.lang.String line)
Add an RFC822 header line to the header store.java.util.Enumeration<java.lang.String>
getAllHeaderLines()
Return all the header lines as an Enumeration of Strings.java.util.Enumeration<Header>
getAllHeaders()
Return all the headers as an Enumeration ofHeader
objects.java.lang.String[]
getHeader(java.lang.String name)
Return all the values for the specified header.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.java.util.Enumeration<java.lang.String>
getMatchingHeaderLines(java.lang.String[] names)
Return all matching header lines as an Enumeration of Strings.java.util.Enumeration<Header>
getMatchingHeaders(java.lang.String[] names)
Return all matchingHeader
objects.java.util.Enumeration<java.lang.String>
getNonMatchingHeaderLines(java.lang.String[] names)
Return all non-matching header linesjava.util.Enumeration<Header>
getNonMatchingHeaders(java.lang.String[] names)
Return all non-matchingHeader
objects.void
load(java.io.InputStream is)
Read and parse the given RFC822 message stream till the blank line separating the header from the body.void
load(java.io.InputStream is, boolean allowutf8)
Read and parse the given RFC822 message stream till the blank line separating the header from the body.void
removeHeader(java.lang.String name)
Remove all header entries that match the given namevoid
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.
-
-
-
Constructor Detail
-
InternetHeaders
public InternetHeaders()
Create an empty InternetHeaders object. Placeholder entries are inserted to indicate the preferred order of headers.
-
InternetHeaders
public InternetHeaders(java.io.InputStream is) throws MessagingException
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.
- Parameters:
is
- RFC822 input stream- Throws:
MessagingException
- for any I/O error reading the stream
-
InternetHeaders
public InternetHeaders(java.io.InputStream is, boolean allowutf8) throws MessagingException
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.
- Parameters:
is
- RFC822 input streamallowutf8
- if UTF-8 encoded headers are allowed- Throws:
MessagingException
- for any I/O error reading the stream- Since:
- JavaMail 1.6
-
-
Method Detail
-
load
public void load(java.io.InputStream is) throws MessagingException
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.
- Parameters:
is
- RFC822 input stream- Throws:
MessagingException
- for any I/O error reading the stream
-
load
public void load(java.io.InputStream is, boolean allowutf8) throws MessagingException
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.
- Parameters:
is
- RFC822 input streamallowutf8
- if UTF-8 encoded headers are allowed- Throws:
MessagingException
- for any I/O error reading the stream- Since:
- JavaMail 1.6
-
getHeader
public java.lang.String[] getHeader(java.lang.String name)
Return all the values for the specified header. The values are String objects. Returnsnull
if no headers with the specified name exist.- Parameters:
name
- header name- Returns:
- array of header values, or null if none
-
getHeader
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 isnull
, only the first header is returned. Returnsnull
if no headers with the specified name exist.- Parameters:
name
- header namedelimiter
- delimiter- Returns:
- the value fields for all headers with this name, or null if none
-
setHeader
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
- Parameters:
name
- header namevalue
- header value
-
addHeader
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 possibleReturn-Path
header.Note that RFC822 headers can only contain US-ASCII characters.
- Parameters:
name
- header namevalue
- header value
-
removeHeader
public void removeHeader(java.lang.String name)
Remove all header entries that match the given name- Parameters:
name
- header name
-
getAllHeaders
public java.util.Enumeration<Header> getAllHeaders()
Return all the headers as an Enumeration ofHeader
objects.- Returns:
- Enumeration of Header objects
-
getMatchingHeaders
public java.util.Enumeration<Header> getMatchingHeaders(java.lang.String[] names)
Return all matchingHeader
objects.- Parameters:
names
- the headers to return- Returns:
- Enumeration of matching Header objects
-
getNonMatchingHeaders
public java.util.Enumeration<Header> getNonMatchingHeaders(java.lang.String[] names)
Return all non-matchingHeader
objects.- Parameters:
names
- the headers to not return- Returns:
- Enumeration of non-matching Header objects
-
addHeaderLine
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
- Parameters:
line
- raw RFC822 header line
-
getAllHeaderLines
public java.util.Enumeration<java.lang.String> getAllHeaderLines()
Return all the header lines as an Enumeration of Strings.- Returns:
- Enumeration of Strings of all header lines
-
getMatchingHeaderLines
public java.util.Enumeration<java.lang.String> getMatchingHeaderLines(java.lang.String[] names)
Return all matching header lines as an Enumeration of Strings.- Parameters:
names
- the headers to return- Returns:
- Enumeration of Strings of all matching header lines
-
getNonMatchingHeaderLines
public java.util.Enumeration<java.lang.String> getNonMatchingHeaderLines(java.lang.String[] names)
Return all non-matching header lines- Parameters:
names
- the headers to not return- Returns:
- Enumeration of Strings of all non-matching header lines
-
-