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.http.protocol;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34
35 import org.apache.http.HttpException;
36 import org.apache.http.HttpRequest;
37 import org.apache.http.HttpRequestInterceptor;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.HttpResponseInterceptor;
40 import org.apache.http.annotation.NotThreadSafe;
41 import org.apache.http.util.Args;
42
43
44
45
46
47
48
49
50
51
52
53 @NotThreadSafe
54 @Deprecated
55 public final class BasicHttpProcessor implements
56 HttpProcessor, HttpRequestInterceptorList, HttpResponseInterceptorList, Cloneable {
57
58
59 protected final List<HttpRequestInterceptor> requestInterceptors = new ArrayList<HttpRequestInterceptor>();
60 protected final List<HttpResponseInterceptor> responseInterceptors = new ArrayList<HttpResponseInterceptor>();
61
62 public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
63 if (itcp == null) {
64 return;
65 }
66 this.requestInterceptors.add(itcp);
67 }
68
69 public void addRequestInterceptor(
70 final HttpRequestInterceptor itcp, final int index) {
71 if (itcp == null) {
72 return;
73 }
74 this.requestInterceptors.add(index, itcp);
75 }
76
77 public void addResponseInterceptor(
78 final HttpResponseInterceptor itcp, final int index) {
79 if (itcp == null) {
80 return;
81 }
82 this.responseInterceptors.add(index, itcp);
83 }
84
85 public void removeRequestInterceptorByClass(final Class<? extends HttpRequestInterceptor> clazz) {
86 for (final Iterator<HttpRequestInterceptor> it = this.requestInterceptors.iterator();
87 it.hasNext(); ) {
88 final Object request = it.next();
89 if (request.getClass().equals(clazz)) {
90 it.remove();
91 }
92 }
93 }
94
95 public void removeResponseInterceptorByClass(final Class<? extends HttpResponseInterceptor> clazz) {
96 for (final Iterator<HttpResponseInterceptor> it = this.responseInterceptors.iterator();
97 it.hasNext(); ) {
98 final Object request = it.next();
99 if (request.getClass().equals(clazz)) {
100 it.remove();
101 }
102 }
103 }
104
105 public final void addInterceptor(final HttpRequestInterceptor interceptor) {
106 addRequestInterceptor(interceptor);
107 }
108
109 public final void addInterceptor(final HttpRequestInterceptor interceptor, final int index) {
110 addRequestInterceptor(interceptor, index);
111 }
112
113 public int getRequestInterceptorCount() {
114 return this.requestInterceptors.size();
115 }
116
117 public HttpRequestInterceptor getRequestInterceptor(final int index) {
118 if ((index < 0) || (index >= this.requestInterceptors.size())) {
119 return null;
120 }
121 return this.requestInterceptors.get(index);
122 }
123
124 public void clearRequestInterceptors() {
125 this.requestInterceptors.clear();
126 }
127
128 public void addResponseInterceptor(final HttpResponseInterceptor itcp) {
129 if (itcp == null) {
130 return;
131 }
132 this.responseInterceptors.add(itcp);
133 }
134
135 public final void addInterceptor(final HttpResponseInterceptor interceptor) {
136 addResponseInterceptor(interceptor);
137 }
138
139 public final void addInterceptor(final HttpResponseInterceptor interceptor, final int index) {
140 addResponseInterceptor(interceptor, index);
141 }
142
143 public int getResponseInterceptorCount() {
144 return this.responseInterceptors.size();
145 }
146
147 public HttpResponseInterceptor getResponseInterceptor(final int index) {
148 if ((index < 0) || (index >= this.responseInterceptors.size())) {
149 return null;
150 }
151 return this.responseInterceptors.get(index);
152 }
153
154 public void clearResponseInterceptors() {
155 this.responseInterceptors.clear();
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public void setInterceptors(final List<?> list) {
176 Args.notNull(list, "Inteceptor list");
177 this.requestInterceptors.clear();
178 this.responseInterceptors.clear();
179 for (int i = 0; i < list.size(); i++) {
180 final Object obj = list.get(i);
181 if (obj instanceof HttpRequestInterceptor) {
182 addInterceptor((HttpRequestInterceptor)obj);
183 }
184 if (obj instanceof HttpResponseInterceptor) {
185 addInterceptor((HttpResponseInterceptor)obj);
186 }
187 }
188 }
189
190
191
192
193 public void clearInterceptors() {
194 clearRequestInterceptors();
195 clearResponseInterceptors();
196 }
197
198 public void process(
199 final HttpRequest request,
200 final HttpContext context)
201 throws IOException, HttpException {
202 for (int i = 0; i < this.requestInterceptors.size(); i++) {
203 final HttpRequestInterceptor interceptor =
204 this.requestInterceptors.get(i);
205 interceptor.process(request, context);
206 }
207 }
208
209 public void process(
210 final HttpResponse response,
211 final HttpContext context)
212 throws IOException, HttpException {
213 for (int i = 0; i < this.responseInterceptors.size(); i++) {
214 final HttpResponseInterceptor interceptor =
215 this.responseInterceptors.get(i);
216 interceptor.process(response, context);
217 }
218 }
219
220
221
222
223
224
225
226 protected void copyInterceptors(final BasicHttpProcessor target) {
227 target.requestInterceptors.clear();
228 target.requestInterceptors.addAll(this.requestInterceptors);
229 target.responseInterceptors.clear();
230 target.responseInterceptors.addAll(this.responseInterceptors);
231 }
232
233
234
235
236
237
238 public BasicHttpProcessor copy() {
239 final BasicHttpProcessor clone = new BasicHttpProcessor();
240 copyInterceptors(clone);
241 return clone;
242 }
243
244 @Override
245 public Object clone() throws CloneNotSupportedException {
246 final BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
247 copyInterceptors(clone);
248 return clone;
249 }
250
251 }