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.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import org.apache.commons.fileupload.FileItemHeaders;
29  
30  /**
31   * Default implementation of the {@link FileItemHeaders} interface.
32   *
33   * @since 1.2.1
34   *
35   * @version $Id: FileItemHeadersImpl.java 1458379 2013-03-19 16:16:47Z britter $
36   */
37  public class FileItemHeadersImpl implements FileItemHeaders, Serializable {
38  
39      /**
40       * Serial version UID, being used, if serialized.
41       */
42      private static final long serialVersionUID = -4455695752627032559L;
43  
44      /**
45       * Map of <code>String</code> keys to a <code>List</code> of
46       * <code>String</code> instances.
47       */
48      private final Map<String, List<String>> headerNameToValueListMap = new LinkedHashMap<String, List<String>>();
49  
50      /**
51       * {@inheritDoc}
52       */
53      public String getHeader(String name) {
54          String nameLower = name.toLowerCase(Locale.ENGLISH);
55          List<String> headerValueList = headerNameToValueListMap.get(nameLower);
56          if (null == headerValueList) {
57              return null;
58          }
59          return headerValueList.get(0);
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      public Iterator<String> getHeaderNames() {
66          return headerNameToValueListMap.keySet().iterator();
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      public Iterator<String> getHeaders(String name) {
73          String nameLower = name.toLowerCase(Locale.ENGLISH);
74          List<String> headerValueList = headerNameToValueListMap.get(nameLower);
75          if (null == headerValueList) {
76              headerValueList = Collections.emptyList();
77          }
78          return headerValueList.iterator();
79      }
80  
81      /**
82       * Method to add header values to this instance.
83       *
84       * @param name name of this header
85       * @param value value of this header
86       */
87      public synchronized void addHeader(String name, String value) {
88          String nameLower = name.toLowerCase(Locale.ENGLISH);
89          List<String> headerValueList = headerNameToValueListMap.get(nameLower);
90          if (null == headerValueList) {
91              headerValueList = new ArrayList<String>();
92              headerNameToValueListMap.put(nameLower, headerValueList);
93          }
94          headerValueList.add(value);
95      }
96  
97  }