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.fileupload.util;
    18
    19  import java.io.Serializable;
    20  import java.util.ArrayList;
    21  import java.util.Collections;
    22  import java.util.HashMap;
    23  import java.util.Iterator;
    24  import java.util.List;
    25  import java.util.Map;
    26
    27  import org.apache.commons.fileupload.FileItemHeaders;
    28
    29  /**
    30   * Default implementation of the {@link FileItemHeaders} interface.
    31   *
    32   * @author Michael C. Macaluso
    33   * @since 1.3
    34   */
    35  public class FileItemHeadersImpl implements FileItemHeaders, Serializable {
    36      private static final long serialVersionUID = -4455695752627032559L;
    37
    38      /**
    39       * Map of <code>String</code> keys to a <code>List</code> of
    40       * <code>String</code> instances.
    41       */
    42      private final Map headerNameToValueListMap = new HashMap();
    43
    44      /**
    45       * List to preserve order of headers as added.  This would not be
    46       * needed if a <code>LinkedHashMap</code> could be used, but don't
    47       * want to depend on 1.4.
    48       */
    49      private final List headerNameList = new ArrayList();
    50
    51      public String getHeader(String name) {
    52          String nameLower = name.toLowerCase();
    53          List headerValueList = (List) headerNameToValueListMap.get(nameLower);
    54          if (null == headerValueList) {
    55              return null;
    56          }
    57          return (String) headerValueList.get(0);
    58      }
    59
    60      public Iterator getHeaderNames() {
    61          return headerNameList.iterator();
    62      }
    63
    64      public Iterator getHeaders(String name) {
    65          String nameLower = name.toLowerCase();
    66          List headerValueList = (List) headerNameToValueListMap.get(nameLower);
    67          if (null == headerValueList) {
    68              return Collections.EMPTY_LIST.iterator();
    69          }
    70          return headerValueList.iterator();
    71      }
    72
    73      /**
    74       * Method to add header values to this instance.
    75       *
    76       * @param name name of this header
    77       * @param value value of this header
    78       */
    79      public synchronized void addHeader(String name, String value) {
    80          String nameLower = name.toLowerCase();
    81          List headerValueList = (List) headerNameToValueListMap.get(nameLower);
    82          if (null == headerValueList) {
    83              headerValueList = new ArrayList();
    84              headerNameToValueListMap.put(nameLower, headerValueList);
    85              headerNameList.add(nameLower);
    86          }
    87          headerValueList.add(value);
    88      }
    89  }