1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/InternetHeaders.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,402 @@ 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 +/* 1.30 + * @(#)InternetHeaders.java 1.16 02/08/08 1.31 + */ 1.32 + 1.33 + 1.34 + 1.35 +package com.sun.xml.internal.messaging.saaj.packaging.mime.internet; 1.36 + 1.37 +import com.sun.xml.internal.messaging.saaj.packaging.mime.Header; 1.38 +import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException; 1.39 +import com.sun.xml.internal.messaging.saaj.packaging.mime.util.LineInputStream; 1.40 +import com.sun.xml.internal.messaging.saaj.util.FinalArrayList; 1.41 + 1.42 +import java.io.IOException; 1.43 +import java.io.InputStream; 1.44 +import java.util.AbstractList; 1.45 +import java.util.List; 1.46 +import java.util.NoSuchElementException; 1.47 + 1.48 +/** 1.49 + * InternetHeaders is a utility class that manages RFC822 style 1.50 + * headers. Given an RFC822 format message stream, it reads lines 1.51 + * until the blank line that indicates end of header. The input stream 1.52 + * is positioned at the start of the body. The lines are stored 1.53 + * within the object and can be extracted as either Strings or 1.54 + * {@link Header} objects. <p> 1.55 + * <p/> 1.56 + * This class is mostly intended for service providers. MimeMessage 1.57 + * and MimeBody use this class for holding their headers. <p> 1.58 + * <p/> 1.59 + * <hr> <strong>A note on RFC822 and MIME headers</strong><p> 1.60 + * <p/> 1.61 + * RFC822 and MIME header fields <strong>must</strong> contain only 1.62 + * US-ASCII characters. If a header contains non US-ASCII characters, 1.63 + * it must be encoded as per the rules in RFC 2047. The MimeUtility 1.64 + * class provided in this package can be used to to achieve this. 1.65 + * Callers of the <code>setHeader</code>, <code>addHeader</code>, and 1.66 + * <code>addHeaderLine</code> methods are responsible for enforcing 1.67 + * the MIME requirements for the specified headers. In addition, these 1.68 + * header fields must be folded (wrapped) before being sent if they 1.69 + * exceed the line length limitation for the transport (1000 bytes for 1.70 + * SMTP). Received headers may have been folded. The application is 1.71 + * responsible for folding and unfolding headers as appropriate. <p> 1.72 + * 1.73 + * @author John Mani 1.74 + * @author Bill Shannon 1.75 + * @see MimeUtility 1.76 + */ 1.77 +public final class InternetHeaders { 1.78 + 1.79 + private final FinalArrayList headers = new FinalArrayList(); 1.80 + 1.81 + /** 1.82 + * Lazily cerated view of header lines (Strings). 1.83 + */ 1.84 + private List headerValueView; 1.85 + 1.86 + /** 1.87 + * Create an empty InternetHeaders object. 1.88 + */ 1.89 + public InternetHeaders() { 1.90 + } 1.91 + 1.92 + /** 1.93 + * Read and parse the given RFC822 message stream till the 1.94 + * blank line separating the header from the body. The input 1.95 + * stream is left positioned at the start of the body. The 1.96 + * header lines are stored internally. <p> 1.97 + * <p/> 1.98 + * For efficiency, wrap a BufferedInputStream around the actual 1.99 + * input stream and pass it as the parameter. 1.100 + * 1.101 + * @param is RFC822 input stream 1.102 + */ 1.103 + public InternetHeaders(InputStream is) throws MessagingException { 1.104 + load(is); 1.105 + } 1.106 + 1.107 + /** 1.108 + * Read and parse the given RFC822 message stream till the 1.109 + * blank line separating the header from the body. Store the 1.110 + * header lines inside this InternetHeaders object. <p> 1.111 + * <p/> 1.112 + * Note that the header lines are added into this InternetHeaders 1.113 + * object, so any existing headers in this object will not be 1.114 + * affected. 1.115 + * 1.116 + * @param is RFC822 input stream 1.117 + */ 1.118 + public void load(InputStream is) throws MessagingException { 1.119 + // Read header lines until a blank line. It is valid 1.120 + // to have BodyParts with no header lines. 1.121 + String line; 1.122 + LineInputStream lis = new LineInputStream(is); 1.123 + String prevline = null; // the previous header line, as a string 1.124 + // a buffer to accumulate the header in, when we know it's needed 1.125 + StringBuffer lineBuffer = new StringBuffer(); 1.126 + 1.127 + try { 1.128 + //while ((line = lis.readLine()) != null) { 1.129 + do { 1.130 + line = lis.readLine(); 1.131 + if (line != null && 1.132 + (line.startsWith(" ") || line.startsWith("\t"))) { 1.133 + // continuation of header 1.134 + if (prevline != null) { 1.135 + lineBuffer.append(prevline); 1.136 + prevline = null; 1.137 + } 1.138 + lineBuffer.append("\r\n"); 1.139 + lineBuffer.append(line); 1.140 + } else { 1.141 + // new header 1.142 + if (prevline != null) 1.143 + addHeaderLine(prevline); 1.144 + else if (lineBuffer.length() > 0) { 1.145 + // store previous header first 1.146 + addHeaderLine(lineBuffer.toString()); 1.147 + lineBuffer.setLength(0); 1.148 + } 1.149 + prevline = line; 1.150 + } 1.151 + } while (line != null && line.length() > 0); 1.152 + } catch (IOException ioex) { 1.153 + throw new MessagingException("Error in input stream", ioex); 1.154 + } 1.155 + } 1.156 + 1.157 + /** 1.158 + * Return all the values for the specified header. The 1.159 + * values are String objects. Returns <code>null</code> 1.160 + * if no headers with the specified name exist. 1.161 + * 1.162 + * @param name header name 1.163 + * @return array of header values, or null if none 1.164 + */ 1.165 + public String[] getHeader(String name) { 1.166 + // XXX - should we just step through in index order? 1.167 + FinalArrayList v = new FinalArrayList(); // accumulate return values 1.168 + 1.169 + int len = headers.size(); 1.170 + for( int i=0; i<len; i++ ) { 1.171 + hdr h = (hdr) headers.get(i); 1.172 + if (name.equalsIgnoreCase(h.name)) { 1.173 + v.add(h.getValue()); 1.174 + } 1.175 + } 1.176 + if (v.size() == 0) 1.177 + return (null); 1.178 + // convert Vector to an array for return 1.179 + return (String[]) v.toArray(new String[v.size()]); 1.180 + } 1.181 + 1.182 + /** 1.183 + * Get all the headers for this header name, returned as a single 1.184 + * String, with headers separated by the delimiter. If the 1.185 + * delimiter is <code>null</code>, only the first header is 1.186 + * returned. Returns <code>null</code> 1.187 + * if no headers with the specified name exist. 1.188 + * 1.189 + * @param delimiter delimiter 1.190 + * @return the value fields for all headers with 1.191 + * this name, or null if none 1.192 + * @param name header name 1.193 + */ 1.194 + public String getHeader(String name, String delimiter) { 1.195 + String[] s = getHeader(name); 1.196 + 1.197 + if (s == null) 1.198 + return null; 1.199 + 1.200 + if ((s.length == 1) || delimiter == null) 1.201 + return s[0]; 1.202 + 1.203 + StringBuffer r = new StringBuffer(s[0]); 1.204 + for (int i = 1; i < s.length; i++) { 1.205 + r.append(delimiter); 1.206 + r.append(s[i]); 1.207 + } 1.208 + return r.toString(); 1.209 + } 1.210 + 1.211 + /** 1.212 + * Change the first header line that matches name 1.213 + * to have value, adding a new header if no existing header 1.214 + * matches. Remove all matching headers but the first. <p> 1.215 + * <p/> 1.216 + * Note that RFC822 headers can only contain US-ASCII characters 1.217 + * 1.218 + * @param name header name 1.219 + * @param value header value 1.220 + */ 1.221 + public void setHeader(String name, String value) { 1.222 + boolean found = false; 1.223 + 1.224 + for (int i = 0; i < headers.size(); i++) { 1.225 + hdr h = (hdr) headers.get(i); 1.226 + if (name.equalsIgnoreCase(h.name)) { 1.227 + if (!found) { 1.228 + int j; 1.229 + if (h.line != null && (j = h.line.indexOf(':')) >= 0) { 1.230 + h.line = h.line.substring(0, j + 1) + " " + value; 1.231 + } else { 1.232 + h.line = name + ": " + value; 1.233 + } 1.234 + found = true; 1.235 + } else { 1.236 + headers.remove(i); 1.237 + i--; // have to look at i again 1.238 + } 1.239 + } 1.240 + } 1.241 + 1.242 + if (!found) { 1.243 + addHeader(name, value); 1.244 + } 1.245 + } 1.246 + 1.247 + /** 1.248 + * Add a header with the specified name and value to the header list. <p> 1.249 + * <p/> 1.250 + * Note that RFC822 headers can only contain US-ASCII characters. 1.251 + * 1.252 + * @param name header name 1.253 + * @param value header value 1.254 + */ 1.255 + public void addHeader(String name, String value) { 1.256 + int pos = headers.size(); 1.257 + for (int i = headers.size() - 1; i >= 0; i--) { 1.258 + hdr h = (hdr) headers.get(i); 1.259 + if (name.equalsIgnoreCase(h.name)) { 1.260 + headers.add(i + 1, new hdr(name, value)); 1.261 + return; 1.262 + } 1.263 + // marker for default place to add new headers 1.264 + if (h.name.equals(":")) 1.265 + pos = i; 1.266 + } 1.267 + headers.add(pos, new hdr(name, value)); 1.268 + } 1.269 + 1.270 + /** 1.271 + * Remove all header entries that match the given name 1.272 + * 1.273 + * @param name header name 1.274 + */ 1.275 + public void removeHeader(String name) { 1.276 + for (int i = 0; i < headers.size(); i++) { 1.277 + hdr h = (hdr) headers.get(i); 1.278 + if (name.equalsIgnoreCase(h.name)) { 1.279 + headers.remove(i); 1.280 + i--; // have to look at i again 1.281 + } 1.282 + } 1.283 + } 1.284 + 1.285 + /** 1.286 + * Return all the headers as an Enumeration of 1.287 + * {@link Header} objects. 1.288 + * 1.289 + * @return Header objects 1.290 + */ 1.291 + public FinalArrayList getAllHeaders() { 1.292 + return headers; // conceptually it should be read-only, but for performance reason I'm not wrapping it here 1.293 + } 1.294 + 1.295 + /** 1.296 + * Add an RFC822 header line to the header store. 1.297 + * If the line starts with a space or tab (a continuation line), 1.298 + * add it to the last header line in the list. <p> 1.299 + * <p/> 1.300 + * Note that RFC822 headers can only contain US-ASCII characters 1.301 + * 1.302 + * @param line raw RFC822 header line 1.303 + */ 1.304 + public void addHeaderLine(String line) { 1.305 + try { 1.306 + char c = line.charAt(0); 1.307 + if (c == ' ' || c == '\t') { 1.308 + hdr h = (hdr) headers.get(headers.size() - 1); 1.309 + h.line += "\r\n" + line; 1.310 + } else 1.311 + headers.add(new hdr(line)); 1.312 + } catch (StringIndexOutOfBoundsException e) { 1.313 + // line is empty, ignore it 1.314 + return; 1.315 + } catch (NoSuchElementException e) { 1.316 + // XXX - vector is empty? 1.317 + } 1.318 + } 1.319 + 1.320 + /** 1.321 + * Return all the header lines as a collection 1.322 + */ 1.323 + public List getAllHeaderLines() { 1.324 + if(headerValueView==null) 1.325 + headerValueView = new AbstractList() { 1.326 + public Object get(int index) { 1.327 + return ((hdr)headers.get(index)).line; 1.328 + } 1.329 + 1.330 + public int size() { 1.331 + return headers.size(); 1.332 + } 1.333 + }; 1.334 + return headerValueView; 1.335 + } 1.336 +} 1.337 + 1.338 +/* 1.339 + * A private utility class to represent an individual header. 1.340 + */ 1.341 + 1.342 +class hdr implements Header { 1.343 + // XXX - should these be private? 1.344 + String name; // the canonicalized (trimmed) name of this header 1.345 + // XXX - should name be stored in lower case? 1.346 + String line; // the entire RFC822 header "line" 1.347 + 1.348 + /* 1.349 + * Constructor that takes a line and splits out 1.350 + * the header name. 1.351 + */ 1.352 + hdr(String l) { 1.353 + int i = l.indexOf(':'); 1.354 + if (i < 0) { 1.355 + // should never happen 1.356 + name = l.trim(); 1.357 + } else { 1.358 + name = l.substring(0, i).trim(); 1.359 + } 1.360 + line = l; 1.361 + } 1.362 + 1.363 + /* 1.364 + * Constructor that takes a header name and value. 1.365 + */ 1.366 + hdr(String n, String v) { 1.367 + name = n; 1.368 + line = n + ": " + v; 1.369 + } 1.370 + 1.371 + /* 1.372 + * Return the "name" part of the header line. 1.373 + */ 1.374 + public String getName() { 1.375 + return name; 1.376 + } 1.377 + 1.378 + /* 1.379 + * Return the "value" part of the header line. 1.380 + */ 1.381 + public String getValue() { 1.382 + int i = line.indexOf(':'); 1.383 + if (i < 0) 1.384 + return line; 1.385 + 1.386 + int j; 1.387 + if (name.equalsIgnoreCase("Content-Description")) { 1.388 + // Content-Description should retain the folded whitespace after header unfolding - 1.389 + // rf. RFC2822 section 2.2.3, rf. RFC2822 section 3.2.3 1.390 + for (j = i + 1; j < line.length(); j++) { 1.391 + char c = line.charAt(j); 1.392 + if (!(/*c == ' ' ||*/c == '\t' || c == '\r' || c == '\n')) 1.393 + break; 1.394 + } 1.395 + } else { 1.396 + // skip whitespace after ':' 1.397 + for (j = i + 1; j < line.length(); j++) { 1.398 + char c = line.charAt(j); 1.399 + if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n')) 1.400 + break; 1.401 + } 1.402 + } 1.403 + return line.substring(j); 1.404 + } 1.405 +}