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 package org.apache.http.protocol;
28
29 import java.io.IOException;
30
31 import org.apache.http.HttpException;
32 import org.apache.http.HttpRequest;
33 import org.apache.http.HttpRequestInterceptor;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.HttpResponseInterceptor;
36 import org.apache.http.annotation.ThreadSafe;
37
38
39
40
41
42
43 @ThreadSafe
44 public final class ImmutableHttpProcessor implements HttpProcessor {
45
46 private final HttpRequestInterceptor[] requestInterceptors;
47 private final HttpResponseInterceptor[] responseInterceptors;
48
49 public ImmutableHttpProcessor(
50 final HttpRequestInterceptor[] requestInterceptors,
51 final HttpResponseInterceptor[] responseInterceptors) {
52 super();
53 if (requestInterceptors != null) {
54 int count = requestInterceptors.length;
55 this.requestInterceptors = new HttpRequestInterceptor[count];
56 for (int i = 0; i < count; i++) {
57 this.requestInterceptors[i] = requestInterceptors[i];
58 }
59 } else {
60 this.requestInterceptors = new HttpRequestInterceptor[0];
61 }
62 if (responseInterceptors != null) {
63 int count = responseInterceptors.length;
64 this.responseInterceptors = new HttpResponseInterceptor[count];
65 for (int i = 0; i < count; i++) {
66 this.responseInterceptors[i] = responseInterceptors[i];
67 }
68 } else {
69 this.responseInterceptors = new HttpResponseInterceptor[0];
70 }
71 }
72
73 public ImmutableHttpProcessor(
74 final HttpRequestInterceptorList requestInterceptors,
75 final HttpResponseInterceptorList responseInterceptors) {
76 super();
77 if (requestInterceptors != null) {
78 int count = requestInterceptors.getRequestInterceptorCount();
79 this.requestInterceptors = new HttpRequestInterceptor[count];
80 for (int i = 0; i < count; i++) {
81 this.requestInterceptors[i] = requestInterceptors.getRequestInterceptor(i);
82 }
83 } else {
84 this.requestInterceptors = new HttpRequestInterceptor[0];
85 }
86 if (responseInterceptors != null) {
87 int count = responseInterceptors.getResponseInterceptorCount();
88 this.responseInterceptors = new HttpResponseInterceptor[count];
89 for (int i = 0; i < count; i++) {
90 this.responseInterceptors[i] = responseInterceptors.getResponseInterceptor(i);
91 }
92 } else {
93 this.responseInterceptors = new HttpResponseInterceptor[0];
94 }
95 }
96
97 public ImmutableHttpProcessor(final HttpRequestInterceptor[] requestInterceptors) {
98 this(requestInterceptors, null);
99 }
100
101 public ImmutableHttpProcessor(final HttpResponseInterceptor[] responseInterceptors) {
102 this(null, responseInterceptors);
103 }
104
105 public void process(
106 final HttpRequest request,
107 final HttpContext context) throws IOException, HttpException {
108 for (int i = 0; i < this.requestInterceptors.length; i++) {
109 this.requestInterceptors[i].process(request, context);
110 }
111 }
112
113 public void process(
114 final HttpResponse response,
115 final HttpContext context) throws IOException, HttpException {
116 for (int i = 0; i < this.responseInterceptors.length; i++) {
117 this.responseInterceptors[i].process(response, context);
118 }
119 }
120
121 }