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