< prev index next >
1 /*
2 * Copyright (c) 2015, 2016, 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 */
24 package java.net.http;
25
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Set;
33
34 /**
35 * Implementation of HttpHeaders.
36 */
37 class HttpHeadersImpl implements HttpHeaders {
38 // key is lowercase
39 private final HashMap<String,List<String>> headers;
40
41 public HttpHeadersImpl() {
42 headers = new HashMap<>();
43 }
44
45 @Override
46 public Optional<String> firstValue(String name) {
47 name = name.toLowerCase();
48 List<String> l = headers.get(name);
49 return Optional.ofNullable(l == null ? null : l.get(0));
50 }
51
52 @Override
53 public List<String> allValues(String name) {
54 name = name.toLowerCase();
55 return headers.get(name);
56 }
57
58 @Override
59 public Map<String, List<String>> map() {
60 return Collections.unmodifiableMap(headers);
61 }
62
63 Map<String, List<String>> directMap() {
64 return headers;
65 }
66
67 // package private mutators
68
69 public HttpHeadersImpl deepCopy() {
70 HttpHeadersImpl h1 = new HttpHeadersImpl();
71 HashMap<String,List<String>> headers1 = h1.headers;
72 Set<String> keys = headers.keySet();
73 for (String key : keys) {
74 List<String> vals = headers.get(key);
75 LinkedList<String> vals1 = new LinkedList<>(vals);
76 headers1.put(key, vals1);
77 }
78 return h1;
79 }
80
81 private List<String> getOrCreate(String name) {
82 List<String> l = headers.get(name);
83 if (l == null) {
84 l = new LinkedList<>();
85 headers.put(name, l);
86 }
87 return l;
88 }
89
90 void addHeader(String name, String value) {
91 name = name.toLowerCase();
92 List<String> l = getOrCreate(name);
93 l.add(value);
94 }
95
96 void setHeader(String name, String value) {
97 name = name.toLowerCase();
98 List<String> l = getOrCreate(name);
99 l.clear();
100 l.add(value);
101 }
102
103 @Override
104 public Optional<Long> firstValueAsLong(String name) {
105 name = name.toLowerCase();
106 List<String> l = headers.get(name);
107 if (l == null) {
108 return Optional.ofNullable(null);
109 } else {
110 String v = l.get(0);
111 Long lv = Long.parseLong(v);
112 return Optional.of(lv);
113 }
114 }
115
116 void clear() {
117 headers.clear();
118 }
119 }
< prev index next >