src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/InternetHeaders.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/InternetHeaders.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,239 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.org.jvnet.mimepull;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.util.NoSuchElementException;
    1.33 +import java.util.List;
    1.34 +
    1.35 +/**
    1.36 + * InternetHeaders is a utility class that manages RFC822 style
    1.37 + * headers. Given an RFC822 format message stream, it reads lines
    1.38 + * until the blank line that indicates end of header. The input stream
    1.39 + * is positioned at the start of the body. The lines are stored
    1.40 + * within the object and can be extracted as either Strings or
    1.41 + * {@link Header} objects. <p>
    1.42 + * <p/>
    1.43 + * This class is mostly intended for service providers. MimeMessage
    1.44 + * and MimeBody use this class for holding their headers. <p>
    1.45 + * <p/>
    1.46 + * <hr> <strong>A note on RFC822 and MIME headers</strong><p>
    1.47 + * <p/>
    1.48 + * RFC822 and MIME header fields <strong>must</strong> contain only
    1.49 + * US-ASCII characters. If a header contains non US-ASCII characters,
    1.50 + * it must be encoded as per the rules in RFC 2047. The MimeUtility
    1.51 + * class provided in this package can be used to to achieve this.
    1.52 + * Callers of the <code>setHeader</code>, <code>addHeader</code>, and
    1.53 + * <code>addHeaderLine</code> methods are responsible for enforcing
    1.54 + * the MIME requirements for the specified headers.  In addition, these
    1.55 + * header fields must be folded (wrapped) before being sent if they
    1.56 + * exceed the line length limitation for the transport (1000 bytes for
    1.57 + * SMTP).  Received headers may have been folded.  The application is
    1.58 + * responsible for folding and unfolding headers as appropriate. <p>
    1.59 + *
    1.60 + * @author John Mani
    1.61 + * @author Bill Shannon
    1.62 + */
    1.63 +final class InternetHeaders {
    1.64 +
    1.65 +    private final FinalArrayList<hdr> headers = new FinalArrayList<hdr>();
    1.66 +
    1.67 +    /**
    1.68 +     * Read and parse the given RFC822 message stream till the
    1.69 +     * blank line separating the header from the body. Store the
    1.70 +     * header lines inside this InternetHeaders object. <p>
    1.71 +     * <p/>
    1.72 +     * Note that the header lines are added into this InternetHeaders
    1.73 +     * object, so any existing headers in this object will not be
    1.74 +     * affected.
    1.75 +     *
    1.76 +     * @param   lis RFC822 input stream
    1.77 +     */
    1.78 +    InternetHeaders(MIMEParser.LineInputStream lis) {
    1.79 +        // Read header lines until a blank line. It is valid
    1.80 +        // to have BodyParts with no header lines.
    1.81 +        String line;
    1.82 +        String prevline = null; // the previous header line, as a string
    1.83 +        // a buffer to accumulate the header in, when we know it's needed
    1.84 +        StringBuffer lineBuffer = new StringBuffer();
    1.85 +
    1.86 +        try {
    1.87 +            //while ((line = lis.readLine()) != null) {
    1.88 +            do {
    1.89 +                line = lis.readLine();
    1.90 +                if (line != null &&
    1.91 +                        (line.startsWith(" ") || line.startsWith("\t"))) {
    1.92 +                    // continuation of header
    1.93 +                    if (prevline != null) {
    1.94 +                        lineBuffer.append(prevline);
    1.95 +                        prevline = null;
    1.96 +                    }
    1.97 +                    lineBuffer.append("\r\n");
    1.98 +                    lineBuffer.append(line);
    1.99 +                } else {
   1.100 +                    // new header
   1.101 +                    if (prevline != null)
   1.102 +                        addHeaderLine(prevline);
   1.103 +                    else if (lineBuffer.length() > 0) {
   1.104 +                        // store previous header first
   1.105 +                        addHeaderLine(lineBuffer.toString());
   1.106 +                        lineBuffer.setLength(0);
   1.107 +                    }
   1.108 +                    prevline = line;
   1.109 +                }
   1.110 +            } while (line != null && line.length() > 0);
   1.111 +        } catch (IOException ioex) {
   1.112 +            throw new MIMEParsingException("Error in input stream", ioex);
   1.113 +        }
   1.114 +    }
   1.115 +
   1.116 +    /**
   1.117 +     * Return all the values for the specified header. The
   1.118 +     * values are String objects.  Returns <code>null</code>
   1.119 +     * if no headers with the specified name exist.
   1.120 +     *
   1.121 +     * @param   name header name
   1.122 +     * @return          array of header values, or null if none
   1.123 +     */
   1.124 +    List<String> getHeader(String name) {
   1.125 +        // XXX - should we just step through in index order?
   1.126 +        FinalArrayList<String> v = new FinalArrayList<String>(); // accumulate return values
   1.127 +
   1.128 +        int len = headers.size();
   1.129 +        for( int i=0; i<len; i++ ) {
   1.130 +            hdr h = (hdr) headers.get(i);
   1.131 +            if (name.equalsIgnoreCase(h.name)) {
   1.132 +                v.add(h.getValue());
   1.133 +            }
   1.134 +        }
   1.135 +        return (v.size() == 0) ? null : v;
   1.136 +    }
   1.137 +
   1.138 +    /**
   1.139 +     * Return all the headers as an Enumeration of
   1.140 +     * {@link Header} objects.
   1.141 +     *
   1.142 +     * @return  Header objects
   1.143 +     */
   1.144 +    FinalArrayList<? extends Header> getAllHeaders() {
   1.145 +        return headers; // conceptually it should be read-only, but for performance reason I'm not wrapping it here
   1.146 +    }
   1.147 +
   1.148 +    /**
   1.149 +     * Add an RFC822 header line to the header store.
   1.150 +     * If the line starts with a space or tab (a continuation line),
   1.151 +     * add it to the last header line in the list. <p>
   1.152 +     * <p/>
   1.153 +     * Note that RFC822 headers can only contain US-ASCII characters
   1.154 +     *
   1.155 +     * @param   line    raw RFC822 header line
   1.156 +     */
   1.157 +    void addHeaderLine(String line) {
   1.158 +        try {
   1.159 +            char c = line.charAt(0);
   1.160 +            if (c == ' ' || c == '\t') {
   1.161 +                hdr h = (hdr) headers.get(headers.size() - 1);
   1.162 +                h.line += "\r\n" + line;
   1.163 +            } else
   1.164 +                headers.add(new hdr(line));
   1.165 +        } catch (StringIndexOutOfBoundsException e) {
   1.166 +            // line is empty, ignore it
   1.167 +            return;
   1.168 +        } catch (NoSuchElementException e) {
   1.169 +            // XXX - vector is empty?
   1.170 +        }
   1.171 +    }
   1.172 +
   1.173 +}
   1.174 +
   1.175 +/*
   1.176 + * A private utility class to represent an individual header.
   1.177 + */
   1.178 +
   1.179 +class hdr implements Header {
   1.180 +
   1.181 +    String name;    // the canonicalized (trimmed) name of this header
   1.182 +    // XXX - should name be stored in lower case?
   1.183 +    String line;    // the entire RFC822 header "line"
   1.184 +
   1.185 +    /*
   1.186 +     * Constructor that takes a line and splits out
   1.187 +     * the header name.
   1.188 +     */
   1.189 +    hdr(String l) {
   1.190 +        int i = l.indexOf(':');
   1.191 +        if (i < 0) {
   1.192 +            // should never happen
   1.193 +            name = l.trim();
   1.194 +        } else {
   1.195 +            name = l.substring(0, i).trim();
   1.196 +        }
   1.197 +        line = l;
   1.198 +    }
   1.199 +
   1.200 +    /*
   1.201 +     * Constructor that takes a header name and value.
   1.202 +     */
   1.203 +    hdr(String n, String v) {
   1.204 +        name = n;
   1.205 +        line = n + ": " + v;
   1.206 +    }
   1.207 +
   1.208 +    /*
   1.209 +     * Return the "name" part of the header line.
   1.210 +     */
   1.211 +    public String getName() {
   1.212 +        return name;
   1.213 +    }
   1.214 +
   1.215 +    /*
   1.216 +     * Return the "value" part of the header line.
   1.217 +     */
   1.218 +    public String getValue() {
   1.219 +        int i = line.indexOf(':');
   1.220 +        if (i < 0)
   1.221 +            return line;
   1.222 +
   1.223 +        int j;
   1.224 +        if (name.equalsIgnoreCase("Content-Description")) {
   1.225 +            // Content-Description should retain the folded whitespace after header unfolding -
   1.226 +            // rf. RFC2822 section 2.2.3, rf. RFC2822 section 3.2.3
   1.227 +            for (j = i + 1; j < line.length(); j++) {
   1.228 +                char c = line.charAt(j);
   1.229 +                if (!(/*c == ' ' ||*/c == '\t' || c == '\r' || c == '\n'))
   1.230 +                    break;
   1.231 +            }
   1.232 +        } else {
   1.233 +            // skip whitespace after ':'
   1.234 +            for (j = i + 1; j < line.length(); j++) {
   1.235 +                char c = line.charAt(j);
   1.236 +                if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n'))
   1.237 +                    break;
   1.238 +            }
   1.239 +        }
   1.240 +        return line.substring(j);
   1.241 +    }
   1.242 +}

mercurial