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.hc.client5.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 a MIME entity.
41   */
42  public class Header implements Iterable<MimeField> {
43  
44      private final List<MimeField> fields;
45      private final Map<String, List<MimeField>> fieldMap;
46  
47      public Header() {
48          super();
49          this.fields = new LinkedList<>();
50          this.fieldMap = new HashMap<>();
51      }
52  
53      public void addField(final MimeField field) {
54          if (field == null) {
55              return;
56          }
57          final String key = field.getName().toLowerCase(Locale.ROOT);
58          List<MimeField> values = this.fieldMap.get(key);
59          if (values == null) {
60              values = new LinkedList<>();
61              this.fieldMap.put(key, values);
62          }
63          values.add(field);
64          this.fields.add(field);
65      }
66  
67      public List<MimeField> getFields() {
68          return new ArrayList<>(this.fields);
69      }
70  
71      public MimeField getField(final String name) {
72          if (name == null) {
73              return null;
74          }
75          final String key = name.toLowerCase(Locale.ROOT);
76          final List<MimeField> 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<MimeField> getFields(final String name) {
84          if (name == null) {
85              return null;
86          }
87          final String key = name.toLowerCase(Locale.ROOT);
88          final List<MimeField> list = this.fieldMap.get(key);
89          if (list == null || list.isEmpty()) {
90              return Collections.emptyList();
91          }
92          return new ArrayList<>(list);
93      }
94  
95      public int removeFields(final String name) {
96          if (name == null) {
97              return 0;
98          }
99          final String key = name.toLowerCase(Locale.ROOT);
100         final List<MimeField> removed = fieldMap.remove(key);
101         if (removed == null || removed.isEmpty()) {
102             return 0;
103         }
104         this.fields.removeAll(removed);
105         return removed.size();
106     }
107 
108     public void setField(final MimeField field) {
109         if (field == null) {
110             return;
111         }
112         final String key = field.getName().toLowerCase(Locale.ROOT);
113         final List<MimeField> list = fieldMap.get(key);
114         if (list == null || list.isEmpty()) {
115             addField(field);
116             return;
117         }
118         list.clear();
119         list.add(field);
120         int firstOccurrence = -1;
121         int index = 0;
122         for (final Iterator<MimeField> it = this.fields.iterator(); it.hasNext(); index++) {
123             final MimeField f = it.next();
124             if (f.getName().equalsIgnoreCase(field.getName())) {
125                 it.remove();
126                 if (firstOccurrence == -1) {
127                     firstOccurrence = index;
128                 }
129             }
130         }
131         this.fields.add(firstOccurrence, field);
132     }
133 
134     @Override
135     public Iterator<MimeField> iterator() {
136         return Collections.unmodifiableList(fields).iterator();
137     }
138 
139     @Override
140     public String toString() {
141         return this.fields.toString();
142     }
143 
144 }
145