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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial