001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.fileupload.util;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.Iterator;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Locale;
026import java.util.Map;
027
028import org.apache.commons.fileupload.FileItemHeaders;
029
030/**
031 * Default implementation of the {@link FileItemHeaders} interface.
032 *
033 * @since 1.2.1
034 *
035 * @version $Id: FileItemHeadersImpl.java 1458379 2013-03-19 16:16:47Z britter $
036 */
037public class FileItemHeadersImpl implements FileItemHeaders, Serializable {
038
039    /**
040     * Serial version UID, being used, if serialized.
041     */
042    private static final long serialVersionUID = -4455695752627032559L;
043
044    /**
045     * Map of <code>String</code> keys to a <code>List</code> of
046     * <code>String</code> instances.
047     */
048    private final Map<String, List<String>> headerNameToValueListMap = new LinkedHashMap<String, List<String>>();
049
050    /**
051     * {@inheritDoc}
052     */
053    public String getHeader(String name) {
054        String nameLower = name.toLowerCase(Locale.ENGLISH);
055        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
056        if (null == headerValueList) {
057            return null;
058        }
059        return headerValueList.get(0);
060    }
061
062    /**
063     * {@inheritDoc}
064     */
065    public Iterator<String> getHeaderNames() {
066        return headerNameToValueListMap.keySet().iterator();
067    }
068
069    /**
070     * {@inheritDoc}
071     */
072    public Iterator<String> getHeaders(String name) {
073        String nameLower = name.toLowerCase(Locale.ENGLISH);
074        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
075        if (null == headerValueList) {
076            headerValueList = Collections.emptyList();
077        }
078        return headerValueList.iterator();
079    }
080
081    /**
082     * Method to add header values to this instance.
083     *
084     * @param name name of this header
085     * @param value value of this header
086     */
087    public synchronized void addHeader(String name, String value) {
088        String nameLower = name.toLowerCase(Locale.ENGLISH);
089        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
090        if (null == headerValueList) {
091            headerValueList = new ArrayList<String>();
092            headerNameToValueListMap.put(nameLower, headerValueList);
093        }
094        headerValueList.add(value);
095    }
096
097}