src/share/jaxws_classes/com/sun/xml/internal/ws/transport/Headers.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, 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 package com.sun.xml.internal.ws.transport;
ohair@286 27
alanb@368 28 import java.io.Serializable;
ohair@286 29 import java.util.Comparator;
ohair@286 30 import java.util.LinkedList;
ohair@286 31 import java.util.List;
ohair@286 32 import java.util.Map;
ohair@286 33 import java.util.TreeMap;
ohair@286 34
ohair@286 35 /**
ohair@286 36 * HTTP request and response headers are represented by this class which implements
ohair@286 37 * the interface {@link java.util.Map}<{@link String},
ohair@286 38 * {@link List}<{@link String}>>.
ohair@286 39 * The keys are case-insensitive Strings representing the header names and
ohair@286 40 * the value associated with each key is a {@link List}<{@link String}> with one
ohair@286 41 * element for each occurrence of the header name in the request or response.
ohair@286 42 * <p>
ohair@286 43 * For example, if the request has the the following headers:
ohair@286 44 * <blockquote><pre>
ohair@286 45 * HeaderName: value1
ohair@286 46 * HeadernaMe: value2
ohair@286 47 * </blockquote></pre>
ohair@286 48 * Then get("hEaDeRnAmE") would give both "value1", and "value2" values in a list
ohair@286 49 * <p>
ohair@286 50 * All the normal {@link Map} methods are provided, but the following
ohair@286 51 * additional convenience methods are most likely to be used:
ohair@286 52 * <ul>
ohair@286 53 * <li>{@link #getFirst(String)} returns a single valued header or the first
ohair@286 54 * value of a multi-valued header.</li>
ohair@286 55 * <li>{@link #add(String,String)} adds the given header value to the list
ohair@286 56 * for the given key</li>
ohair@286 57 * <li>{@link #set(String,String)} sets the given header field to the single
ohair@286 58 * value given overwriting any existing values in the value list.
ohair@286 59 * </ul><p>
ohair@286 60 * All methods in this class accept <code>null</code> values for keys and values.
ohair@286 61 * However, null keys will never will be present in HTTP request headers, and
ohair@286 62 * will not be output/sent in response headers. Null values can be represented
ohair@286 63 * as either a null entry for the key (i.e. the list is null) or where the key
ohair@286 64 * has a list, but one (or more) of the list's values is null. Null values are
ohair@286 65 * output as a header line containing the key but no associated value.
ohair@286 66 *
ohair@286 67 * @author Jitendra Kotamraju
ohair@286 68 */
ohair@286 69 public class Headers extends TreeMap<String,List<String>> {
ohair@286 70
ohair@286 71 public Headers() {
ohair@286 72 super(INSTANCE);
ohair@286 73 }
ohair@286 74
ohair@286 75 private static final InsensitiveComparator INSTANCE = new InsensitiveComparator();
ohair@286 76
ohair@286 77 // case-insensitive string comparison of HTTP header names.
alanb@368 78 private static final class InsensitiveComparator implements Comparator<String>, Serializable {
ohair@286 79 public int compare(String o1, String o2) {
ohair@286 80 if (o1 == null && o2 == null)
ohair@286 81 return 0;
ohair@286 82 if (o1 == null)
ohair@286 83 return -1;
ohair@286 84 if (o2 == null)
ohair@286 85 return 1;
ohair@286 86 return o1.compareToIgnoreCase(o2);
ohair@286 87 }
ohair@286 88 }
ohair@286 89
ohair@286 90 /**
ohair@286 91 * Adds the given value to the list of headers for the given key. If the
ohair@286 92 * mapping does not already exist, then it is created.
ohair@286 93 *
ohair@286 94 * @param key the header name
ohair@286 95 * @param value the header value to add to the header
ohair@286 96 */
ohair@286 97 public void add (String key, String value) {
ohair@286 98 List<String> list = this.get(key);
ohair@286 99 if (list == null) {
ohair@286 100 list = new LinkedList<String>();
ohair@286 101 put(key,list);
ohair@286 102 }
ohair@286 103 list.add (value);
ohair@286 104 }
ohair@286 105
ohair@286 106 /**
ohair@286 107 * Returns the first value from the List of String values for the given key
ohair@286 108 * (if at least one exists).
ohair@286 109 *
ohair@286 110 * @param key the key to search for
ohair@286 111 * @return the first string value associated with the key
ohair@286 112 */
ohair@286 113 public String getFirst (String key) {
ohair@286 114 List<String> l = get(key);
ohair@286 115 return (l == null) ? null : l.get(0);
ohair@286 116 }
ohair@286 117
ohair@286 118 /**
ohair@286 119 * Sets the given value as the sole header value for the given key. If the
ohair@286 120 * mapping does not already exist, then it is created.
ohair@286 121 *
ohair@286 122 * @param key the header name
ohair@286 123 * @param value the header value to set.
ohair@286 124 */
ohair@286 125 public void set (String key, String value) {
ohair@286 126 LinkedList<String> l = new LinkedList<String>();
ohair@286 127 l.add (value);
ohair@286 128 put(key, l);
ohair@286 129 }
ohair@286 130
ohair@286 131 }

mercurial