mobile/android/thirdparty/ch/boye/httpclientandroidlib/protocol/BasicHttpProcessor.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/protocol/BasicHttpProcessor.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,245 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.protocol;
    1.32 +
    1.33 +import java.io.IOException;
    1.34 +import java.util.ArrayList;
    1.35 +import java.util.Iterator;
    1.36 +import java.util.List;
    1.37 +
    1.38 +import ch.boye.httpclientandroidlib.HttpException;
    1.39 +import ch.boye.httpclientandroidlib.HttpRequest;
    1.40 +import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
    1.41 +import ch.boye.httpclientandroidlib.HttpResponse;
    1.42 +import ch.boye.httpclientandroidlib.HttpResponseInterceptor;
    1.43 +
    1.44 +/**
    1.45 + * Default implementation of {@link HttpProcessor}.
    1.46 + * <p>
    1.47 + * Please note access to the internal structures of this class is not
    1.48 + * synchronized and therefore this class may be thread-unsafe.
    1.49 + *
    1.50 + * @since 4.0
    1.51 + */
    1.52 +//@NotThreadSafe // Lists are not synchronized
    1.53 +public final class BasicHttpProcessor implements
    1.54 +    HttpProcessor, HttpRequestInterceptorList, HttpResponseInterceptorList, Cloneable {
    1.55 +
    1.56 +    // Don't allow direct access, as nulls are not allowed
    1.57 +    protected final List requestInterceptors = new ArrayList();
    1.58 +    protected final List responseInterceptors = new ArrayList();
    1.59 +
    1.60 +    public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
    1.61 +        if (itcp == null) {
    1.62 +            return;
    1.63 +        }
    1.64 +        this.requestInterceptors.add(itcp);
    1.65 +    }
    1.66 +
    1.67 +    public void addRequestInterceptor(
    1.68 +            final HttpRequestInterceptor itcp, int index) {
    1.69 +        if (itcp == null) {
    1.70 +            return;
    1.71 +        }
    1.72 +        this.requestInterceptors.add(index, itcp);
    1.73 +    }
    1.74 +
    1.75 +    public void addResponseInterceptor(
    1.76 +            final HttpResponseInterceptor itcp, int index) {
    1.77 +        if (itcp == null) {
    1.78 +            return;
    1.79 +        }
    1.80 +        this.responseInterceptors.add(index, itcp);
    1.81 +    }
    1.82 +
    1.83 +    public void removeRequestInterceptorByClass(final Class clazz) {
    1.84 +        for (Iterator it = this.requestInterceptors.iterator();
    1.85 +             it.hasNext(); ) {
    1.86 +            Object request = it.next();
    1.87 +            if (request.getClass().equals(clazz)) {
    1.88 +                it.remove();
    1.89 +            }
    1.90 +        }
    1.91 +    }
    1.92 +
    1.93 +    public void removeResponseInterceptorByClass(final Class clazz) {
    1.94 +        for (Iterator it = this.responseInterceptors.iterator();
    1.95 +             it.hasNext(); ) {
    1.96 +            Object request = it.next();
    1.97 +            if (request.getClass().equals(clazz)) {
    1.98 +                it.remove();
    1.99 +            }
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    public final void addInterceptor(final HttpRequestInterceptor interceptor) {
   1.104 +        addRequestInterceptor(interceptor);
   1.105 +    }
   1.106 +
   1.107 +     public final void addInterceptor(final HttpRequestInterceptor interceptor, int index) {
   1.108 +        addRequestInterceptor(interceptor, index);
   1.109 +    }
   1.110 +
   1.111 +    public int getRequestInterceptorCount() {
   1.112 +        return this.requestInterceptors.size();
   1.113 +    }
   1.114 +
   1.115 +    public HttpRequestInterceptor getRequestInterceptor(int index) {
   1.116 +        if ((index < 0) || (index >= this.requestInterceptors.size()))
   1.117 +            return null;
   1.118 +        return (HttpRequestInterceptor) this.requestInterceptors.get(index);
   1.119 +    }
   1.120 +
   1.121 +    public void clearRequestInterceptors() {
   1.122 +        this.requestInterceptors.clear();
   1.123 +    }
   1.124 +
   1.125 +    public void addResponseInterceptor(final HttpResponseInterceptor itcp) {
   1.126 +        if (itcp == null) {
   1.127 +            return;
   1.128 +        }
   1.129 +        this.responseInterceptors.add(itcp);
   1.130 +    }
   1.131 +
   1.132 +    public final void addInterceptor(final HttpResponseInterceptor interceptor) {
   1.133 +        addResponseInterceptor(interceptor);
   1.134 +    }
   1.135 +
   1.136 +    public final void addInterceptor(final HttpResponseInterceptor interceptor, int index) {
   1.137 +        addResponseInterceptor(interceptor, index);
   1.138 +    }
   1.139 +
   1.140 +    public int getResponseInterceptorCount() {
   1.141 +        return this.responseInterceptors.size();
   1.142 +    }
   1.143 +
   1.144 +    public HttpResponseInterceptor getResponseInterceptor(int index) {
   1.145 +        if ((index < 0) || (index >= this.responseInterceptors.size()))
   1.146 +            return null;
   1.147 +        return (HttpResponseInterceptor) this.responseInterceptors.get(index);
   1.148 +    }
   1.149 +
   1.150 +    public void clearResponseInterceptors() {
   1.151 +        this.responseInterceptors.clear();
   1.152 +    }
   1.153 +
   1.154 +    /**
   1.155 +     * Sets the interceptor lists.
   1.156 +     * First, both interceptor lists maintained by this processor
   1.157 +     * will be cleared.
   1.158 +     * Subsequently,
   1.159 +     * elements of the argument list that are request interceptors will be
   1.160 +     * added to the request interceptor list.
   1.161 +     * Elements that are response interceptors will be
   1.162 +     * added to the response interceptor list.
   1.163 +     * Elements that are both request and response interceptor will be
   1.164 +     * added to both lists.
   1.165 +     * Elements that are neither request nor response interceptor
   1.166 +     * will be ignored.
   1.167 +     *
   1.168 +     * @param list      the list of request and response interceptors
   1.169 +     *                  from which to initialize
   1.170 +     */
   1.171 +    public void setInterceptors(final List list) {
   1.172 +        if (list == null) {
   1.173 +            throw new IllegalArgumentException("List must not be null.");
   1.174 +        }
   1.175 +        this.requestInterceptors.clear();
   1.176 +        this.responseInterceptors.clear();
   1.177 +        for (int i = 0; i < list.size(); i++) {
   1.178 +            Object obj = list.get(i);
   1.179 +            if (obj instanceof HttpRequestInterceptor) {
   1.180 +                addInterceptor((HttpRequestInterceptor)obj);
   1.181 +            }
   1.182 +            if (obj instanceof HttpResponseInterceptor) {
   1.183 +                addInterceptor((HttpResponseInterceptor)obj);
   1.184 +            }
   1.185 +        }
   1.186 +    }
   1.187 +
   1.188 +    /**
   1.189 +     * Clears both interceptor lists maintained by this processor.
   1.190 +     */
   1.191 +    public void clearInterceptors() {
   1.192 +        clearRequestInterceptors();
   1.193 +        clearResponseInterceptors();
   1.194 +    }
   1.195 +
   1.196 +    public void process(
   1.197 +            final HttpRequest request,
   1.198 +            final HttpContext context)
   1.199 +            throws IOException, HttpException {
   1.200 +        for (int i = 0; i < this.requestInterceptors.size(); i++) {
   1.201 +            HttpRequestInterceptor interceptor =
   1.202 +                (HttpRequestInterceptor) this.requestInterceptors.get(i);
   1.203 +            interceptor.process(request, context);
   1.204 +        }
   1.205 +    }
   1.206 +
   1.207 +    public void process(
   1.208 +            final HttpResponse response,
   1.209 +            final HttpContext context)
   1.210 +            throws IOException, HttpException {
   1.211 +        for (int i = 0; i < this.responseInterceptors.size(); i++) {
   1.212 +            HttpResponseInterceptor interceptor =
   1.213 +                (HttpResponseInterceptor) this.responseInterceptors.get(i);
   1.214 +            interceptor.process(response, context);
   1.215 +        }
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Sets up the target to have the same list of interceptors
   1.220 +     * as the current instance.
   1.221 +     *
   1.222 +     * @param target object to be initialised
   1.223 +     */
   1.224 +    protected void copyInterceptors(final BasicHttpProcessor target) {
   1.225 +        target.requestInterceptors.clear();
   1.226 +        target.requestInterceptors.addAll(this.requestInterceptors);
   1.227 +        target.responseInterceptors.clear();
   1.228 +        target.responseInterceptors.addAll(this.responseInterceptors);
   1.229 +    }
   1.230 +
   1.231 +    /**
   1.232 +     * Creates a copy of this instance
   1.233 +     *
   1.234 +     * @return new instance of the BasicHttpProcessor
   1.235 +     */
   1.236 +    public BasicHttpProcessor copy() {
   1.237 +        BasicHttpProcessor clone = new BasicHttpProcessor();
   1.238 +        copyInterceptors(clone);
   1.239 +        return clone;
   1.240 +    }
   1.241 +
   1.242 +    public Object clone() throws CloneNotSupportedException {
   1.243 +        BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
   1.244 +        copyInterceptors(clone);
   1.245 +        return clone;
   1.246 +    }
   1.247 +
   1.248 +}

mercurial