jdk/src/share/classes/com/sun/net/httpserver/Headers.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6906 d57020b6118f
child 10596 39b3a979e600
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6906
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * HTTP request and response headers are represented by this class which implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * the interface {@link java.util.Map}<
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * {@link java.lang.String},{@link java.util.List}<{@link java.lang.String}>>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The keys are case-insensitive Strings representing the header names and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the value associated with each key is a {@link List}<{@link String}> with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * element for each occurence of the header name in the request or response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * For example, if a response header instance contains one key "HeaderName" with two values "value1 and value2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * then this object is output as two header lines:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * HeaderName: value1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * HeaderName: value2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * </blockquote></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * All the normal {@link java.util.Map} methods are provided, but the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * additional convenience methods are most likely to be used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <li>{@link #getFirst(String)} returns a single valued header or the first value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a multi-valued header.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li>{@link #add(String,String)} adds the given header value to the list for the given key</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li>{@link #set(String,String)} sets the given header field to the single value given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * overwriting any existing values in the value list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </ul><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * All methods in this class accept <code>null</code> values for keys and values. However, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * keys will never will be present in HTTP request headers, and will not be output/sent in response headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Null values can be represented as either a null entry for the key (i.e. the list is null) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * where the key has a list, but one (or more) of the list's values is null. Null values are output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * as a header line containing the key but no associated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class Headers implements Map<String,List<String>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        HashMap<String,List<String>> map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        public Headers () {map = new HashMap<String,List<String>>(32);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        /* Normalize the key by converting to following form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         * First char upper case, rest lower case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         * key is presumed to be ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        private String normalize (String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            int len = key.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
6906
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    79
            char[] b = key.toCharArray();
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    80
            if (b[0] >= 'a' && b[0] <= 'z') {
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    81
                b[0] = (char)(b[0] - ('a' - 'A'));
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    82
            }
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    83
            for (int i=1; i<len; i++) {
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    84
                if (b[i] >= 'A' && b[i] <= 'Z') {
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    85
                    b[i] = (char) (b[i] + ('a' - 'A'));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
6906
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    87
            }
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    88
            return new String(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        public int size() {return map.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public boolean isEmpty() {return map.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (!(key instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return map.containsKey (normalize((String)key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return map.containsValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        public List<String> get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return map.get(normalize((String)key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * returns the first value from the List of String values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * for the given key (if at least one exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * @param key the key to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * @return the first string value associated with the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public String getFirst (String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            List<String> l = map.get(normalize((String)key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return l.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public List<String> put(String key, List<String> value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return map.put (normalize(key), value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * adds the given value to the list of headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * for the given key. If the mapping does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * already exist, then it is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * @param key the header name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * @param value the header value to add to the header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        public void add (String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            String k = normalize(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            List<String> l = map.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                l = new LinkedList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                map.put(k,l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            l.add (value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         * sets the given value as the sole header value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * for the given key. If the mapping does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * already exist, then it is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * @param key the header name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * @param value the header value to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public void set (String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            LinkedList<String> l = new LinkedList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            l.add (value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            put (key, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public List<String> remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return map.remove(normalize((String)key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public void putAll(Map<? extends String,? extends List<String>> t)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            map.putAll (t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public void clear() {map.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        public Set<String> keySet() {return map.keySet();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public Collection<List<String>> values() {return map.values();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        public Set<Map.Entry<String, List<String>>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return map.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        public boolean equals(Object o) {return map.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public int hashCode() {return map.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }