Showing the changes to java/pkg/com/sun/net/httpserver/Headers.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,183 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2005, 2010, 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 | */ | ||
| 25 | |||
| 26 | package com.sun.net.httpserver; | ||
| 27 | |||
| 28 | import java.util.*; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * HTTP request and response headers are represented by this class which implements | ||
| 32 | * the interface {@link java.util.Map}< | ||
| 33 | * {@link java.lang.String},{@link java.util.List}<{@link java.lang.String}>>. | ||
| 34 | * The keys are case-insensitive Strings representing the header names and | ||
| 35 | * the value associated with each key is a {@link List}<{@link String}> with one | ||
| 36 | * element for each occurence of the header name in the request or response. | ||
| 37 | * <p> | ||
| 38 | * For example, if a response header instance contains one key "HeaderName" with two values "value1 and value2" | ||
| 39 | * then this object is output as two header lines: | ||
| 40 | * <blockquote><pre> | ||
| 41 | * HeaderName: value1 | ||
| 42 | * HeaderName: value2 | ||
| 43 | * </blockquote></pre> | ||
| 44 | * <p> | ||
| 45 | * All the normal {@link java.util.Map} methods are provided, but the following | ||
| 46 | * additional convenience methods are most likely to be used: | ||
| 47 | * <ul> | ||
| 48 | * <li>{@link #getFirst(String)} returns a single valued header or the first value of | ||
| 49 | * a multi-valued header.</li> | ||
| 50 | * <li>{@link #add(String,String)} adds the given header value to the list for the given key</li> | ||
| 51 | * <li>{@link #set(String,String)} sets the given header field to the single value given | ||
| 52 | * overwriting any existing values in the value list. | ||
| 53 | * </ul><p> | ||
| 54 | * All methods in this class accept <code>null</code> values for keys and values. However, null | ||
| 55 | * keys will never will be present in HTTP request headers, and will not be output/sent in response headers. | ||
| 56 | * Null values can be represented as either a null entry for the key (i.e. the list is null) or | ||
| 57 | * where the key has a list, but one (or more) of the list's values is null. Null values are output | ||
| 58 | * as a header line containing the key but no associated value. | ||
| 59 | * @since 1.6 | ||
| 60 | */ | ||
| 61 | public class Headers implements Map<String,List<String>> { | ||
| 62 | |||
| 63 | HashMap<String,List<String>> map; | ||
| 64 | |||
| 65 | public Headers () {map = new HashMap<String,List<String>>(32);} | ||
| 66 | |||
| 67 | /* Normalize the key by converting to following form. | ||
| 68 | * First char upper case, rest lower case. | ||
| 69 | * key is presumed to be ASCII | ||
| 70 | */ | ||
| 71 | private String normalize (String key) { | ||
| 72 | if (key == null) { | ||
| 73 | return null; | ||
| 74 | } | ||
| 75 | int len = key.length(); | ||
| 76 | if (len == 0) { | ||
| 77 | return key; | ||
| 78 | } | ||
| 79 | char[] b = key.toCharArray(); | ||
| 80 | if (b[0] >= 'a' && b[0] <= 'z') { | ||
| 81 | b[0] = (char)(b[0] - ('a' - 'A')); | ||
| 82 | } | ||
| 83 | for (int i=1; i<len; i++) { | ||
| 84 | if (b[i] >= 'A' && b[i] <= 'Z') { | ||
| 85 | b[i] = (char) (b[i] + ('a' - 'A')); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | return new String(b); | ||
| 89 | } | ||
| 90 | |||
| 91 | public int size() {return map.size();} | ||
| 92 | |||
| 93 | public boolean isEmpty() {return map.isEmpty();} | ||
| 94 | |||
| 95 | public boolean containsKey(Object key) { | ||
| 96 | if (key == null) { | ||
| 97 | return false; | ||
| 98 | } | ||
| 99 | if (!(key instanceof String)) { | ||
| 100 | return false; | ||
| 101 | } | ||
| 102 | return map.containsKey (normalize((String)key)); | ||
| 103 | } | ||
| 104 | |||
| 105 | public boolean containsValue(Object value) { | ||
| 106 | return map.containsValue(value); | ||
| 107 | } | ||
| 108 | |||
| 109 | public List<String> get(Object key) { | ||
| 110 | return map.get(normalize((String)key)); | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * returns the first value from the List of String values | ||
| 115 | * for the given key (if at least one exists). | ||
| 116 | * @param key the key to search for | ||
| 117 | * @return the first string value associated with the key | ||
| 118 | */ | ||
| 119 | public String getFirst (String key) { | ||
| 120 | List<String> l = map.get(normalize((String)key)); | ||
| 121 | if (l == null) { | ||
| 122 | return null; | ||
| 123 | } | ||
| 124 | return l.get(0); | ||
| 125 | } | ||
| 126 | |||
| 127 | public List<String> put(String key, List<String> value) { | ||
| 128 | return map.put (normalize(key), value); | ||
| 129 | } | ||
| 130 | |||
| 131 | /** | ||
| 132 | * adds the given value to the list of headers | ||
| 133 | * for the given key. If the mapping does not | ||
| 134 | * already exist, then it is created | ||
| 135 | * @param key the header name | ||
| 136 | * @param value the header value to add to the header | ||
| 137 | */ | ||
| 138 | public void add (String key, String value) { | ||
| 139 | String k = normalize(key); | ||
| 140 | List<String> l = map.get(k); | ||
| 141 | if (l == null) { | ||
| 142 | l = new LinkedList<String>(); | ||
| 143 | map.put(k,l); | ||
| 144 | } | ||
| 145 | l.add (value); | ||
| 146 | } | ||
| 147 | |||
| 148 | /** | ||
| 149 | * sets the given value as the sole header value | ||
| 150 | * for the given key. If the mapping does not | ||
| 151 | * already exist, then it is created | ||
| 152 | * @param key the header name | ||
| 153 | * @param value the header value to set. | ||
| 154 | */ | ||
| 155 | public void set (String key, String value) { | ||
| 156 | LinkedList<String> l = new LinkedList<String>(); | ||
| 157 | l.add (value); | ||
| 158 | put (key, l); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | public List<String> remove(Object key) { | ||
| 163 | return map.remove(normalize((String)key)); | ||
| 164 | } | ||
| 165 | |||
| 166 | public void putAll(Map<? extends String,? extends List<String>> t) { | ||
| 167 | map.putAll (t); | ||
| 168 | } | ||
| 169 | |||
| 170 | public void clear() {map.clear();} | ||
| 171 | |||
| 172 | public Set<String> keySet() {return map.keySet();} | ||
| 173 | |||
| 174 | public Collection<List<String>> values() {return map.values();} | ||
| 175 | |||
| 176 | public Set<Map.Entry<String, List<String>>> entrySet() { | ||
| 177 | return map.entrySet(); | ||
| 178 | } | ||
| 179 | |||
| 180 | public boolean equals(Object o) {return map.equals(o);} | ||
| 181 | |||
| 182 | public int hashCode() {return map.hashCode();} | ||
| 183 | } | ||