src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/InternetHeaders.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 /*
ohair@286 27 * @(#)InternetHeaders.java 1.16 02/08/08
ohair@286 28 */
ohair@286 29
ohair@286 30
ohair@286 31
ohair@286 32 package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
ohair@286 33
ohair@286 34 import com.sun.xml.internal.messaging.saaj.packaging.mime.Header;
ohair@286 35 import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
ohair@286 36 import com.sun.xml.internal.messaging.saaj.packaging.mime.util.LineInputStream;
ohair@286 37 import com.sun.xml.internal.messaging.saaj.util.FinalArrayList;
ohair@286 38
ohair@286 39 import java.io.IOException;
ohair@286 40 import java.io.InputStream;
ohair@286 41 import java.util.AbstractList;
ohair@286 42 import java.util.List;
ohair@286 43 import java.util.NoSuchElementException;
ohair@286 44
ohair@286 45 /**
ohair@286 46 * InternetHeaders is a utility class that manages RFC822 style
ohair@286 47 * headers. Given an RFC822 format message stream, it reads lines
ohair@286 48 * until the blank line that indicates end of header. The input stream
ohair@286 49 * is positioned at the start of the body. The lines are stored
ohair@286 50 * within the object and can be extracted as either Strings or
ohair@286 51 * {@link Header} objects. <p>
ohair@286 52 * <p/>
ohair@286 53 * This class is mostly intended for service providers. MimeMessage
ohair@286 54 * and MimeBody use this class for holding their headers. <p>
ohair@286 55 * <p/>
ohair@286 56 * <hr> <strong>A note on RFC822 and MIME headers</strong><p>
ohair@286 57 * <p/>
ohair@286 58 * RFC822 and MIME header fields <strong>must</strong> contain only
ohair@286 59 * US-ASCII characters. If a header contains non US-ASCII characters,
ohair@286 60 * it must be encoded as per the rules in RFC 2047. The MimeUtility
ohair@286 61 * class provided in this package can be used to to achieve this.
ohair@286 62 * Callers of the <code>setHeader</code>, <code>addHeader</code>, and
ohair@286 63 * <code>addHeaderLine</code> methods are responsible for enforcing
ohair@286 64 * the MIME requirements for the specified headers. In addition, these
ohair@286 65 * header fields must be folded (wrapped) before being sent if they
ohair@286 66 * exceed the line length limitation for the transport (1000 bytes for
ohair@286 67 * SMTP). Received headers may have been folded. The application is
ohair@286 68 * responsible for folding and unfolding headers as appropriate. <p>
ohair@286 69 *
ohair@286 70 * @author John Mani
ohair@286 71 * @author Bill Shannon
ohair@286 72 * @see MimeUtility
ohair@286 73 */
ohair@286 74 public final class InternetHeaders {
ohair@286 75
ohair@286 76 private final FinalArrayList headers = new FinalArrayList();
ohair@286 77
ohair@286 78 /**
ohair@286 79 * Lazily cerated view of header lines (Strings).
ohair@286 80 */
ohair@286 81 private List headerValueView;
ohair@286 82
ohair@286 83 /**
ohair@286 84 * Create an empty InternetHeaders object.
ohair@286 85 */
ohair@286 86 public InternetHeaders() {
ohair@286 87 }
ohair@286 88
ohair@286 89 /**
ohair@286 90 * Read and parse the given RFC822 message stream till the
ohair@286 91 * blank line separating the header from the body. The input
ohair@286 92 * stream is left positioned at the start of the body. The
ohair@286 93 * header lines are stored internally. <p>
ohair@286 94 * <p/>
ohair@286 95 * For efficiency, wrap a BufferedInputStream around the actual
ohair@286 96 * input stream and pass it as the parameter.
ohair@286 97 *
ohair@286 98 * @param is RFC822 input stream
ohair@286 99 */
ohair@286 100 public InternetHeaders(InputStream is) throws MessagingException {
ohair@286 101 load(is);
ohair@286 102 }
ohair@286 103
ohair@286 104 /**
ohair@286 105 * Read and parse the given RFC822 message stream till the
ohair@286 106 * blank line separating the header from the body. Store the
ohair@286 107 * header lines inside this InternetHeaders object. <p>
ohair@286 108 * <p/>
ohair@286 109 * Note that the header lines are added into this InternetHeaders
ohair@286 110 * object, so any existing headers in this object will not be
ohair@286 111 * affected.
ohair@286 112 *
ohair@286 113 * @param is RFC822 input stream
ohair@286 114 */
ohair@286 115 public void load(InputStream is) throws MessagingException {
ohair@286 116 // Read header lines until a blank line. It is valid
ohair@286 117 // to have BodyParts with no header lines.
ohair@286 118 String line;
ohair@286 119 LineInputStream lis = new LineInputStream(is);
ohair@286 120 String prevline = null; // the previous header line, as a string
ohair@286 121 // a buffer to accumulate the header in, when we know it's needed
ohair@286 122 StringBuffer lineBuffer = new StringBuffer();
ohair@286 123
ohair@286 124 try {
ohair@286 125 //while ((line = lis.readLine()) != null) {
ohair@286 126 do {
ohair@286 127 line = lis.readLine();
ohair@286 128 if (line != null &&
ohair@286 129 (line.startsWith(" ") || line.startsWith("\t"))) {
ohair@286 130 // continuation of header
ohair@286 131 if (prevline != null) {
ohair@286 132 lineBuffer.append(prevline);
ohair@286 133 prevline = null;
ohair@286 134 }
ohair@286 135 lineBuffer.append("\r\n");
ohair@286 136 lineBuffer.append(line);
ohair@286 137 } else {
ohair@286 138 // new header
ohair@286 139 if (prevline != null)
ohair@286 140 addHeaderLine(prevline);
ohair@286 141 else if (lineBuffer.length() > 0) {
ohair@286 142 // store previous header first
ohair@286 143 addHeaderLine(lineBuffer.toString());
ohair@286 144 lineBuffer.setLength(0);
ohair@286 145 }
ohair@286 146 prevline = line;
ohair@286 147 }
ohair@286 148 } while (line != null && line.length() > 0);
ohair@286 149 } catch (IOException ioex) {
ohair@286 150 throw new MessagingException("Error in input stream", ioex);
ohair@286 151 }
ohair@286 152 }
ohair@286 153
ohair@286 154 /**
ohair@286 155 * Return all the values for the specified header. The
ohair@286 156 * values are String objects. Returns <code>null</code>
ohair@286 157 * if no headers with the specified name exist.
ohair@286 158 *
ohair@286 159 * @param name header name
ohair@286 160 * @return array of header values, or null if none
ohair@286 161 */
ohair@286 162 public String[] getHeader(String name) {
ohair@286 163 // XXX - should we just step through in index order?
ohair@286 164 FinalArrayList v = new FinalArrayList(); // accumulate return values
ohair@286 165
ohair@286 166 int len = headers.size();
ohair@286 167 for( int i=0; i<len; i++ ) {
ohair@286 168 hdr h = (hdr) headers.get(i);
ohair@286 169 if (name.equalsIgnoreCase(h.name)) {
ohair@286 170 v.add(h.getValue());
ohair@286 171 }
ohair@286 172 }
ohair@286 173 if (v.size() == 0)
ohair@286 174 return (null);
ohair@286 175 // convert Vector to an array for return
ohair@286 176 return (String[]) v.toArray(new String[v.size()]);
ohair@286 177 }
ohair@286 178
ohair@286 179 /**
ohair@286 180 * Get all the headers for this header name, returned as a single
ohair@286 181 * String, with headers separated by the delimiter. If the
ohair@286 182 * delimiter is <code>null</code>, only the first header is
ohair@286 183 * returned. Returns <code>null</code>
ohair@286 184 * if no headers with the specified name exist.
ohair@286 185 *
ohair@286 186 * @param delimiter delimiter
ohair@286 187 * @return the value fields for all headers with
ohair@286 188 * this name, or null if none
ohair@286 189 * @param name header name
ohair@286 190 */
ohair@286 191 public String getHeader(String name, String delimiter) {
ohair@286 192 String[] s = getHeader(name);
ohair@286 193
ohair@286 194 if (s == null)
ohair@286 195 return null;
ohair@286 196
ohair@286 197 if ((s.length == 1) || delimiter == null)
ohair@286 198 return s[0];
ohair@286 199
ohair@286 200 StringBuffer r = new StringBuffer(s[0]);
ohair@286 201 for (int i = 1; i < s.length; i++) {
ohair@286 202 r.append(delimiter);
ohair@286 203 r.append(s[i]);
ohair@286 204 }
ohair@286 205 return r.toString();
ohair@286 206 }
ohair@286 207
ohair@286 208 /**
ohair@286 209 * Change the first header line that matches name
ohair@286 210 * to have value, adding a new header if no existing header
ohair@286 211 * matches. Remove all matching headers but the first. <p>
ohair@286 212 * <p/>
ohair@286 213 * Note that RFC822 headers can only contain US-ASCII characters
ohair@286 214 *
ohair@286 215 * @param name header name
ohair@286 216 * @param value header value
ohair@286 217 */
ohair@286 218 public void setHeader(String name, String value) {
ohair@286 219 boolean found = false;
ohair@286 220
ohair@286 221 for (int i = 0; i < headers.size(); i++) {
ohair@286 222 hdr h = (hdr) headers.get(i);
ohair@286 223 if (name.equalsIgnoreCase(h.name)) {
ohair@286 224 if (!found) {
ohair@286 225 int j;
ohair@286 226 if (h.line != null && (j = h.line.indexOf(':')) >= 0) {
ohair@286 227 h.line = h.line.substring(0, j + 1) + " " + value;
ohair@286 228 } else {
ohair@286 229 h.line = name + ": " + value;
ohair@286 230 }
ohair@286 231 found = true;
ohair@286 232 } else {
ohair@286 233 headers.remove(i);
ohair@286 234 i--; // have to look at i again
ohair@286 235 }
ohair@286 236 }
ohair@286 237 }
ohair@286 238
ohair@286 239 if (!found) {
ohair@286 240 addHeader(name, value);
ohair@286 241 }
ohair@286 242 }
ohair@286 243
ohair@286 244 /**
ohair@286 245 * Add a header with the specified name and value to the header list. <p>
ohair@286 246 * <p/>
ohair@286 247 * Note that RFC822 headers can only contain US-ASCII characters.
ohair@286 248 *
ohair@286 249 * @param name header name
ohair@286 250 * @param value header value
ohair@286 251 */
ohair@286 252 public void addHeader(String name, String value) {
ohair@286 253 int pos = headers.size();
ohair@286 254 for (int i = headers.size() - 1; i >= 0; i--) {
ohair@286 255 hdr h = (hdr) headers.get(i);
ohair@286 256 if (name.equalsIgnoreCase(h.name)) {
ohair@286 257 headers.add(i + 1, new hdr(name, value));
ohair@286 258 return;
ohair@286 259 }
ohair@286 260 // marker for default place to add new headers
ohair@286 261 if (h.name.equals(":"))
ohair@286 262 pos = i;
ohair@286 263 }
ohair@286 264 headers.add(pos, new hdr(name, value));
ohair@286 265 }
ohair@286 266
ohair@286 267 /**
ohair@286 268 * Remove all header entries that match the given name
ohair@286 269 *
ohair@286 270 * @param name header name
ohair@286 271 */
ohair@286 272 public void removeHeader(String name) {
ohair@286 273 for (int i = 0; i < headers.size(); i++) {
ohair@286 274 hdr h = (hdr) headers.get(i);
ohair@286 275 if (name.equalsIgnoreCase(h.name)) {
ohair@286 276 headers.remove(i);
ohair@286 277 i--; // have to look at i again
ohair@286 278 }
ohair@286 279 }
ohair@286 280 }
ohair@286 281
ohair@286 282 /**
ohair@286 283 * Return all the headers as an Enumeration of
ohair@286 284 * {@link Header} objects.
ohair@286 285 *
ohair@286 286 * @return Header objects
ohair@286 287 */
ohair@286 288 public FinalArrayList getAllHeaders() {
ohair@286 289 return headers; // conceptually it should be read-only, but for performance reason I'm not wrapping it here
ohair@286 290 }
ohair@286 291
ohair@286 292 /**
ohair@286 293 * Add an RFC822 header line to the header store.
ohair@286 294 * If the line starts with a space or tab (a continuation line),
ohair@286 295 * add it to the last header line in the list. <p>
ohair@286 296 * <p/>
ohair@286 297 * Note that RFC822 headers can only contain US-ASCII characters
ohair@286 298 *
ohair@286 299 * @param line raw RFC822 header line
ohair@286 300 */
ohair@286 301 public void addHeaderLine(String line) {
ohair@286 302 try {
ohair@286 303 char c = line.charAt(0);
ohair@286 304 if (c == ' ' || c == '\t') {
ohair@286 305 hdr h = (hdr) headers.get(headers.size() - 1);
ohair@286 306 h.line += "\r\n" + line;
ohair@286 307 } else
ohair@286 308 headers.add(new hdr(line));
ohair@286 309 } catch (StringIndexOutOfBoundsException e) {
ohair@286 310 // line is empty, ignore it
ohair@286 311 return;
ohair@286 312 } catch (NoSuchElementException e) {
ohair@286 313 // XXX - vector is empty?
ohair@286 314 }
ohair@286 315 }
ohair@286 316
ohair@286 317 /**
ohair@286 318 * Return all the header lines as a collection
ohair@286 319 */
ohair@286 320 public List getAllHeaderLines() {
ohair@286 321 if(headerValueView==null)
ohair@286 322 headerValueView = new AbstractList() {
ohair@286 323 public Object get(int index) {
ohair@286 324 return ((hdr)headers.get(index)).line;
ohair@286 325 }
ohair@286 326
ohair@286 327 public int size() {
ohair@286 328 return headers.size();
ohair@286 329 }
ohair@286 330 };
ohair@286 331 return headerValueView;
ohair@286 332 }
ohair@286 333 }
ohair@286 334
ohair@286 335 /*
ohair@286 336 * A private utility class to represent an individual header.
ohair@286 337 */
ohair@286 338
ohair@286 339 class hdr implements Header {
ohair@286 340 // XXX - should these be private?
ohair@286 341 String name; // the canonicalized (trimmed) name of this header
ohair@286 342 // XXX - should name be stored in lower case?
ohair@286 343 String line; // the entire RFC822 header "line"
ohair@286 344
ohair@286 345 /*
ohair@286 346 * Constructor that takes a line and splits out
ohair@286 347 * the header name.
ohair@286 348 */
ohair@286 349 hdr(String l) {
ohair@286 350 int i = l.indexOf(':');
ohair@286 351 if (i < 0) {
ohair@286 352 // should never happen
ohair@286 353 name = l.trim();
ohair@286 354 } else {
ohair@286 355 name = l.substring(0, i).trim();
ohair@286 356 }
ohair@286 357 line = l;
ohair@286 358 }
ohair@286 359
ohair@286 360 /*
ohair@286 361 * Constructor that takes a header name and value.
ohair@286 362 */
ohair@286 363 hdr(String n, String v) {
ohair@286 364 name = n;
ohair@286 365 line = n + ": " + v;
ohair@286 366 }
ohair@286 367
ohair@286 368 /*
ohair@286 369 * Return the "name" part of the header line.
ohair@286 370 */
ohair@286 371 public String getName() {
ohair@286 372 return name;
ohair@286 373 }
ohair@286 374
ohair@286 375 /*
ohair@286 376 * Return the "value" part of the header line.
ohair@286 377 */
ohair@286 378 public String getValue() {
ohair@286 379 int i = line.indexOf(':');
ohair@286 380 if (i < 0)
ohair@286 381 return line;
ohair@286 382
ohair@286 383 int j;
ohair@286 384 if (name.equalsIgnoreCase("Content-Description")) {
ohair@286 385 // Content-Description should retain the folded whitespace after header unfolding -
ohair@286 386 // rf. RFC2822 section 2.2.3, rf. RFC2822 section 3.2.3
ohair@286 387 for (j = i + 1; j < line.length(); j++) {
ohair@286 388 char c = line.charAt(j);
ohair@286 389 if (!(/*c == ' ' ||*/c == '\t' || c == '\r' || c == '\n'))
ohair@286 390 break;
ohair@286 391 }
ohair@286 392 } else {
ohair@286 393 // skip whitespace after ':'
ohair@286 394 for (j = i + 1; j < line.length(); j++) {
ohair@286 395 char c = line.charAt(j);
ohair@286 396 if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n'))
ohair@286 397 break;
ohair@286 398 }
ohair@286 399 }
ohair@286 400 return line.substring(j);
ohair@286 401 }
ohair@286 402 }

mercurial