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.core5.http.support;
29
30 import java.util.Iterator;
31
32 import org.apache.hc.core5.http.Header;
33 import org.apache.hc.core5.http.HttpMessage;
34 import org.apache.hc.core5.http.ProtocolVersion;
35 import org.apache.hc.core5.http.message.BasicHeader;
36 import org.apache.hc.core5.http.message.HeaderGroup;
37
38
39
40
41
42
43 public abstract class AbstractMessageBuilder<T> {
44
45 private ProtocolVersion version;
46 private HeaderGroup headerGroup;
47
48 protected AbstractMessageBuilder() {
49 }
50
51 protected void digest(final HttpMessage message) {
52 if (message == null) {
53 return;
54 }
55 setVersion(message.getVersion());
56 setHeaders(message.headerIterator());
57 }
58
59 public ProtocolVersion getVersion() {
60 return version;
61 }
62
63 public AbstractMessageBuilder<T> setVersion(final ProtocolVersion version) {
64 this.version = version;
65 return this;
66 }
67
68 public Header[] getHeaders() {
69 return headerGroup != null ? headerGroup.getHeaders() : null;
70 }
71
72 public Header[] getHeaders(final String name) {
73 return headerGroup != null ? headerGroup.getHeaders(name) : null;
74 }
75
76 public AbstractMessageBuilder<T> setHeaders(final Header... headers) {
77 if (headerGroup == null) {
78 headerGroup = new HeaderGroup();
79 }
80 headerGroup.setHeaders(headers);
81 return this;
82 }
83
84 public AbstractMessageBuilder<T> setHeaders(final Iterator<Header> it) {
85 if (headerGroup == null) {
86 headerGroup = new HeaderGroup();
87 } else {
88 headerGroup.clear();
89 }
90 while (it.hasNext()) {
91 headerGroup.addHeader(it.next());
92 }
93 return this;
94 }
95
96 public Header[] getFirstHeaders() {
97 return headerGroup != null ? headerGroup.getHeaders() : null;
98 }
99
100 public Header getFirstHeader(final String name) {
101 return headerGroup != null ? headerGroup.getFirstHeader(name) : null;
102 }
103
104 public Header getLastHeader(final String name) {
105 return headerGroup != null ? headerGroup.getLastHeader(name) : null;
106 }
107
108 public AbstractMessageBuilder<T> addHeader(final Header header) {
109 if (headerGroup == null) {
110 headerGroup = new HeaderGroup();
111 }
112 headerGroup.addHeader(header);
113 return this;
114 }
115
116 public AbstractMessageBuilder<T> addHeader(final String name, final String value) {
117 if (headerGroup == null) {
118 headerGroup = new HeaderGroup();
119 }
120 headerGroup.addHeader(new BasicHeader(name, value));
121 return this;
122 }
123
124 public AbstractMessageBuilder<T> removeHeader(final Header header) {
125 if (headerGroup == null) {
126 headerGroup = new HeaderGroup();
127 }
128 headerGroup.removeHeader(header);
129 return this;
130 }
131
132 public AbstractMessageBuilder<T> removeHeaders(final String name) {
133 if (name == null || headerGroup == null) {
134 return this;
135 }
136 for (final Iterator<Header> i = headerGroup.headerIterator(); i.hasNext(); ) {
137 final Header header = i.next();
138 if (name.equalsIgnoreCase(header.getName())) {
139 i.remove();
140 }
141 }
142 return this;
143 }
144
145 public AbstractMessageBuilder<T> setHeader(final Header header) {
146 if (headerGroup == null) {
147 headerGroup = new HeaderGroup();
148 }
149 headerGroup.setHeader(header);
150 return this;
151 }
152
153 public AbstractMessageBuilder<T> setHeader(final String name, final String value) {
154 if (headerGroup == null) {
155 headerGroup = new HeaderGroup();
156 }
157 headerGroup.setHeader(new BasicHeader(name, value));
158 return this;
159 }
160
161 protected abstract T build();
162
163 }