FileItemHeadersImpl.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.fileupload2.core;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.LinkedHashMap;
  22. import java.util.List;
  23. import java.util.Locale;
  24. import java.util.Map;

  25. /**
  26.  * Default implementation of the {@link FileItemHeaders} interface.
  27.  */
  28. class FileItemHeadersImpl implements FileItemHeaders {

  29.     /**
  30.      * Map of {@code String} keys to a {@code List} of {@code String} instances.
  31.      */
  32.     private final Map<String, List<String>> headerNameToValueListMap = new LinkedHashMap<>();

  33.     /**
  34.      * Method to add header values to this instance.
  35.      *
  36.      * @param name  name of this header
  37.      * @param value value of this header
  38.      */
  39.     @Override
  40.     public synchronized void addHeader(final String name, final String value) {
  41.         headerNameToValueListMap.computeIfAbsent(toLowerCase(name), k -> new ArrayList<>()).add(value);
  42.     }

  43.     /**
  44.      * {@inheritDoc}
  45.      */
  46.     @Override
  47.     public String getHeader(final String name) {
  48.         final var headerValueList = getList(name);
  49.         if (null == headerValueList) {
  50.             return null;
  51.         }
  52.         return headerValueList.get(0);
  53.     }

  54.     /**
  55.      * {@inheritDoc}
  56.      */
  57.     @Override
  58.     public Iterator<String> getHeaderNames() {
  59.         return headerNameToValueListMap.keySet().iterator();
  60.     }

  61.     /**
  62.      * {@inheritDoc}
  63.      */
  64.     @Override
  65.     public Iterator<String> getHeaders(final String name) {
  66.         var headerValueList = getList(name);
  67.         if (null == headerValueList) {
  68.             headerValueList = Collections.emptyList();
  69.         }
  70.         return headerValueList.iterator();
  71.     }

  72.     private List<String> getList(final String name) {
  73.         return headerNameToValueListMap.get(toLowerCase(name));
  74.     }

  75.     private String toLowerCase(final String value) {
  76.         return value.toLowerCase(Locale.ROOT);
  77.     }

  78. }