001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/protocol/BasicHttpProcessor.java $
003: * $Revision: 613298 $
004: * $Date: 2008-01-18 23:09:22 +0100 (Fri, 18 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.protocol;
033:
034: import java.io.IOException;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: import org.apache.http.HttpException;
040: import org.apache.http.HttpRequest;
041: import org.apache.http.HttpRequestInterceptor;
042: import org.apache.http.HttpResponse;
043: import org.apache.http.HttpResponseInterceptor;
044:
045: /**
046: * Keeps lists of interceptors for processing requests and responses.
047: *
048: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
049: * @author Andrea Selva
050: *
051: * @version $Revision: 613298 $
052: *
053: * @since 4.0
054: */
055: public final class BasicHttpProcessor implements HttpProcessor,
056: HttpRequestInterceptorList, HttpResponseInterceptorList,
057: Cloneable {
058:
059: protected List requestInterceptors = null;
060: protected List responseInterceptors = null;
061:
062: // non-Javadoc, see interface HttpRequestInterceptorList
063: public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
064:
065: if (itcp == null) {
066: return;
067: }
068: if (this .requestInterceptors == null) {
069: this .requestInterceptors = new ArrayList();
070: }
071: this .requestInterceptors.add(itcp);
072: }
073:
074: // non-Javadoc, see interface HttpRequestInterceptorList
075: public void addRequestInterceptor(
076: final HttpRequestInterceptor itcp, int index) {
077: if (index < 0) {
078: throw new IndexOutOfBoundsException(String.valueOf(index));
079: }
080: if (itcp == null) {
081: return;
082: }
083:
084: if (this .requestInterceptors == null) {
085: if (index > 0) {
086: throw new IndexOutOfBoundsException(String
087: .valueOf(index));
088: }
089: this .requestInterceptors = new ArrayList();
090: }
091: this .requestInterceptors.add(index, itcp);
092: }
093:
094: public void addResponseInterceptor(HttpResponseInterceptor itcp,
095: int index) {
096: if (index < 0) {
097: throw new IndexOutOfBoundsException(String.valueOf(index));
098: }
099: if (itcp == null) {
100: return;
101: }
102:
103: if (this .responseInterceptors == null) {
104: if (index > 0) {
105: throw new IndexOutOfBoundsException(String
106: .valueOf(index));
107: }
108: this .responseInterceptors = new ArrayList();
109: }
110: this .responseInterceptors.add(index, itcp);
111: }
112:
113: // non-Javadoc, see interface HttpRequestInterceptorList
114: public void removeRequestInterceptorByClass(final Class clazz) {
115: if (this .requestInterceptors == null) {
116: return;
117: }
118: for (Iterator it = this .requestInterceptors.iterator(); it
119: .hasNext();) {
120: Object request = it.next();
121: if (request.getClass().equals(clazz)) {
122: it.remove();
123: }
124: }
125: }
126:
127: // non-Javadoc, see interface HttpResponseInterceptorList
128: public void removeResponseInterceptorByClass(final Class clazz) {
129: if (this .responseInterceptors == null) {
130: return;
131: }
132: for (Iterator it = this .responseInterceptors.iterator(); it
133: .hasNext();) {
134: Object request = it.next();
135: if (request.getClass().equals(clazz)) {
136: it.remove();
137: }
138: }
139: }
140:
141: /**
142: * Same as {@link #addRequestInterceptor(HttpRequestInterceptor) addRequestInterceptor}.
143: *
144: * @param interceptor the interceptor to add
145: */
146: public final void addInterceptor(
147: final HttpRequestInterceptor interceptor) {
148: addRequestInterceptor(interceptor);
149: }
150:
151: public final void addInterceptor(
152: final HttpRequestInterceptor interceptor, int index) {
153: addRequestInterceptor(interceptor, index);
154: }
155:
156: // non-Javadoc, see interface HttpRequestInterceptorList
157: public int getRequestInterceptorCount() {
158: return (this .requestInterceptors == null) ? 0
159: : this .requestInterceptors.size();
160: }
161:
162: // non-Javadoc, see interface HttpRequestInterceptorList
163: public HttpRequestInterceptor getRequestInterceptor(int index) {
164:
165: if ((this .requestInterceptors == null) || (index < 0)
166: || (index >= this .requestInterceptors.size()))
167: return null;
168:
169: return (HttpRequestInterceptor) this .requestInterceptors
170: .get(index);
171: }
172:
173: // non-Javadoc, see interface HttpRequestInterceptorList
174: public void clearRequestInterceptors() {
175: this .requestInterceptors = null;
176: }
177:
178: // non-Javadoc, see interface HttpResponseInterceptorList
179: public void addResponseInterceptor(
180: final HttpResponseInterceptor itcp) {
181: if (itcp == null) {
182: return;
183: }
184: if (this .responseInterceptors == null) {
185: this .responseInterceptors = new ArrayList();
186: }
187: this .responseInterceptors.add(itcp);
188: }
189:
190: /**
191: * Same as {@link #addResponseInterceptor(HttpResponseInterceptor) addResponseInterceptor}.
192: *
193: * @param interceptor the interceptor to add
194: */
195: public final void addInterceptor(
196: final HttpResponseInterceptor interceptor) {
197: addResponseInterceptor(interceptor);
198: }
199:
200: public final void addInterceptor(
201: final HttpResponseInterceptor interceptor, int index) {
202: addResponseInterceptor(interceptor, index);
203: }
204:
205: // non-Javadoc, see interface HttpResponseInterceptorList
206: public int getResponseInterceptorCount() {
207: return (this .responseInterceptors == null) ? 0
208: : this .responseInterceptors.size();
209: }
210:
211: // non-Javadoc, see interface HttpResponseInterceptorList
212: public HttpResponseInterceptor getResponseInterceptor(int index) {
213:
214: if ((this .responseInterceptors == null) || (index < 0)
215: || (index >= this .responseInterceptors.size()))
216: return null;
217:
218: return (HttpResponseInterceptor) this .responseInterceptors
219: .get(index);
220: }
221:
222: // non-Javadoc, see interface HttpResponseInterceptorList
223: public void clearResponseInterceptors() {
224: this .responseInterceptors = null;
225: }
226:
227: /**
228: * Sets the interceptor lists.
229: * First, both interceptor lists maintained by this processor
230: * will be cleared.
231: * Subsequently,
232: * elements of the argument list that are request interceptors will be
233: * added to the request interceptor list.
234: * Elements that are response interceptors will be
235: * added to the response interceptor list.
236: * Elements that are both request and response interceptor will be
237: * added to both lists.
238: * Elements that are neither request nor response interceptor
239: * will be ignored.
240: *
241: * @param list the list of request and response interceptors
242: * from which to initialize
243: */
244: public void setInterceptors(final List list) {
245: if (list == null) {
246: throw new IllegalArgumentException("List must not be null.");
247: }
248: if (this .requestInterceptors != null) {
249: this .requestInterceptors.clear();
250: }
251: if (this .responseInterceptors != null) {
252: this .responseInterceptors.clear();
253: }
254: for (int i = 0; i < list.size(); i++) {
255: Object obj = list.get(i);
256: if (obj instanceof HttpRequestInterceptor) {
257: addInterceptor((HttpRequestInterceptor) obj);
258: }
259: if (obj instanceof HttpResponseInterceptor) {
260: addInterceptor((HttpResponseInterceptor) obj);
261: }
262: }
263: }
264:
265: /**
266: * Clears both interceptor lists maintained by this processor.
267: */
268: public void clearInterceptors() {
269: clearRequestInterceptors();
270: clearResponseInterceptors();
271: }
272:
273: // non-Javadoc, see interface HttpRequestInterceptor (via HttpProcessor)
274: public void process(final HttpRequest request,
275: final HttpContext context) throws IOException,
276: HttpException {
277: if (this .requestInterceptors != null) {
278: for (int i = 0; i < this .requestInterceptors.size(); i++) {
279: HttpRequestInterceptor interceptor = (HttpRequestInterceptor) this .requestInterceptors
280: .get(i);
281: interceptor.process(request, context);
282: }
283: }
284: }
285:
286: // non-Javadoc, see interface HttpResponseInterceptor (via HttpProcessor)
287: public void process(final HttpResponse response,
288: final HttpContext context) throws IOException,
289: HttpException {
290: if (this .responseInterceptors != null) {
291: for (int i = 0; i < this .responseInterceptors.size(); i++) {
292: HttpResponseInterceptor interceptor = (HttpResponseInterceptor) this .responseInterceptors
293: .get(i);
294: interceptor.process(response, context);
295: }
296: }
297: }
298:
299: protected void copyInterceptors(final BasicHttpProcessor target) {
300: if (this .requestInterceptors != null) {
301: target.requestInterceptors = new ArrayList(
302: this .requestInterceptors);
303: }
304: if (this .responseInterceptors != null) {
305: target.responseInterceptors = new ArrayList(
306: this .responseInterceptors);
307: }
308: }
309:
310: /**
311: * Creates a copy of this instance
312: *
313: * @return new instance of the BasicHttpProcessor
314: */
315: public BasicHttpProcessor copy() {
316: BasicHttpProcessor clone = new BasicHttpProcessor();
317: copyInterceptors(clone);
318: return clone;
319: }
320:
321: public Object clone() throws CloneNotSupportedException {
322: BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
323: copyInterceptors(clone);
324: return clone;
325: }
326:
327: }
|