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 */
035public class FileItemHeadersImpl implements FileItemHeaders, Serializable {
036
037    /**
038     * Serial version UID, being used, if serialized.
039     */
040    private static final long serialVersionUID = -4455695752627032559L;
041
042    /**
043     * Map of <code>String</code> keys to a <code>List</code> of
044     * <code>String</code> instances.
045     */
046    private final Map<String, List<String>> headerNameToValueListMap = new LinkedHashMap<String, List<String>>();
047
048    /**
049     * {@inheritDoc}
050     */
051    @Override
052    public String getHeader(String name) {
053        String nameLower = name.toLowerCase(Locale.ENGLISH);
054        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
055        if (null == headerValueList) {
056            return null;
057        }
058        return headerValueList.get(0);
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public Iterator<String> getHeaderNames() {
066        return headerNameToValueListMap.keySet().iterator();
067    }
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public Iterator<String> getHeaders(String name) {
074        String nameLower = name.toLowerCase(Locale.ENGLISH);
075        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
076        if (null == headerValueList) {
077            headerValueList = Collections.emptyList();
078        }
079        return headerValueList.iterator();
080    }
081
082    /**
083     * Method to add header values to this instance.
084     *
085     * @param name name of this header
086     * @param value value of this header
087     */
088    public synchronized void addHeader(String name, String value) {
089        String nameLower = name.toLowerCase(Locale.ENGLISH);
090        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
091        if (null == headerValueList) {
092            headerValueList = new ArrayList<String>();
093            headerNameToValueListMap.put(nameLower, headerValueList);
094        }
095        headerValueList.add(value);
096    }
097
098}