|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 */ |
|
26 |
|
27 package ch.boye.httpclientandroidlib.protocol; |
|
28 |
|
29 import java.io.IOException; |
|
30 |
|
31 import ch.boye.httpclientandroidlib.HttpException; |
|
32 import ch.boye.httpclientandroidlib.HttpRequest; |
|
33 import ch.boye.httpclientandroidlib.HttpRequestInterceptor; |
|
34 import ch.boye.httpclientandroidlib.HttpResponse; |
|
35 import ch.boye.httpclientandroidlib.HttpResponseInterceptor; |
|
36 |
|
37 /** |
|
38 * Immutable {@link HttpProcessor}. |
|
39 * |
|
40 * @since 4.1 |
|
41 */ |
|
42 //@ThreadSafe |
|
43 public final class ImmutableHttpProcessor implements HttpProcessor { |
|
44 |
|
45 private final HttpRequestInterceptor[] requestInterceptors; |
|
46 private final HttpResponseInterceptor[] responseInterceptors; |
|
47 |
|
48 public ImmutableHttpProcessor( |
|
49 final HttpRequestInterceptor[] requestInterceptors, |
|
50 final HttpResponseInterceptor[] responseInterceptors) { |
|
51 super(); |
|
52 if (requestInterceptors != null) { |
|
53 int count = requestInterceptors.length; |
|
54 this.requestInterceptors = new HttpRequestInterceptor[count]; |
|
55 for (int i = 0; i < count; i++) { |
|
56 this.requestInterceptors[i] = requestInterceptors[i]; |
|
57 } |
|
58 } else { |
|
59 this.requestInterceptors = new HttpRequestInterceptor[0]; |
|
60 } |
|
61 if (responseInterceptors != null) { |
|
62 int count = responseInterceptors.length; |
|
63 this.responseInterceptors = new HttpResponseInterceptor[count]; |
|
64 for (int i = 0; i < count; i++) { |
|
65 this.responseInterceptors[i] = responseInterceptors[i]; |
|
66 } |
|
67 } else { |
|
68 this.responseInterceptors = new HttpResponseInterceptor[0]; |
|
69 } |
|
70 } |
|
71 |
|
72 public ImmutableHttpProcessor( |
|
73 final HttpRequestInterceptorList requestInterceptors, |
|
74 final HttpResponseInterceptorList responseInterceptors) { |
|
75 super(); |
|
76 if (requestInterceptors != null) { |
|
77 int count = requestInterceptors.getRequestInterceptorCount(); |
|
78 this.requestInterceptors = new HttpRequestInterceptor[count]; |
|
79 for (int i = 0; i < count; i++) { |
|
80 this.requestInterceptors[i] = requestInterceptors.getRequestInterceptor(i); |
|
81 } |
|
82 } else { |
|
83 this.requestInterceptors = new HttpRequestInterceptor[0]; |
|
84 } |
|
85 if (responseInterceptors != null) { |
|
86 int count = responseInterceptors.getResponseInterceptorCount(); |
|
87 this.responseInterceptors = new HttpResponseInterceptor[count]; |
|
88 for (int i = 0; i < count; i++) { |
|
89 this.responseInterceptors[i] = responseInterceptors.getResponseInterceptor(i); |
|
90 } |
|
91 } else { |
|
92 this.responseInterceptors = new HttpResponseInterceptor[0]; |
|
93 } |
|
94 } |
|
95 |
|
96 public ImmutableHttpProcessor(final HttpRequestInterceptor[] requestInterceptors) { |
|
97 this(requestInterceptors, null); |
|
98 } |
|
99 |
|
100 public ImmutableHttpProcessor(final HttpResponseInterceptor[] responseInterceptors) { |
|
101 this(null, responseInterceptors); |
|
102 } |
|
103 |
|
104 public void process( |
|
105 final HttpRequest request, |
|
106 final HttpContext context) throws IOException, HttpException { |
|
107 for (int i = 0; i < this.requestInterceptors.length; i++) { |
|
108 this.requestInterceptors[i].process(request, context); |
|
109 } |
|
110 } |
|
111 |
|
112 public void process( |
|
113 final HttpResponse response, |
|
114 final HttpContext context) throws IOException, HttpException { |
|
115 for (int i = 0; i < this.responseInterceptors.length; i++) { |
|
116 this.responseInterceptors[i].process(response, context); |
|
117 } |
|
118 } |
|
119 |
|
120 } |