Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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 */
28 package ch.boye.httpclientandroidlib.protocol;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
35 import ch.boye.httpclientandroidlib.HttpException;
36 import ch.boye.httpclientandroidlib.HttpRequest;
37 import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
38 import ch.boye.httpclientandroidlib.HttpResponse;
39 import ch.boye.httpclientandroidlib.HttpResponseInterceptor;
41 /**
42 * Default implementation of {@link HttpProcessor}.
43 * <p>
44 * Please note access to the internal structures of this class is not
45 * synchronized and therefore this class may be thread-unsafe.
46 *
47 * @since 4.0
48 */
49 //@NotThreadSafe // Lists are not synchronized
50 public final class BasicHttpProcessor implements
51 HttpProcessor, HttpRequestInterceptorList, HttpResponseInterceptorList, Cloneable {
53 // Don't allow direct access, as nulls are not allowed
54 protected final List requestInterceptors = new ArrayList();
55 protected final List responseInterceptors = new ArrayList();
57 public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
58 if (itcp == null) {
59 return;
60 }
61 this.requestInterceptors.add(itcp);
62 }
64 public void addRequestInterceptor(
65 final HttpRequestInterceptor itcp, int index) {
66 if (itcp == null) {
67 return;
68 }
69 this.requestInterceptors.add(index, itcp);
70 }
72 public void addResponseInterceptor(
73 final HttpResponseInterceptor itcp, int index) {
74 if (itcp == null) {
75 return;
76 }
77 this.responseInterceptors.add(index, itcp);
78 }
80 public void removeRequestInterceptorByClass(final Class clazz) {
81 for (Iterator it = this.requestInterceptors.iterator();
82 it.hasNext(); ) {
83 Object request = it.next();
84 if (request.getClass().equals(clazz)) {
85 it.remove();
86 }
87 }
88 }
90 public void removeResponseInterceptorByClass(final Class clazz) {
91 for (Iterator it = this.responseInterceptors.iterator();
92 it.hasNext(); ) {
93 Object request = it.next();
94 if (request.getClass().equals(clazz)) {
95 it.remove();
96 }
97 }
98 }
100 public final void addInterceptor(final HttpRequestInterceptor interceptor) {
101 addRequestInterceptor(interceptor);
102 }
104 public final void addInterceptor(final HttpRequestInterceptor interceptor, int index) {
105 addRequestInterceptor(interceptor, index);
106 }
108 public int getRequestInterceptorCount() {
109 return this.requestInterceptors.size();
110 }
112 public HttpRequestInterceptor getRequestInterceptor(int index) {
113 if ((index < 0) || (index >= this.requestInterceptors.size()))
114 return null;
115 return (HttpRequestInterceptor) this.requestInterceptors.get(index);
116 }
118 public void clearRequestInterceptors() {
119 this.requestInterceptors.clear();
120 }
122 public void addResponseInterceptor(final HttpResponseInterceptor itcp) {
123 if (itcp == null) {
124 return;
125 }
126 this.responseInterceptors.add(itcp);
127 }
129 public final void addInterceptor(final HttpResponseInterceptor interceptor) {
130 addResponseInterceptor(interceptor);
131 }
133 public final void addInterceptor(final HttpResponseInterceptor interceptor, int index) {
134 addResponseInterceptor(interceptor, index);
135 }
137 public int getResponseInterceptorCount() {
138 return this.responseInterceptors.size();
139 }
141 public HttpResponseInterceptor getResponseInterceptor(int index) {
142 if ((index < 0) || (index >= this.responseInterceptors.size()))
143 return null;
144 return (HttpResponseInterceptor) this.responseInterceptors.get(index);
145 }
147 public void clearResponseInterceptors() {
148 this.responseInterceptors.clear();
149 }
151 /**
152 * Sets the interceptor lists.
153 * First, both interceptor lists maintained by this processor
154 * will be cleared.
155 * Subsequently,
156 * elements of the argument list that are request interceptors will be
157 * added to the request interceptor list.
158 * Elements that are response interceptors will be
159 * added to the response interceptor list.
160 * Elements that are both request and response interceptor will be
161 * added to both lists.
162 * Elements that are neither request nor response interceptor
163 * will be ignored.
164 *
165 * @param list the list of request and response interceptors
166 * from which to initialize
167 */
168 public void setInterceptors(final List list) {
169 if (list == null) {
170 throw new IllegalArgumentException("List must not be null.");
171 }
172 this.requestInterceptors.clear();
173 this.responseInterceptors.clear();
174 for (int i = 0; i < list.size(); i++) {
175 Object obj = list.get(i);
176 if (obj instanceof HttpRequestInterceptor) {
177 addInterceptor((HttpRequestInterceptor)obj);
178 }
179 if (obj instanceof HttpResponseInterceptor) {
180 addInterceptor((HttpResponseInterceptor)obj);
181 }
182 }
183 }
185 /**
186 * Clears both interceptor lists maintained by this processor.
187 */
188 public void clearInterceptors() {
189 clearRequestInterceptors();
190 clearResponseInterceptors();
191 }
193 public void process(
194 final HttpRequest request,
195 final HttpContext context)
196 throws IOException, HttpException {
197 for (int i = 0; i < this.requestInterceptors.size(); i++) {
198 HttpRequestInterceptor interceptor =
199 (HttpRequestInterceptor) this.requestInterceptors.get(i);
200 interceptor.process(request, context);
201 }
202 }
204 public void process(
205 final HttpResponse response,
206 final HttpContext context)
207 throws IOException, HttpException {
208 for (int i = 0; i < this.responseInterceptors.size(); i++) {
209 HttpResponseInterceptor interceptor =
210 (HttpResponseInterceptor) this.responseInterceptors.get(i);
211 interceptor.process(response, context);
212 }
213 }
215 /**
216 * Sets up the target to have the same list of interceptors
217 * as the current instance.
218 *
219 * @param target object to be initialised
220 */
221 protected void copyInterceptors(final BasicHttpProcessor target) {
222 target.requestInterceptors.clear();
223 target.requestInterceptors.addAll(this.requestInterceptors);
224 target.responseInterceptors.clear();
225 target.responseInterceptors.addAll(this.responseInterceptors);
226 }
228 /**
229 * Creates a copy of this instance
230 *
231 * @return new instance of the BasicHttpProcessor
232 */
233 public BasicHttpProcessor copy() {
234 BasicHttpProcessor clone = new BasicHttpProcessor();
235 copyInterceptors(clone);
236 return clone;
237 }
239 public Object clone() throws CloneNotSupportedException {
240 BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
241 copyInterceptors(clone);
242 return clone;
243 }
245 }