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     */
017    package org.apache.commons.fileupload.util;
018    
019    import java.io.Serializable;
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.apache.commons.fileupload.FileItemHeaders;
028    
029    /**
030     * Default implementation of the {@link FileItemHeaders} interface.
031     *
032     * @author Michael C. Macaluso
033     * @since 1.3
034     */
035    public class FileItemHeadersImpl implements FileItemHeaders, Serializable {
036        private static final long serialVersionUID = -4455695752627032559L;
037    
038        /**
039         * Map of <code>String</code> keys to a <code>List</code> of
040         * <code>String</code> instances.
041         */
042        private final Map headerNameToValueListMap = new HashMap();
043    
044        /**
045         * List to preserve order of headers as added.  This would not be
046         * needed if a <code>LinkedHashMap</code> could be used, but don't
047         * want to depend on 1.4.
048         */
049        private final List headerNameList = new ArrayList();
050    
051        public String getHeader(String name) {
052            String nameLower = name.toLowerCase();
053            List headerValueList = (List) headerNameToValueListMap.get(nameLower);
054            if (null == headerValueList) {
055                return null;
056            }
057            return (String) headerValueList.get(0);
058        }
059    
060        public Iterator getHeaderNames() {
061            return headerNameList.iterator();
062        }
063    
064        public Iterator getHeaders(String name) {
065            String nameLower = name.toLowerCase();
066            List headerValueList = (List) headerNameToValueListMap.get(nameLower);
067            if (null == headerValueList) {
068                return Collections.EMPTY_LIST.iterator();
069            }
070            return headerValueList.iterator();
071        }
072    
073        /**
074         * Method to add header values to this instance.
075         *
076         * @param name name of this header
077         * @param value value of this header
078         */
079        public synchronized void addHeader(String name, String value) {
080            String nameLower = name.toLowerCase();
081            List headerValueList = (List) headerNameToValueListMap.get(nameLower);
082            if (null == headerValueList) {
083                headerValueList = new ArrayList();
084                headerNameToValueListMap.put(nameLower, headerValueList);
085                headerNameList.add(nameLower);
086            }
087            headerValueList.add(value);
088        }
089    }