Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.ws.transport;
28 import java.io.Serializable;
29 import java.util.Comparator;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.TreeMap;
35 /**
36 * HTTP request and response headers are represented by this class which implements
37 * the interface {@link java.util.Map}<{@link String},
38 * {@link List}<{@link String}>>.
39 * The keys are case-insensitive Strings representing the header names and
40 * the value associated with each key is a {@link List}<{@link String}> with one
41 * element for each occurrence of the header name in the request or response.
42 * <p>
43 * For example, if the request has the the following headers:
44 * <blockquote><pre>
45 * HeaderName: value1
46 * HeadernaMe: value2
47 * </blockquote></pre>
48 * Then get("hEaDeRnAmE") would give both "value1", and "value2" values in a list
49 * <p>
50 * All the normal {@link Map} methods are provided, but the following
51 * additional convenience methods are most likely to be used:
52 * <ul>
53 * <li>{@link #getFirst(String)} returns a single valued header or the first
54 * value of a multi-valued header.</li>
55 * <li>{@link #add(String,String)} adds the given header value to the list
56 * for the given key</li>
57 * <li>{@link #set(String,String)} sets the given header field to the single
58 * value given overwriting any existing values in the value list.
59 * </ul><p>
60 * All methods in this class accept <code>null</code> values for keys and values.
61 * However, null keys will never will be present in HTTP request headers, and
62 * will not be output/sent in response headers. Null values can be represented
63 * as either a null entry for the key (i.e. the list is null) or where the key
64 * has a list, but one (or more) of the list's values is null. Null values are
65 * output as a header line containing the key but no associated value.
66 *
67 * @author Jitendra Kotamraju
68 */
69 public class Headers extends TreeMap<String,List<String>> {
71 public Headers() {
72 super(INSTANCE);
73 }
75 private static final InsensitiveComparator INSTANCE = new InsensitiveComparator();
77 // case-insensitive string comparison of HTTP header names.
78 private static final class InsensitiveComparator implements Comparator<String>, Serializable {
79 public int compare(String o1, String o2) {
80 if (o1 == null && o2 == null)
81 return 0;
82 if (o1 == null)
83 return -1;
84 if (o2 == null)
85 return 1;
86 return o1.compareToIgnoreCase(o2);
87 }
88 }
90 /**
91 * Adds the given value to the list of headers for the given key. If the
92 * mapping does not already exist, then it is created.
93 *
94 * @param key the header name
95 * @param value the header value to add to the header
96 */
97 public void add (String key, String value) {
98 List<String> list = this.get(key);
99 if (list == null) {
100 list = new LinkedList<String>();
101 put(key,list);
102 }
103 list.add (value);
104 }
106 /**
107 * Returns the first value from the List of String values for the given key
108 * (if at least one exists).
109 *
110 * @param key the key to search for
111 * @return the first string value associated with the key
112 */
113 public String getFirst (String key) {
114 List<String> l = get(key);
115 return (l == null) ? null : l.get(0);
116 }
118 /**
119 * Sets the given value as the sole header value for the given key. If the
120 * mapping does not already exist, then it is created.
121 *
122 * @param key the header name
123 * @param value the header value to set.
124 */
125 public void set (String key, String value) {
126 LinkedList<String> l = new LinkedList<String>();
127 l.add (value);
128 put(key, l);
129 }
131 }