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          final List<MimeField> values = this.fieldMap.computeIfAbsent(key, k -> new LinkedList<>());
59          values.add(field);
60          this.fields.add(field);
61      }
62  
63      public List<MimeField> getFields() {
64          return new ArrayList<>(this.fields);
65      }
66  
67      public MimeField getField(final String name) {
68          if (name == null) {
69              return null;
70          }
71          final String key = name.toLowerCase(Locale.ROOT);
72          final List<MimeField> list = this.fieldMap.get(key);
73          if (list != null && !list.isEmpty()) {
74              return list.get(0);
75          }
76          return null;
77      }
78  
79      public List<MimeField> getFields(final String name) {
80          if (name == null) {
81              return null;
82          }
83          final String key = name.toLowerCase(Locale.ROOT);
84          final List<MimeField> list = this.fieldMap.get(key);
85          if (list == null || list.isEmpty()) {
86              return Collections.emptyList();
87          }
88          return new ArrayList<>(list);
89      }
90  
91      public int removeFields(final String name) {
92          if (name == null) {
93              return 0;
94          }
95          final String key = name.toLowerCase(Locale.ROOT);
96          final List<MimeField> removed = fieldMap.remove(key);
97          if (removed == null || removed.isEmpty()) {
98              return 0;
99          }
100         this.fields.removeAll(removed);
101         return removed.size();
102     }
103 
104     public void setField(final MimeField field) {
105         if (field == null) {
106             return;
107         }
108         final String key = field.getName().toLowerCase(Locale.ROOT);
109         final List<MimeField> list = fieldMap.get(key);
110         if (list == null || list.isEmpty()) {
111             addField(field);
112             return;
113         }
114         list.clear();
115         list.add(field);
116         int firstOccurrence = -1;
117         int index = 0;
118         for (final Iterator<MimeField> it = this.fields.iterator(); it.hasNext(); index++) {
119             final MimeField f = it.next();
120             if (f.getName().equalsIgnoreCase(field.getName())) {
121                 it.remove();
122                 if (firstOccurrence == -1) {
123                     firstOccurrence = index;
124                 }
125             }
126         }
127         this.fields.add(firstOccurrence, field);
128     }
129 
130     @Override
131     public Iterator<MimeField> iterator() {
132         return Collections.unmodifiableList(fields).iterator();
133     }
134 
135     @Override
136     public String toString() {
137         return this.fields.toString();
138     }
139 
140 }
141