public class InternetHeaders extends Object
Header
objects. This class is mostly intended for service providers. MimeMessage and MimeBody use this class for holding their 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.
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.
MimeUtility
Modifier and Type | Class and Description |
---|---|
protected static class |
InternetHeaders.InternetHeader
An individual internet header.
|
Modifier and Type | Field and Description |
---|---|
protected List<InternetHeaders.InternetHeader> |
headers
The actual list of Headers, including placeholder entries.
|
Constructor and Description |
---|
InternetHeaders()
Create an empty InternetHeaders object.
|
InternetHeaders(InputStream is)
Read and parse the given RFC822 message stream till the
blank line separating the header from the body.
|
InternetHeaders(InputStream is,
boolean allowutf8)
Read and parse the given RFC822 message stream till the
blank line separating the header from the body.
|
Modifier and Type | Method and Description |
---|---|
void |
addHeader(String name,
String value)
Add a header with the specified name and value to the header list.
|
void |
addHeaderLine(String line)
Add an RFC822 header line to the header store.
|
Enumeration<String> |
getAllHeaderLines()
Return all the header lines as an Enumeration of Strings.
|
Enumeration<Header> |
getAllHeaders()
Return all the headers as an Enumeration of
Header objects. |
String[] |
getHeader(String name)
Return all the values for the specified header.
|
String |
getHeader(String name,
String delimiter)
Get all the headers for this header name, returned as a single
String, with headers separated by the delimiter.
|
Enumeration<String> |
getMatchingHeaderLines(String[] names)
Return all matching header lines as an Enumeration of Strings.
|
Enumeration<Header> |
getMatchingHeaders(String[] names)
Return all matching
Header objects. |
Enumeration<String> |
getNonMatchingHeaderLines(String[] names)
Return all non-matching header lines
|
Enumeration<Header> |
getNonMatchingHeaders(String[] names)
Return all non-matching
Header objects. |
void |
load(InputStream is)
Read and parse the given RFC822 message stream till the
blank line separating the header from the body.
|
void |
load(InputStream is,
boolean allowutf8)
Read and parse the given RFC822 message stream till the
blank line separating the header from the body.
|
void |
removeHeader(String name)
Remove all header entries that match the given name
|
void |
setHeader(String name,
String value)
Change the first header line that matches name
to have value, adding a new header if no existing header
matches.
|
protected List<InternetHeaders.InternetHeader> headers
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 ":".public InternetHeaders()
public InternetHeaders(InputStream is) throws MessagingException
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.
is
- RFC822 input streamMessagingException
- for any I/O error reading the streampublic InternetHeaders(InputStream is, boolean allowutf8) throws MessagingException
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.
is
- RFC822 input streamallowutf8
- if UTF-8 encoded headers are allowedMessagingException
- for any I/O error reading the streampublic void load(InputStream is) throws MessagingException
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.
is
- RFC822 input streamMessagingException
- for any I/O error reading the streampublic void load(InputStream is, boolean allowutf8) throws MessagingException
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.
is
- RFC822 input streamallowutf8
- if UTF-8 encoded headers are allowedMessagingException
- for any I/O error reading the streampublic String[] getHeader(String name)
null
if no headers with the specified name exist.name
- header namepublic String getHeader(String name, String delimiter)
null
, only the first header is
returned. Returns null
if no headers with the specified name exist.name
- header namedelimiter
- delimiterpublic void setHeader(String name, String value)
Note that RFC822 headers can only contain US-ASCII characters
name
- header namevalue
- header valuepublic void addHeader(String name, String value)
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.
name
- header namevalue
- header valuepublic void removeHeader(String name)
name
- header namepublic Enumeration<Header> getAllHeaders()
Header
objects.public Enumeration<Header> getMatchingHeaders(String[] names)
Header
objects.names
- the headers to returnpublic Enumeration<Header> getNonMatchingHeaders(String[] names)
Header
objects.names
- the headers to not returnpublic void addHeaderLine(String line)
Note that RFC822 headers can only contain US-ASCII characters
line
- raw RFC822 header linepublic Enumeration<String> getAllHeaderLines()
public Enumeration<String> getMatchingHeaderLines(String[] names)
names
- the headers to returnpublic Enumeration<String> getNonMatchingHeaderLines(String[] names)
names
- the headers to not returnCopyright © 2018,2020 Eclipse Foundation.
Use is subject to license terms.