1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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