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