InternetHeaders.java revision 325
2362N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A * or visit www.oracle.com if you need additional information or have any 0N/A * InternetHeaders is a utility class that manages RFC822 style 0N/A * headers. Given an RFC822 format message stream, it reads lines 0N/A * until the blank line that indicates end of header. The input stream 0N/A * is positioned at the start of the body. The lines are stored 0N/A * within the object and can be extracted as either Strings or 0N/A * {@link Header} objects. <p> 0N/A * This class is mostly intended for service providers. MimeMessage 0N/A * and MimeBody use this class for holding their headers. <p> 0N/A * <hr> <strong>A note on RFC822 and MIME headers</strong><p> 0N/A * RFC822 and MIME header fields <strong>must</strong> contain only 0N/A * US-ASCII characters. If a header contains non US-ASCII characters, 0N/A * it must be encoded as per the rules in RFC 2047. The MimeUtility 0N/A * class provided in this package can be used to to achieve this. 0N/A * Callers of the <code>setHeader</code>, <code>addHeader</code>, and 0N/A * <code>addHeaderLine</code> methods are responsible for enforcing 0N/A * the MIME requirements for the specified headers. In addition, these 0N/A * header fields must be folded (wrapped) before being sent if they 0N/A * exceed the line length limitation for the transport (1000 bytes for 0N/A * SMTP). Received headers may have been folded. The application is 0N/A * responsible for folding and unfolding headers as appropriate. <p> * Lazily cerated view of header lines (Strings). * Create an empty InternetHeaders object. * 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. <p> * For efficiency, wrap a BufferedInputStream around the actual * input stream and pass it as the parameter. * @param is RFC822 input stream * 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. <p> * Note that the header lines are added into this InternetHeaders * object, so any existing headers in this object will not be * @param is RFC822 input stream // Read header lines until a blank line. It is valid // to have BodyParts with no header lines. // a buffer to accumulate the header in, when we know it's needed //while ((line = lis.readLine()) != null) { // continuation of header // store previous header first * Return all the values for the specified header. The * values are String objects. Returns <code>null</code> * if no headers with the specified name exist. * @param name header name * @return array of header values, or null if none // XXX - should we just step through in index order? for(
int i=
0; i<
len; i++ ) {
// convert Vector to an array for return * Get all the headers for this header name, returned as a single * String, with headers separated by the delimiter. If the * delimiter is <code>null</code>, only the first header is * returned. Returns <code>null</code> * if no headers with the specified name exist. * @param delimiter delimiter * @return the value fields for all headers with * this name, or null if none * @param name header name for (
int i =
1; i < s.
length; i++) {
* 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. <p> * Note that RFC822 headers can only contain US-ASCII characters * @param name header name * @param value header value i--;
// have to look at i again * Add a header with the specified name and value to the header list. <p> * Note that RFC822 headers can only contain US-ASCII characters. * @param name header name * @param value header value // marker for default place to add new headers * Remove all header entries that match the given name * @param name header name i--;
// have to look at i again * Return all the headers as an Enumeration of * {@link Header} objects. return headers;
// conceptually it should be read-only, but for performance reason I'm not wrapping it here * 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. <p> * Note that RFC822 headers can only contain US-ASCII characters * @param line raw RFC822 header line if (c ==
' ' || c ==
'\t') {
// line is empty, ignore it // XXX - vector is empty? * Return all the header lines as a collection * A private utility class to represent an individual header. // XXX - should these be private? String name;
// the canonicalized (trimmed) name of this header // XXX - should name be stored in lower case? * Constructor that takes a line and splits out * Constructor that takes a header name and value. * Return the "name" part of the header line. * Return the "value" part of the header line. // Content-Description should retain the folded whitespace after header unfolding - // rf. RFC2822 section 2.2.3, rf. RFC2822 section 3.2.3 if (!(
/*c == ' ' ||*/c ==
'\t' || c ==
'\r' || c ==
'\n'))
// skip whitespace after ':' if (!(c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n'))