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

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.protocol;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31 import java.util.ArrayList;
michael@0 32 import java.util.Iterator;
michael@0 33 import java.util.List;
michael@0 34
michael@0 35 import ch.boye.httpclientandroidlib.HttpException;
michael@0 36 import ch.boye.httpclientandroidlib.HttpRequest;
michael@0 37 import ch.boye.httpclientandroidlib.HttpRequestInterceptor;
michael@0 38 import ch.boye.httpclientandroidlib.HttpResponse;
michael@0 39 import ch.boye.httpclientandroidlib.HttpResponseInterceptor;
michael@0 40
michael@0 41 /**
michael@0 42 * Default implementation of {@link HttpProcessor}.
michael@0 43 * <p>
michael@0 44 * Please note access to the internal structures of this class is not
michael@0 45 * synchronized and therefore this class may be thread-unsafe.
michael@0 46 *
michael@0 47 * @since 4.0
michael@0 48 */
michael@0 49 //@NotThreadSafe // Lists are not synchronized
michael@0 50 public final class BasicHttpProcessor implements
michael@0 51 HttpProcessor, HttpRequestInterceptorList, HttpResponseInterceptorList, Cloneable {
michael@0 52
michael@0 53 // Don't allow direct access, as nulls are not allowed
michael@0 54 protected final List requestInterceptors = new ArrayList();
michael@0 55 protected final List responseInterceptors = new ArrayList();
michael@0 56
michael@0 57 public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
michael@0 58 if (itcp == null) {
michael@0 59 return;
michael@0 60 }
michael@0 61 this.requestInterceptors.add(itcp);
michael@0 62 }
michael@0 63
michael@0 64 public void addRequestInterceptor(
michael@0 65 final HttpRequestInterceptor itcp, int index) {
michael@0 66 if (itcp == null) {
michael@0 67 return;
michael@0 68 }
michael@0 69 this.requestInterceptors.add(index, itcp);
michael@0 70 }
michael@0 71
michael@0 72 public void addResponseInterceptor(
michael@0 73 final HttpResponseInterceptor itcp, int index) {
michael@0 74 if (itcp == null) {
michael@0 75 return;
michael@0 76 }
michael@0 77 this.responseInterceptors.add(index, itcp);
michael@0 78 }
michael@0 79
michael@0 80 public void removeRequestInterceptorByClass(final Class clazz) {
michael@0 81 for (Iterator it = this.requestInterceptors.iterator();
michael@0 82 it.hasNext(); ) {
michael@0 83 Object request = it.next();
michael@0 84 if (request.getClass().equals(clazz)) {
michael@0 85 it.remove();
michael@0 86 }
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 public void removeResponseInterceptorByClass(final Class clazz) {
michael@0 91 for (Iterator it = this.responseInterceptors.iterator();
michael@0 92 it.hasNext(); ) {
michael@0 93 Object request = it.next();
michael@0 94 if (request.getClass().equals(clazz)) {
michael@0 95 it.remove();
michael@0 96 }
michael@0 97 }
michael@0 98 }
michael@0 99
michael@0 100 public final void addInterceptor(final HttpRequestInterceptor interceptor) {
michael@0 101 addRequestInterceptor(interceptor);
michael@0 102 }
michael@0 103
michael@0 104 public final void addInterceptor(final HttpRequestInterceptor interceptor, int index) {
michael@0 105 addRequestInterceptor(interceptor, index);
michael@0 106 }
michael@0 107
michael@0 108 public int getRequestInterceptorCount() {
michael@0 109 return this.requestInterceptors.size();
michael@0 110 }
michael@0 111
michael@0 112 public HttpRequestInterceptor getRequestInterceptor(int index) {
michael@0 113 if ((index < 0) || (index >= this.requestInterceptors.size()))
michael@0 114 return null;
michael@0 115 return (HttpRequestInterceptor) this.requestInterceptors.get(index);
michael@0 116 }
michael@0 117
michael@0 118 public void clearRequestInterceptors() {
michael@0 119 this.requestInterceptors.clear();
michael@0 120 }
michael@0 121
michael@0 122 public void addResponseInterceptor(final HttpResponseInterceptor itcp) {
michael@0 123 if (itcp == null) {
michael@0 124 return;
michael@0 125 }
michael@0 126 this.responseInterceptors.add(itcp);
michael@0 127 }
michael@0 128
michael@0 129 public final void addInterceptor(final HttpResponseInterceptor interceptor) {
michael@0 130 addResponseInterceptor(interceptor);
michael@0 131 }
michael@0 132
michael@0 133 public final void addInterceptor(final HttpResponseInterceptor interceptor, int index) {
michael@0 134 addResponseInterceptor(interceptor, index);
michael@0 135 }
michael@0 136
michael@0 137 public int getResponseInterceptorCount() {
michael@0 138 return this.responseInterceptors.size();
michael@0 139 }
michael@0 140
michael@0 141 public HttpResponseInterceptor getResponseInterceptor(int index) {
michael@0 142 if ((index < 0) || (index >= this.responseInterceptors.size()))
michael@0 143 return null;
michael@0 144 return (HttpResponseInterceptor) this.responseInterceptors.get(index);
michael@0 145 }
michael@0 146
michael@0 147 public void clearResponseInterceptors() {
michael@0 148 this.responseInterceptors.clear();
michael@0 149 }
michael@0 150
michael@0 151 /**
michael@0 152 * Sets the interceptor lists.
michael@0 153 * First, both interceptor lists maintained by this processor
michael@0 154 * will be cleared.
michael@0 155 * Subsequently,
michael@0 156 * elements of the argument list that are request interceptors will be
michael@0 157 * added to the request interceptor list.
michael@0 158 * Elements that are response interceptors will be
michael@0 159 * added to the response interceptor list.
michael@0 160 * Elements that are both request and response interceptor will be
michael@0 161 * added to both lists.
michael@0 162 * Elements that are neither request nor response interceptor
michael@0 163 * will be ignored.
michael@0 164 *
michael@0 165 * @param list the list of request and response interceptors
michael@0 166 * from which to initialize
michael@0 167 */
michael@0 168 public void setInterceptors(final List list) {
michael@0 169 if (list == null) {
michael@0 170 throw new IllegalArgumentException("List must not be null.");
michael@0 171 }
michael@0 172 this.requestInterceptors.clear();
michael@0 173 this.responseInterceptors.clear();
michael@0 174 for (int i = 0; i < list.size(); i++) {
michael@0 175 Object obj = list.get(i);
michael@0 176 if (obj instanceof HttpRequestInterceptor) {
michael@0 177 addInterceptor((HttpRequestInterceptor)obj);
michael@0 178 }
michael@0 179 if (obj instanceof HttpResponseInterceptor) {
michael@0 180 addInterceptor((HttpResponseInterceptor)obj);
michael@0 181 }
michael@0 182 }
michael@0 183 }
michael@0 184
michael@0 185 /**
michael@0 186 * Clears both interceptor lists maintained by this processor.
michael@0 187 */
michael@0 188 public void clearInterceptors() {
michael@0 189 clearRequestInterceptors();
michael@0 190 clearResponseInterceptors();
michael@0 191 }
michael@0 192
michael@0 193 public void process(
michael@0 194 final HttpRequest request,
michael@0 195 final HttpContext context)
michael@0 196 throws IOException, HttpException {
michael@0 197 for (int i = 0; i < this.requestInterceptors.size(); i++) {
michael@0 198 HttpRequestInterceptor interceptor =
michael@0 199 (HttpRequestInterceptor) this.requestInterceptors.get(i);
michael@0 200 interceptor.process(request, context);
michael@0 201 }
michael@0 202 }
michael@0 203
michael@0 204 public void process(
michael@0 205 final HttpResponse response,
michael@0 206 final HttpContext context)
michael@0 207 throws IOException, HttpException {
michael@0 208 for (int i = 0; i < this.responseInterceptors.size(); i++) {
michael@0 209 HttpResponseInterceptor interceptor =
michael@0 210 (HttpResponseInterceptor) this.responseInterceptors.get(i);
michael@0 211 interceptor.process(response, context);
michael@0 212 }
michael@0 213 }
michael@0 214
michael@0 215 /**
michael@0 216 * Sets up the target to have the same list of interceptors
michael@0 217 * as the current instance.
michael@0 218 *
michael@0 219 * @param target object to be initialised
michael@0 220 */
michael@0 221 protected void copyInterceptors(final BasicHttpProcessor target) {
michael@0 222 target.requestInterceptors.clear();
michael@0 223 target.requestInterceptors.addAll(this.requestInterceptors);
michael@0 224 target.responseInterceptors.clear();
michael@0 225 target.responseInterceptors.addAll(this.responseInterceptors);
michael@0 226 }
michael@0 227
michael@0 228 /**
michael@0 229 * Creates a copy of this instance
michael@0 230 *
michael@0 231 * @return new instance of the BasicHttpProcessor
michael@0 232 */
michael@0 233 public BasicHttpProcessor copy() {
michael@0 234 BasicHttpProcessor clone = new BasicHttpProcessor();
michael@0 235 copyInterceptors(clone);
michael@0 236 return clone;
michael@0 237 }
michael@0 238
michael@0 239 public Object clone() throws CloneNotSupportedException {
michael@0 240 BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
michael@0 241 copyInterceptors(clone);
michael@0 242 return clone;
michael@0 243 }
michael@0 244
michael@0 245 }

mercurial