< 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.HashSet;
  29 import java.util.LinkedList;
  30 import java.util.List;
  31 import java.util.Map;
  32 import java.util.Optional;
  33 import java.util.Set;
  34 
  35 /**
  36  * Implementation of HttpHeaders.
  37  */
  38 class HttpHeadersImpl implements HttpHeaders1 {
  39 
  40     private final HashMap<String,List<String>> headers;
  41     private boolean isUnmodifiable = false;
  42 
  43     public HttpHeadersImpl() {
  44         headers = new HashMap<>();
  45     }
  46 
  47     /**
  48      * Replace all List<String> in headers with unmodifiable Lists. Call
  49      * this only after all headers are added. The headers HashMap
  50      * is wrapped with an unmodifiable HashMap in map()
  51      */
  52     @Override
  53     public void makeUnmodifiable() {
  54         if (isUnmodifiable)
  55             return;
  56 
  57         Set<String> keys = new HashSet<>(headers.keySet());
  58         for (String key : keys) {
  59             List<String> values = headers.remove(key);
  60             if (values != null) {
  61                 headers.put(key, Collections.unmodifiableList(values));
  62             }
  63         }
  64         isUnmodifiable = true;
  65     }
  66 
  67     @Override
  68     public Optional<String> firstValue(String name) {
  69         List<String> l = headers.get(name);
  70         return Optional.ofNullable(l == null ? null : l.get(0));
  71     }
  72 
  73     @Override
  74     public List<String> allValues(String name) {
  75         return headers.get(name);
  76     }
  77 
  78     @Override
  79     public Map<String, List<String>> map() {
  80         return Collections.unmodifiableMap(headers);
  81     }
  82 
  83     Map<String, List<String>> directMap() {
  84         return headers;
  85     }
  86 
  87     // package private mutators
  88 
  89     public HttpHeadersImpl deepCopy() {
  90         HttpHeadersImpl h1 = new HttpHeadersImpl();
  91         HashMap<String,List<String>> headers1 = h1.headers;
  92         Set<String> keys = headers.keySet();
  93         for (String key : keys) {
  94             List<String> vals = headers.get(key);
  95             LinkedList<String> vals1 = new LinkedList<>(vals);
  96             headers1.put(key, vals1);
  97         }
  98         return h1;
  99     }
 100 
 101     private List<String> getOrCreate(String name) {
 102         List<String> l = headers.get(name);
 103         if (l == null) {
 104             l = new LinkedList<>();
 105             headers.put(name, l);
 106         }
 107         return l;
 108     }
 109 
 110     void addHeader(String name, String value) {
 111         List<String> l = getOrCreate(name);
 112         l.add(value);
 113     }
 114 
 115     void setHeader(String name, String value) {
 116         List<String> l = getOrCreate(name);
 117         l.clear();
 118         l.add(value);
 119     }
 120 
 121     @Override
 122     public Optional<Long> firstValueAsLong(String name) {
 123         List<String> l = headers.get(name);
 124         if (l == null) {
 125             return Optional.ofNullable(null);
 126         } else {
 127             String v = l.get(0);
 128             Long lv = Long.parseLong(v);
 129             return Optional.of(lv);
 130         }
 131     }
 132 
 133     void clear() {
 134         headers.clear();
 135     }
 136 }
< prev index next >