View Javadoc

    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
    19  import java.util.ArrayList;
    20  import java.util.Collections;
    21  import java.util.Iterator;
    22  import java.util.LinkedHashMap;
    23  import java.util.List;
    24  import java.util.Locale;
    25  import java.util.Map;
    26
    27  /**
    28   * Default implementation of the {@link FileItemHeaders} interface.
    29   */
    30  class FileItemHeadersImpl implements FileItemHeaders {
    31
    32      /**
    33       * Map of {@code String} keys to a {@code List} of {@code String} instances.
    34       */
    35      private final Map<String, List<String>> headerNameToValueListMap = new LinkedHashMap<>();
    36
    37      /**
    38       * Method to add header values to this instance.
    39       *
    40       * @param name  name of this header
    41       * @param value value of this header
    42       */
    43      @Override
    44      public synchronized void addHeader(final String name, final String value) {
    45          headerNameToValueListMap.computeIfAbsent(toLowerCase(name), k -> new ArrayList<>()).add(value);
    46      }
    47
    48      /**
    49       * {@inheritDoc}
    50       */
    51      @Override
    52      public String getHeader(final String name) {
    53          final var headerValueList = getList(name);
    54          if (null == headerValueList) {
    55              return null;
    56          }
    57          return headerValueList.get(0);
    58      }
    59
    60      /**
    61       * {@inheritDoc}
    62       */
    63      @Override
    64      public Iterator<String> getHeaderNames() {
    65          return headerNameToValueListMap.keySet().iterator();
    66      }
    67
    68      /**
    69       * {@inheritDoc}
    70       */
    71      @Override
    72      public Iterator<String> getHeaders(final String name) {
    73          var headerValueList = getList(name);
    74          if (null == headerValueList) {
    75              headerValueList = Collections.emptyList();
    76          }
    77          return headerValueList.iterator();
    78      }
    79
    80      private List<String> getList(final String name) {
    81          return headerNameToValueListMap.get(toLowerCase(name));
    82      }
    83
    84      private String toLowerCase(final String value) {
    85          return value.toLowerCase(Locale.ENGLISH);
    86      }
    87
    88  }