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

mercurial