This class is used to contain standard internet message headers,
used for SMTP (RFC822) and HTTP (RFC2068) messages as well as for
MIME (RFC 2045) applications such as transferring typed data and
grouping related items in multipart message bodies.
Message headers, as specified in RFC822, include a field name
and a field body. Order has no semantic significance, and several
fields with the same name may exist. However, most fields do not
(and should not) exist more than once in a header.
Many kinds of field body must conform to a specified syntax,
including the standard parenthesized comment syntax. This class
supports only two simple syntaxes, for dates and integers.
When processing headers, care must be taken to handle the case of
multiple same-name fields correctly. The values of such fields are
only available as strings. They may be accessed by index (treating
the header as an array of fields), or by name (returning an array
of string values).
Method from org.apache.tomcat.util.MimeHeaders Detail: |
public void addDateHeader(String name) {
addHeader(name).setDateValue();
}
|
public void addDateHeader(String name,
long t) {
addHeader(name).setDateValue(t);
}
|
protected MimeHeaderField addHeader(String name) {
MimeHeaderField mh = putHeader();
mh.setName(name);
return mh;
}
|
public void addHeader(String name,
String s) {
addHeader(name).setValue(s);
}
|
public void addIntHeader(String name,
int i) {
addHeader(name).setIntValue(i);
}
|
public void appendHeader(String name,
String s) {
MimeHeaderField mh = putHeader();
mh.setName(name);
mh.setValue(s);
}
Creates a new header with given name, and add it to the headers. |
public void clear() {
for (int i = 0; i < count; i++) {
headers[i].reset();
}
count = 0;
}
Clears all header fields. |
public boolean containsHeader(String name) {
return find(name) != null;
}
Returns true if the specified field is contained in the header,
otherwise returns false. |
public void dump(PrintStream out) {
for (int i = 0; i < count; i++) {
out.println(headers[i]);
}
}
Dumps current headers to specified PrintStream for debugging. |
protected MimeHeaderField find(String name) {
for (int i = 0; i < count; i++) {
if (headers[i].nameEquals(name)) {
return headers[i];
}
}
return null;
}
Finds and returns a header field with the given name. If no such
field exists, null is returned. If more than one such field is
in the header, an arbitrary one is returned. |
public int getAll(byte[] buf,
int buf_offset) {
int start_pt = buf_offset;
for (int i = 0; i < count; i++) {
buf_offset += headers[i].getBytes(buf, buf_offset);
}
return buf_offset - start_pt;
}
Get the current header fields in the byte array buf. The headers
fields are placed starting at offset buf_offset. |
public long getDateHeader(String name) throws IllegalArgumentException {
MimeHeaderField mh = find(name);
return mh != null ? mh.getDateValue() : -1;
}
Returns the date value of a header with the specified name. |
public MimeHeaderField getField(int n) {
return n >= 0 && n < count ? headers[n] : null;
}
Returns the Nth header field, or null if there is no such header.
This may be used to iterate through all header fields. |
public int getFieldCount(String name) {
int retval = 0;
for (int i = 0; i < count; i++)
if (headers [i].nameEquals (name))
retval++;
return retval;
}
Returns the number of fields using a given field name. |
public String getHeader(String name) {
MimeHeaderField mh = find(name);
return mh != null ? mh.getValue() : null;
}
Returns the string value of one of the headers with the
specified name. |
public String getHeader(int n) {
return n >= 0 && n < count ? headers[n].getValue() : null;
}
Returns the body of the nth header field where n >= 0. Returns null
if there were fewer than (n + 1) fields. This can be used along
with getHeaderName to iterate through all the fields in the header. |
public String getHeaderName(int n) {
return n >= 0 && n < count ? headers[n].getName() : null;
}
Returns the name of the nth header field where n >= 0. Returns null
if there were fewer than (n + 1) fields. This can be used to iterate
through all the fields in the header. |
public String[] getHeaders(String name) {
Vector values = getHeadersVector(name);
if (values.size() > 0) {
String retval[] = new String[values.size()];
for (int i = 0; i < retval.length; i++)
retval[i] = (String)values.elementAt(i);
return retval;
}
return null;
}
Returns the string value of all of the headers with the
specified name. |
public Vector getHeadersVector(String name) {
Vector values = new Vector();
for (int i = 0; i < count; i++) {
if (headers[i].nameEquals(name))
values.addElement(headers[i].getValue());
}
return values;
}
Same as getHeaders, return a Vector - avoid Vector-[]-Vector conversion |
public int getIntHeader(String name) throws NumberFormatException {
MimeHeaderField mh = find(name);
return mh != null ? mh.getIntValue() : -1;
}
Returns the integer value of a header with the specified name. |
public Enumeration names() {
return new MimeHeadersEnumerator(this);
}
Returns an enumeration of strings representing the header field names.
Field names may appear multiple times in this enumeration, indicating
that multiple fields with that name exist in this header. |
public void putDateHeader(String name) {
putHeader(name).setDateValue();
}
Creates a new header field whose value is the current date and time. |
public void putDateHeader(String name,
long t) {
putHeader(name).setDateValue(t);
}
Creates a new header field whose value is the specified time.
The encoding uses RFC 822 date format, as updated by RFC 1123. |
protected MimeHeaderField putHeader() {
MimeHeaderField mh;
int len = headers.length;
if (count >= len) {
// expand header list array
MimeHeaderField tmp[] = new MimeHeaderField[count * 2];
System.arraycopy(headers, 0, tmp, 0, len);
headers = tmp;
}
if ((mh = headers[count]) == null) {
headers[count] = mh = new MimeHeaderField();
}
count++;
return mh;
}
Adds a partially constructed field to the header. This
field has not had its name or value initialized. |
protected MimeHeaderField putHeader(String name) {
if (containsHeader(name)) {
removeHeader(name);
}
return addHeader(name);
}
Finds a header field given name. If the header doesn't exist,
it will create a new one. |
public void putHeader(String name,
String s) {
putHeader(name).setValue(s);
}
Creates a new header field whose value is the specified string. |
public void putIntHeader(String name,
int i) {
putHeader(name).setIntValue(i);
}
Creates a new header field whose value is the specified integer. |
public void read(ServletInputStream in) throws IOException {
// use pre-allocated buffer if possible
byte[] b;
if (count == 0) {
b = buf;
} else {
b = new byte[buf.length];
}
int off = 0;
while (true) {
int start = off;
while (true) {
int len = b.length - off;
if (len > 0) {
len = in.readLine(b, off, len);
if (len == -1) {
StringManager sm = getStringManager();
String msg = sm.getString("mimeHeader.connection.ioe");
throw new IOException (msg);
}
}
off += len;
if (len == 0 || b[off-1] == '\n') {
break;
}
// overflowed buffer, so temporarily expand and continue
byte[] tmp = new byte[b.length * 2];
System.arraycopy(b, 0, tmp, 0, b.length);
b = tmp;
}
// strip off trailing "\r\n"
if (--off > start && b[off-1] == '\r') {
--off;
}
if (off == start) {
break;
}
// XXX this does not currently handle headers which
// are folded to take more than one line.
putHeader().parse(b, start, off - start);
}
}
Reads header fields from the specified servlet input stream until
a blank line is encountered. |
public void removeHeader(String name) {
// XXX
// warning: rather sticky code; heavily tuned
for (int i = 0; i < count; i++) {
if (headers[i].nameEquals(name)) {
// reset and swap with last header
MimeHeaderField mh = headers[i];
mh.reset();
headers[i] = headers[count - 1];
headers[count - 1] = mh;
count--;
i--;
}
}
}
Removes a header field with the specified name. Does nothing
if such a field could not be found. |
public int size() {
return count;
}
Returns the current number of header fields. |
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("{");
for (int i = 0; i < count; i++) {
sb.append("{");
sb.append(headers[i].toString());
sb.append("}");
if (i < count - 1) {
sb.append(",");
}
}
sb.append("}");
return sb.toString();
}
Returns a lengthly string representation of the current header fields. |
public void write(ServletOutputStream out) throws IOException {
for (int i = 0; i < count; i++) {
headers[i].write(out);
}
out.println();
}
Writes out header fields to the specified servlet output stream. |