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