package org.apache.http.protocol;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.util.Args;
@Deprecated
public final class BasicHttpProcessor implements
HttpProcessor, HttpRequestInterceptorList, HttpResponseInterceptorList, Cloneable {
protected final List<HttpRequestInterceptor> requestInterceptors = new ArrayList<HttpRequestInterceptor>();
protected final List<HttpResponseInterceptor> responseInterceptors = new ArrayList<HttpResponseInterceptor>();
@Override
public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
if (itcp == null) {
return;
}
this.requestInterceptors.add(itcp);
}
@Override
public void addRequestInterceptor(
final HttpRequestInterceptor itcp, final int index) {
if (itcp == null) {
return;
}
this.requestInterceptors.add(index, itcp);
}
@Override
public void addResponseInterceptor(
final HttpResponseInterceptor itcp, final int index) {
if (itcp == null) {
return;
}
this.responseInterceptors.add(index, itcp);
}
@Override
public void removeRequestInterceptorByClass(final Class<? extends HttpRequestInterceptor> clazz) {
for (final Iterator<HttpRequestInterceptor> it = this.requestInterceptors.iterator();
it.hasNext(); ) {
final Object request = it.next();
if (request.getClass().equals(clazz)) {
it.remove();
}
}
}
@Override
public void removeResponseInterceptorByClass(final Class<? extends HttpResponseInterceptor> clazz) {
for (final Iterator<HttpResponseInterceptor> it = this.responseInterceptors.iterator();
it.hasNext(); ) {
final Object request = it.next();
if (request.getClass().equals(clazz)) {
it.remove();
}
}
}
public void addInterceptor(final HttpRequestInterceptor interceptor) {
addRequestInterceptor(interceptor);
}
public void addInterceptor(final HttpRequestInterceptor interceptor, final int index) {
addRequestInterceptor(interceptor, index);
}
@Override
public int getRequestInterceptorCount() {
return this.requestInterceptors.size();
}
@Override
public HttpRequestInterceptor getRequestInterceptor(final int index) {
if ((index < 0) || (index >= this.requestInterceptors.size())) {
return null;
}
return this.requestInterceptors.get(index);
}
@Override
public void clearRequestInterceptors() {
this.requestInterceptors.clear();
}
@Override
public void addResponseInterceptor(final HttpResponseInterceptor itcp) {
if (itcp == null) {
return;
}
this.responseInterceptors.add(itcp);
}
public void addInterceptor(final HttpResponseInterceptor interceptor) {
addResponseInterceptor(interceptor);
}
public void addInterceptor(final HttpResponseInterceptor interceptor, final int index) {
addResponseInterceptor(interceptor, index);
}
@Override
public int getResponseInterceptorCount() {
return this.responseInterceptors.size();
}
@Override
public HttpResponseInterceptor getResponseInterceptor(final int index) {
if ((index < 0) || (index >= this.responseInterceptors.size())) {
return null;
}
return this.responseInterceptors.get(index);
}
@Override
public void clearResponseInterceptors() {
this.responseInterceptors.clear();
}
@Override
public void setInterceptors(final List<?> list) {
Args.notNull(list, "Inteceptor list");
this.requestInterceptors.clear();
this.responseInterceptors.clear();
for (final Object obj : list) {
if (obj instanceof HttpRequestInterceptor) {
addInterceptor((HttpRequestInterceptor) obj);
}
if (obj instanceof HttpResponseInterceptor) {
addInterceptor((HttpResponseInterceptor) obj);
}
}
}
public void clearInterceptors() {
clearRequestInterceptors();
clearResponseInterceptors();
}
@Override
public void process(
final HttpRequest request,
final HttpContext context)
throws IOException, HttpException {
for (final HttpRequestInterceptor interceptor : this.requestInterceptors) {
interceptor.process(request, context);
}
}
@Override
public void process(
final HttpResponse response,
final HttpContext context)
throws IOException, HttpException {
for (final HttpResponseInterceptor interceptor : this.responseInterceptors) {
interceptor.process(response, context);
}
}
protected void copyInterceptors(final BasicHttpProcessor target) {
target.requestInterceptors.clear();
target.requestInterceptors.addAll(this.requestInterceptors);
target.responseInterceptors.clear();
target.responseInterceptors.addAll(this.responseInterceptors);
}
public BasicHttpProcessor copy() {
final BasicHttpProcessor clone = new BasicHttpProcessor();
copyInterceptors(clone);
return clone;
}
@Override
public Object clone() throws CloneNotSupportedException {
final BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
copyInterceptors(clone);
return clone;
}
}