michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.protocol; michael@0: michael@0: import java.io.IOException; michael@0: import java.util.ArrayList; michael@0: import java.util.Iterator; michael@0: import java.util.List; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpException; michael@0: import ch.boye.httpclientandroidlib.HttpRequest; michael@0: import ch.boye.httpclientandroidlib.HttpRequestInterceptor; michael@0: import ch.boye.httpclientandroidlib.HttpResponse; michael@0: import ch.boye.httpclientandroidlib.HttpResponseInterceptor; michael@0: michael@0: /** michael@0: * Default implementation of {@link HttpProcessor}. michael@0: *

michael@0: * Please note access to the internal structures of this class is not michael@0: * synchronized and therefore this class may be thread-unsafe. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: //@NotThreadSafe // Lists are not synchronized michael@0: public final class BasicHttpProcessor implements michael@0: HttpProcessor, HttpRequestInterceptorList, HttpResponseInterceptorList, Cloneable { michael@0: michael@0: // Don't allow direct access, as nulls are not allowed michael@0: protected final List requestInterceptors = new ArrayList(); michael@0: protected final List responseInterceptors = new ArrayList(); michael@0: michael@0: public void addRequestInterceptor(final HttpRequestInterceptor itcp) { michael@0: if (itcp == null) { michael@0: return; michael@0: } michael@0: this.requestInterceptors.add(itcp); michael@0: } michael@0: michael@0: public void addRequestInterceptor( michael@0: final HttpRequestInterceptor itcp, int index) { michael@0: if (itcp == null) { michael@0: return; michael@0: } michael@0: this.requestInterceptors.add(index, itcp); michael@0: } michael@0: michael@0: public void addResponseInterceptor( michael@0: final HttpResponseInterceptor itcp, int index) { michael@0: if (itcp == null) { michael@0: return; michael@0: } michael@0: this.responseInterceptors.add(index, itcp); michael@0: } michael@0: michael@0: public void removeRequestInterceptorByClass(final Class clazz) { michael@0: for (Iterator it = this.requestInterceptors.iterator(); michael@0: it.hasNext(); ) { michael@0: Object request = it.next(); michael@0: if (request.getClass().equals(clazz)) { michael@0: it.remove(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: public void removeResponseInterceptorByClass(final Class clazz) { michael@0: for (Iterator it = this.responseInterceptors.iterator(); michael@0: it.hasNext(); ) { michael@0: Object request = it.next(); michael@0: if (request.getClass().equals(clazz)) { michael@0: it.remove(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: public final void addInterceptor(final HttpRequestInterceptor interceptor) { michael@0: addRequestInterceptor(interceptor); michael@0: } michael@0: michael@0: public final void addInterceptor(final HttpRequestInterceptor interceptor, int index) { michael@0: addRequestInterceptor(interceptor, index); michael@0: } michael@0: michael@0: public int getRequestInterceptorCount() { michael@0: return this.requestInterceptors.size(); michael@0: } michael@0: michael@0: public HttpRequestInterceptor getRequestInterceptor(int index) { michael@0: if ((index < 0) || (index >= this.requestInterceptors.size())) michael@0: return null; michael@0: return (HttpRequestInterceptor) this.requestInterceptors.get(index); michael@0: } michael@0: michael@0: public void clearRequestInterceptors() { michael@0: this.requestInterceptors.clear(); michael@0: } michael@0: michael@0: public void addResponseInterceptor(final HttpResponseInterceptor itcp) { michael@0: if (itcp == null) { michael@0: return; michael@0: } michael@0: this.responseInterceptors.add(itcp); michael@0: } michael@0: michael@0: public final void addInterceptor(final HttpResponseInterceptor interceptor) { michael@0: addResponseInterceptor(interceptor); michael@0: } michael@0: michael@0: public final void addInterceptor(final HttpResponseInterceptor interceptor, int index) { michael@0: addResponseInterceptor(interceptor, index); michael@0: } michael@0: michael@0: public int getResponseInterceptorCount() { michael@0: return this.responseInterceptors.size(); michael@0: } michael@0: michael@0: public HttpResponseInterceptor getResponseInterceptor(int index) { michael@0: if ((index < 0) || (index >= this.responseInterceptors.size())) michael@0: return null; michael@0: return (HttpResponseInterceptor) this.responseInterceptors.get(index); michael@0: } michael@0: michael@0: public void clearResponseInterceptors() { michael@0: this.responseInterceptors.clear(); michael@0: } michael@0: michael@0: /** michael@0: * Sets the interceptor lists. michael@0: * First, both interceptor lists maintained by this processor michael@0: * will be cleared. michael@0: * Subsequently, michael@0: * elements of the argument list that are request interceptors will be michael@0: * added to the request interceptor list. michael@0: * Elements that are response interceptors will be michael@0: * added to the response interceptor list. michael@0: * Elements that are both request and response interceptor will be michael@0: * added to both lists. michael@0: * Elements that are neither request nor response interceptor michael@0: * will be ignored. michael@0: * michael@0: * @param list the list of request and response interceptors michael@0: * from which to initialize michael@0: */ michael@0: public void setInterceptors(final List list) { michael@0: if (list == null) { michael@0: throw new IllegalArgumentException("List must not be null."); michael@0: } michael@0: this.requestInterceptors.clear(); michael@0: this.responseInterceptors.clear(); michael@0: for (int i = 0; i < list.size(); i++) { michael@0: Object obj = list.get(i); michael@0: if (obj instanceof HttpRequestInterceptor) { michael@0: addInterceptor((HttpRequestInterceptor)obj); michael@0: } michael@0: if (obj instanceof HttpResponseInterceptor) { michael@0: addInterceptor((HttpResponseInterceptor)obj); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Clears both interceptor lists maintained by this processor. michael@0: */ michael@0: public void clearInterceptors() { michael@0: clearRequestInterceptors(); michael@0: clearResponseInterceptors(); michael@0: } michael@0: michael@0: public void process( michael@0: final HttpRequest request, michael@0: final HttpContext context) michael@0: throws IOException, HttpException { michael@0: for (int i = 0; i < this.requestInterceptors.size(); i++) { michael@0: HttpRequestInterceptor interceptor = michael@0: (HttpRequestInterceptor) this.requestInterceptors.get(i); michael@0: interceptor.process(request, context); michael@0: } michael@0: } michael@0: michael@0: public void process( michael@0: final HttpResponse response, michael@0: final HttpContext context) michael@0: throws IOException, HttpException { michael@0: for (int i = 0; i < this.responseInterceptors.size(); i++) { michael@0: HttpResponseInterceptor interceptor = michael@0: (HttpResponseInterceptor) this.responseInterceptors.get(i); michael@0: interceptor.process(response, context); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Sets up the target to have the same list of interceptors michael@0: * as the current instance. michael@0: * michael@0: * @param target object to be initialised michael@0: */ michael@0: protected void copyInterceptors(final BasicHttpProcessor target) { michael@0: target.requestInterceptors.clear(); michael@0: target.requestInterceptors.addAll(this.requestInterceptors); michael@0: target.responseInterceptors.clear(); michael@0: target.responseInterceptors.addAll(this.responseInterceptors); michael@0: } michael@0: michael@0: /** michael@0: * Creates a copy of this instance michael@0: * michael@0: * @return new instance of the BasicHttpProcessor michael@0: */ michael@0: public BasicHttpProcessor copy() { michael@0: BasicHttpProcessor clone = new BasicHttpProcessor(); michael@0: copyInterceptors(clone); michael@0: return clone; michael@0: } michael@0: michael@0: public Object clone() throws CloneNotSupportedException { michael@0: BasicHttpProcessor clone = (BasicHttpProcessor) super.clone(); michael@0: copyInterceptors(clone); michael@0: return clone; michael@0: } michael@0: michael@0: }