325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/A/*
325N/A * @(#)InternetHeaders.java 1.16 02/08/08
325N/A */
325N/A
325N/A
325N/A
325N/Apackage com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
325N/A
325N/Aimport com.sun.xml.internal.messaging.saaj.packaging.mime.Header;
325N/Aimport com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
325N/Aimport com.sun.xml.internal.messaging.saaj.packaging.mime.util.LineInputStream;
325N/Aimport com.sun.xml.internal.messaging.saaj.util.FinalArrayList;
325N/A
325N/Aimport java.io.IOException;
325N/Aimport java.io.InputStream;
325N/Aimport java.util.AbstractList;
325N/Aimport java.util.List;
325N/Aimport java.util.NoSuchElementException;
325N/A
325N/A/**
325N/A * InternetHeaders is a utility class that manages RFC822 style
325N/A * headers. Given an RFC822 format message stream, it reads lines
325N/A * until the blank line that indicates end of header. The input stream
325N/A * is positioned at the start of the body. The lines are stored
325N/A * within the object and can be extracted as either Strings or
325N/A * {@link Header} objects. <p>
325N/A * <p/>
325N/A * This class is mostly intended for service providers. MimeMessage
325N/A * and MimeBody use this class for holding their headers. <p>
325N/A * <p/>
325N/A * <hr> <strong>A note on RFC822 and MIME headers</strong><p>
325N/A * <p/>
325N/A * RFC822 and MIME header fields <strong>must</strong> contain only
325N/A * US-ASCII characters. If a header contains non US-ASCII characters,
325N/A * it must be encoded as per the rules in RFC 2047. The MimeUtility
325N/A * class provided in this package can be used to to achieve this.
325N/A * Callers of the <code>setHeader</code>, <code>addHeader</code>, and
325N/A * <code>addHeaderLine</code> methods are responsible for enforcing
325N/A * the MIME requirements for the specified headers. In addition, these
325N/A * header fields must be folded (wrapped) before being sent if they
325N/A * exceed the line length limitation for the transport (1000 bytes for
325N/A * SMTP). Received headers may have been folded. The application is
325N/A * responsible for folding and unfolding headers as appropriate. <p>
325N/A *
325N/A * @author John Mani
325N/A * @author Bill Shannon
325N/A * @see MimeUtility
325N/A */
325N/Apublic final class InternetHeaders {
325N/A
325N/A private final FinalArrayList headers = new FinalArrayList();
325N/A
325N/A /**
325N/A * Lazily cerated view of header lines (Strings).
325N/A */
325N/A private List headerValueView;
325N/A
325N/A /**
325N/A * Create an empty InternetHeaders object.
325N/A */
325N/A public InternetHeaders() {
325N/A }
325N/A
325N/A /**
325N/A * Read and parse the given RFC822 message stream till the
325N/A * blank line separating the header from the body. The input
325N/A * stream is left positioned at the start of the body. The
325N/A * header lines are stored internally. <p>
325N/A * <p/>
325N/A * For efficiency, wrap a BufferedInputStream around the actual
325N/A * input stream and pass it as the parameter.
325N/A *
325N/A * @param is RFC822 input stream
325N/A */
325N/A public InternetHeaders(InputStream is) throws MessagingException {
325N/A load(is);
325N/A }
325N/A
325N/A /**
325N/A * Read and parse the given RFC822 message stream till the
325N/A * blank line separating the header from the body. Store the
325N/A * header lines inside this InternetHeaders object. <p>
325N/A * <p/>
325N/A * Note that the header lines are added into this InternetHeaders
325N/A * object, so any existing headers in this object will not be
325N/A * affected.
325N/A *
325N/A * @param is RFC822 input stream
325N/A */
325N/A public void load(InputStream is) throws MessagingException {
325N/A // Read header lines until a blank line. It is valid
325N/A // to have BodyParts with no header lines.
325N/A String line;
325N/A LineInputStream lis = new LineInputStream(is);
325N/A String prevline = null; // the previous header line, as a string
325N/A // a buffer to accumulate the header in, when we know it's needed
325N/A StringBuffer lineBuffer = new StringBuffer();
325N/A
325N/A try {
325N/A //while ((line = lis.readLine()) != null) {
325N/A do {
325N/A line = lis.readLine();
325N/A if (line != null &&
325N/A (line.startsWith(" ") || line.startsWith("\t"))) {
325N/A // continuation of header
325N/A if (prevline != null) {
325N/A lineBuffer.append(prevline);
325N/A prevline = null;
325N/A }
325N/A lineBuffer.append("\r\n");
325N/A lineBuffer.append(line);
325N/A } else {
325N/A // new header
325N/A if (prevline != null)
325N/A addHeaderLine(prevline);
325N/A else if (lineBuffer.length() > 0) {
325N/A // store previous header first
325N/A addHeaderLine(lineBuffer.toString());
325N/A lineBuffer.setLength(0);
325N/A }
325N/A prevline = line;
325N/A }
325N/A } while (line != null && line.length() > 0);
325N/A } catch (IOException ioex) {
325N/A throw new MessagingException("Error in input stream", ioex);
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Return all the values for the specified header. The
325N/A * values are String objects. Returns <code>null</code>
325N/A * if no headers with the specified name exist.
325N/A *
325N/A * @param name header name
325N/A * @return array of header values, or null if none
325N/A */
325N/A public String[] getHeader(String name) {
325N/A // XXX - should we just step through in index order?
325N/A FinalArrayList v = new FinalArrayList(); // accumulate return values
325N/A
325N/A int len = headers.size();
325N/A for( int i=0; i<len; i++ ) {
325N/A hdr h = (hdr) headers.get(i);
325N/A if (name.equalsIgnoreCase(h.name)) {
325N/A v.add(h.getValue());
325N/A }
325N/A }
325N/A if (v.size() == 0)
325N/A return (null);
325N/A // convert Vector to an array for return
325N/A return (String[]) v.toArray(new String[v.size()]);
325N/A }
325N/A
325N/A /**
325N/A * Get all the headers for this header name, returned as a single
325N/A * String, with headers separated by the delimiter. If the
325N/A * delimiter is <code>null</code>, only the first header is
325N/A * returned. Returns <code>null</code>
325N/A * if no headers with the specified name exist.
325N/A *
325N/A * @param delimiter delimiter
325N/A * @return the value fields for all headers with
325N/A * this name, or null if none
325N/A * @param name header name
325N/A */
325N/A public String getHeader(String name, String delimiter) {
325N/A String[] s = getHeader(name);
325N/A
325N/A if (s == null)
325N/A return null;
325N/A
325N/A if ((s.length == 1) || delimiter == null)
325N/A return s[0];
325N/A
325N/A StringBuffer r = new StringBuffer(s[0]);
325N/A for (int i = 1; i < s.length; i++) {
325N/A r.append(delimiter);
325N/A r.append(s[i]);
325N/A }
325N/A return r.toString();
325N/A }
325N/A
325N/A /**
325N/A * Change the first header line that matches name
325N/A * to have value, adding a new header if no existing header
325N/A * matches. Remove all matching headers but the first. <p>
325N/A * <p/>
325N/A * Note that RFC822 headers can only contain US-ASCII characters
325N/A *
325N/A * @param name header name
325N/A * @param value header value
325N/A */
325N/A public void setHeader(String name, String value) {
325N/A boolean found = false;
325N/A
325N/A for (int i = 0; i < headers.size(); i++) {
325N/A hdr h = (hdr) headers.get(i);
325N/A if (name.equalsIgnoreCase(h.name)) {
325N/A if (!found) {
325N/A int j;
325N/A if (h.line != null && (j = h.line.indexOf(':')) >= 0) {
325N/A h.line = h.line.substring(0, j + 1) + " " + value;
325N/A } else {
325N/A h.line = name + ": " + value;
325N/A }
325N/A found = true;
325N/A } else {
325N/A headers.remove(i);
325N/A i--; // have to look at i again
325N/A }
325N/A }
325N/A }
325N/A
325N/A if (!found) {
325N/A addHeader(name, value);
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Add a header with the specified name and value to the header list. <p>
325N/A * <p/>
325N/A * Note that RFC822 headers can only contain US-ASCII characters.
325N/A *
325N/A * @param name header name
325N/A * @param value header value
325N/A */
325N/A public void addHeader(String name, String value) {
325N/A int pos = headers.size();
325N/A for (int i = headers.size() - 1; i >= 0; i--) {
325N/A hdr h = (hdr) headers.get(i);
325N/A if (name.equalsIgnoreCase(h.name)) {
325N/A headers.add(i + 1, new hdr(name, value));
325N/A return;
325N/A }
325N/A // marker for default place to add new headers
325N/A if (h.name.equals(":"))
325N/A pos = i;
325N/A }
325N/A headers.add(pos, new hdr(name, value));
325N/A }
325N/A
325N/A /**
325N/A * Remove all header entries that match the given name
325N/A *
325N/A * @param name header name
325N/A */
325N/A public void removeHeader(String name) {
325N/A for (int i = 0; i < headers.size(); i++) {
325N/A hdr h = (hdr) headers.get(i);
325N/A if (name.equalsIgnoreCase(h.name)) {
325N/A headers.remove(i);
325N/A i--; // have to look at i again
325N/A }
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Return all the headers as an Enumeration of
325N/A * {@link Header} objects.
325N/A *
325N/A * @return Header objects
325N/A */
325N/A public FinalArrayList getAllHeaders() {
325N/A return headers; // conceptually it should be read-only, but for performance reason I'm not wrapping it here
325N/A }
325N/A
325N/A /**
325N/A * Add an RFC822 header line to the header store.
325N/A * If the line starts with a space or tab (a continuation line),
325N/A * add it to the last header line in the list. <p>
325N/A * <p/>
325N/A * Note that RFC822 headers can only contain US-ASCII characters
325N/A *
325N/A * @param line raw RFC822 header line
325N/A */
325N/A public void addHeaderLine(String line) {
325N/A try {
325N/A char c = line.charAt(0);
325N/A if (c == ' ' || c == '\t') {
325N/A hdr h = (hdr) headers.get(headers.size() - 1);
325N/A h.line += "\r\n" + line;
325N/A } else
325N/A headers.add(new hdr(line));
325N/A } catch (StringIndexOutOfBoundsException e) {
325N/A // line is empty, ignore it
325N/A return;
325N/A } catch (NoSuchElementException e) {
325N/A // XXX - vector is empty?
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Return all the header lines as a collection
325N/A */
325N/A public List getAllHeaderLines() {
325N/A if(headerValueView==null)
325N/A headerValueView = new AbstractList() {
325N/A public Object get(int index) {
325N/A return ((hdr)headers.get(index)).line;
325N/A }
325N/A
325N/A public int size() {
325N/A return headers.size();
325N/A }
325N/A };
325N/A return headerValueView;
325N/A }
325N/A}
325N/A
325N/A/*
325N/A * A private utility class to represent an individual header.
325N/A */
325N/A
325N/Aclass hdr implements Header {
325N/A // XXX - should these be private?
325N/A String name; // the canonicalized (trimmed) name of this header
325N/A // XXX - should name be stored in lower case?
325N/A String line; // the entire RFC822 header "line"
325N/A
325N/A /*
325N/A * Constructor that takes a line and splits out
325N/A * the header name.
325N/A */
325N/A hdr(String l) {
325N/A int i = l.indexOf(':');
325N/A if (i < 0) {
325N/A // should never happen
325N/A name = l.trim();
325N/A } else {
325N/A name = l.substring(0, i).trim();
325N/A }
325N/A line = l;
325N/A }
325N/A
325N/A /*
325N/A * Constructor that takes a header name and value.
325N/A */
325N/A hdr(String n, String v) {
325N/A name = n;
325N/A line = n + ": " + v;
325N/A }
325N/A
325N/A /*
325N/A * Return the "name" part of the header line.
325N/A */
325N/A public String getName() {
325N/A return name;
325N/A }
325N/A
325N/A /*
325N/A * Return the "value" part of the header line.
325N/A */
325N/A public String getValue() {
325N/A int i = line.indexOf(':');
325N/A if (i < 0)
325N/A return line;
325N/A
325N/A int j;
325N/A if (name.equalsIgnoreCase("Content-Description")) {
325N/A // Content-Description should retain the folded whitespace after header unfolding -
325N/A // rf. RFC2822 section 2.2.3, rf. RFC2822 section 3.2.3
325N/A for (j = i + 1; j < line.length(); j++) {
325N/A char c = line.charAt(j);
325N/A if (!(/*c == ' ' ||*/c == '\t' || c == '\r' || c == '\n'))
325N/A break;
325N/A }
325N/A } else {
325N/A // skip whitespace after ':'
325N/A for (j = i + 1; j < line.length(); j++) {
325N/A char c = line.charAt(j);
325N/A if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n'))
325N/A break;
325N/A }
325N/A }
325N/A return line.substring(j);
325N/A }
325N/A}