View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.entity.mime;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.LinkedList;
35  import java.util.List;
36  import java.util.Locale;
37  import java.util.Map;
38  
39  /**
40   * The header of an entity (see RFC 2045).
41   */
42  public class Header implements Iterable<MinimalField> {
43  
44      private final List<MinimalField> fields;
45      private final Map<String, List<MinimalField>> fieldMap;
46  
47      public Header() {
48          super();
49          this.fields = new LinkedList<MinimalField>();
50          this.fieldMap = new HashMap<String, List<MinimalField>>();
51      }
52  
53      public void addField(final MinimalField field) {
54          if (field == null) {
55              return;
56          }
57          final String key = field.getName().toLowerCase(Locale.ROOT);
58          List<MinimalField> values = this.fieldMap.get(key);
59          if (values == null) {
60              values = new LinkedList<MinimalField>();
61              this.fieldMap.put(key, values);
62          }
63          values.add(field);
64          this.fields.add(field);
65      }
66  
67      public List<MinimalField> getFields() {
68          return new ArrayList<MinimalField>(this.fields);
69      }
70  
71      public MinimalField getField(final String name) {
72          if (name == null) {
73              return null;
74          }
75          final String key = name.toLowerCase(Locale.ROOT);
76          final List<MinimalField> list = this.fieldMap.get(key);
77          if (list != null && !list.isEmpty()) {
78              return list.get(0);
79          }
80          return null;
81      }
82  
83      public List<MinimalField> getFields(final String name) {
84          if (name == null) {
85              return null;
86          }
87          final String key = name.toLowerCase(Locale.ROOT);
88          final List<MinimalField> list = this.fieldMap.get(key);
89          if (list == null || list.isEmpty()) {
90              return Collections.emptyList();
91          } else {
92              return new ArrayList<MinimalField>(list);
93          }
94      }
95  
96      public int removeFields(final String name) {
97          if (name == null) {
98              return 0;
99          }
100         final String key = name.toLowerCase(Locale.ROOT);
101         final List<MinimalField> removed = fieldMap.remove(key);
102         if (removed == null || removed.isEmpty()) {
103             return 0;
104         }
105         this.fields.removeAll(removed);
106         return removed.size();
107     }
108 
109     public void setField(final MinimalField field) {
110         if (field == null) {
111             return;
112         }
113         final String key = field.getName().toLowerCase(Locale.ROOT);
114         final List<MinimalField> list = fieldMap.get(key);
115         if (list == null || list.isEmpty()) {
116             addField(field);
117             return;
118         }
119         list.clear();
120         list.add(field);
121         int firstOccurrence = -1;
122         int index = 0;
123         for (final Iterator<MinimalField> it = this.fields.iterator(); it.hasNext(); index++) {
124             final MinimalField f = it.next();
125             if (f.getName().equalsIgnoreCase(field.getName())) {
126                 it.remove();
127                 if (firstOccurrence == -1) {
128                     firstOccurrence = index;
129                 }
130             }
131         }
132         this.fields.add(firstOccurrence, field);
133     }
134 
135     @Override
136     public Iterator<MinimalField> iterator() {
137         return Collections.unmodifiableList(fields).iterator();
138     }
139 
140     @Override
141     public String toString() {
142         return this.fields.toString();
143     }
144 
145 }
146