    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    public String getHeader(String name) {
    052        String nameLower = name.toLowerCase(Locale.ENGLISH);
    053        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
    054        if (null == headerValueList) {
    055            return null;
    056        }
    057        return headerValueList.get(0);
    058    }
    059
    060    /**
    061     * {@inheritDoc}
    062     */
    063    public Iterator<String> getHeaderNames() {
    064        return headerNameToValueListMap.keySet().iterator();
    065    }
    066
    067    /**
    068     * {@inheritDoc}
    069     */
    070    public Iterator<String> getHeaders(String name) {
    071        String nameLower = name.toLowerCase(Locale.ENGLISH);
    072        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
    073        if (null == headerValueList) {
    074            headerValueList = Collections.emptyList();
    075        }
    076        return headerValueList.iterator();
    077    }
    078
    079    /**
    080     * Method to add header values to this instance.
    081     *
    082     * @param name name of this header
    083     * @param value value of this header
    084     */
    085    public synchronized void addHeader(String name, String value) {
    086        String nameLower = name.toLowerCase(Locale.ENGLISH);
    087        List<String> headerValueList = headerNameToValueListMap.get(nameLower);
    088        if (null == headerValueList) {
    089            headerValueList = new ArrayList<String>();
    090            headerNameToValueListMap.put(nameLower, headerValueList);
    091        }
    092        headerValueList.add(value);
    093    }
    094
    095}