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