Home | History | Annotate | Download | only in protocol
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/BasicHttpProcessor.java $
      3  * $Revision: 613298 $
      4  * $Date: 2008-01-18 14:09:22 -0800 (Fri, 18 Jan 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.protocol;
     33 
     34 import java.io.IOException;
     35 import java.util.ArrayList;
     36 import java.util.Iterator;
     37 import java.util.List;
     38 
     39 import org.apache.http.HttpException;
     40 import org.apache.http.HttpRequest;
     41 import org.apache.http.HttpRequestInterceptor;
     42 import org.apache.http.HttpResponse;
     43 import org.apache.http.HttpResponseInterceptor;
     44 
     45 /**
     46  * Keeps lists of interceptors for processing requests and responses.
     47  *
     48  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     49  * @author Andrea Selva
     50  *
     51  * @version $Revision: 613298 $
     52  *
     53  * @since 4.0
     54  *
     55  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     56  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     57  *     for further details.
     58  */
     59 @Deprecated
     60 public final class BasicHttpProcessor implements
     61     HttpProcessor, HttpRequestInterceptorList, HttpResponseInterceptorList, Cloneable {
     62 
     63     protected List requestInterceptors = null;
     64     protected List responseInterceptors = null;
     65 
     66 
     67     // non-Javadoc, see interface HttpRequestInterceptorList
     68     public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
     69 
     70         if (itcp == null) {
     71             return;
     72         }
     73         if (this.requestInterceptors == null) {
     74             this.requestInterceptors = new ArrayList();
     75         }
     76         this.requestInterceptors.add(itcp);
     77     }
     78 
     79     // non-Javadoc, see interface HttpRequestInterceptorList
     80     public void addRequestInterceptor(final HttpRequestInterceptor itcp,
     81                                       int index) {
     82         if (index < 0) {
     83             throw new IndexOutOfBoundsException(String.valueOf(index));
     84         }
     85         if (itcp == null) {
     86             return;
     87         }
     88 
     89         if (this.requestInterceptors == null) {
     90             if (index > 0) {
     91                 throw new IndexOutOfBoundsException(String.valueOf(index));
     92             }
     93             this.requestInterceptors = new ArrayList();
     94         }
     95         this.requestInterceptors.add(index, itcp);
     96     }
     97 
     98 
     99     public void addResponseInterceptor(HttpResponseInterceptor itcp,
    100                                        int index) {
    101         if (index < 0) {
    102             throw new IndexOutOfBoundsException(String.valueOf(index));
    103         }
    104         if (itcp == null) {
    105             return;
    106         }
    107 
    108         if (this.responseInterceptors == null) {
    109             if (index > 0) {
    110                 throw new IndexOutOfBoundsException(String.valueOf(index));
    111             }
    112             this.responseInterceptors = new ArrayList();
    113         }
    114         this.responseInterceptors.add(index, itcp);
    115     }
    116 
    117 
    118     // non-Javadoc, see interface HttpRequestInterceptorList
    119     public void removeRequestInterceptorByClass(final Class clazz) {
    120         if (this.requestInterceptors == null) {
    121             return;
    122         }
    123         for (Iterator it = this.requestInterceptors.iterator();
    124              it.hasNext(); ) {
    125             Object request = it.next();
    126             if (request.getClass().equals(clazz)) {
    127                 it.remove();
    128             }
    129         }
    130     }
    131 
    132     // non-Javadoc, see interface HttpResponseInterceptorList
    133     public void removeResponseInterceptorByClass(final Class clazz) {
    134         if (this.responseInterceptors == null) {
    135             return;
    136         }
    137         for (Iterator it = this.responseInterceptors.iterator();
    138              it.hasNext(); ) {
    139             Object request = it.next();
    140             if (request.getClass().equals(clazz)) {
    141                 it.remove();
    142             }
    143         }
    144     }
    145 
    146     /**
    147      * Same as {@link #addRequestInterceptor(HttpRequestInterceptor) addRequestInterceptor}.
    148      *
    149      * @param interceptor       the interceptor to add
    150      */
    151     public final
    152             void addInterceptor(final HttpRequestInterceptor interceptor) {
    153         addRequestInterceptor(interceptor);
    154     }
    155 
    156      public final
    157             void addInterceptor(final HttpRequestInterceptor interceptor,
    158                                 int index) {
    159         addRequestInterceptor(interceptor, index);
    160     }
    161 
    162 
    163     // non-Javadoc, see interface HttpRequestInterceptorList
    164     public int getRequestInterceptorCount() {
    165         return (this.requestInterceptors == null) ?
    166             0 : this.requestInterceptors.size();
    167     }
    168 
    169 
    170     // non-Javadoc, see interface HttpRequestInterceptorList
    171     public HttpRequestInterceptor getRequestInterceptor(int index) {
    172 
    173         if ((this.requestInterceptors == null) ||
    174                 (index < 0) || (index >= this.requestInterceptors.size()))
    175             return null;
    176 
    177         return (HttpRequestInterceptor) this.requestInterceptors.get(index);
    178     }
    179 
    180 
    181     // non-Javadoc, see interface HttpRequestInterceptorList
    182     public void clearRequestInterceptors() {
    183         this.requestInterceptors = null;
    184     }
    185 
    186 
    187 
    188     // non-Javadoc, see interface HttpResponseInterceptorList
    189     public void addResponseInterceptor(final HttpResponseInterceptor itcp) {
    190         if (itcp == null) {
    191             return;
    192         }
    193         if (this.responseInterceptors == null) {
    194             this.responseInterceptors = new ArrayList();
    195         }
    196         this.responseInterceptors.add(itcp);
    197     }
    198 
    199     /**
    200      * Same as {@link #addResponseInterceptor(HttpResponseInterceptor) addResponseInterceptor}.
    201      *
    202      * @param interceptor       the interceptor to add
    203      */
    204     public final
    205             void addInterceptor(final HttpResponseInterceptor interceptor) {
    206         addResponseInterceptor(interceptor);
    207     }
    208 
    209     public final void addInterceptor(final HttpResponseInterceptor interceptor,
    210                                      int index) {
    211         addResponseInterceptor(interceptor, index);
    212     }
    213 
    214 
    215 
    216     // non-Javadoc, see interface HttpResponseInterceptorList
    217     public int getResponseInterceptorCount() {
    218         return (this.responseInterceptors == null) ?
    219             0 : this.responseInterceptors.size();
    220     }
    221 
    222 
    223     // non-Javadoc, see interface HttpResponseInterceptorList
    224     public HttpResponseInterceptor getResponseInterceptor(int index) {
    225 
    226         if ((this.responseInterceptors == null) ||
    227                 (index < 0) || (index >= this.responseInterceptors.size()))
    228             return null;
    229 
    230         return (HttpResponseInterceptor) this.responseInterceptors.get(index);
    231     }
    232 
    233 
    234     // non-Javadoc, see interface HttpResponseInterceptorList
    235     public void clearResponseInterceptors() {
    236         this.responseInterceptors = null;
    237     }
    238 
    239 
    240     /**
    241      * Sets the interceptor lists.
    242      * First, both interceptor lists maintained by this processor
    243      * will be cleared.
    244      * Subsequently,
    245      * elements of the argument list that are request interceptors will be
    246      * added to the request interceptor list.
    247      * Elements that are response interceptors will be
    248      * added to the response interceptor list.
    249      * Elements that are both request and response interceptor will be
    250      * added to both lists.
    251      * Elements that are neither request nor response interceptor
    252      * will be ignored.
    253      *
    254      * @param list      the list of request and response interceptors
    255      *                  from which to initialize
    256      */
    257     public void setInterceptors(final List list) {
    258         if (list == null) {
    259             throw new IllegalArgumentException("List must not be null.");
    260         }
    261         if (this.requestInterceptors != null) {
    262             this.requestInterceptors.clear();
    263         }
    264         if (this.responseInterceptors != null) {
    265             this.responseInterceptors.clear();
    266         }
    267         for (int i = 0; i < list.size(); i++) {
    268             Object obj = list.get(i);
    269             if (obj instanceof HttpRequestInterceptor) {
    270                 addInterceptor((HttpRequestInterceptor)obj);
    271             }
    272             if (obj instanceof HttpResponseInterceptor) {
    273                 addInterceptor((HttpResponseInterceptor)obj);
    274             }
    275         }
    276     }
    277 
    278     /**
    279      * Clears both interceptor lists maintained by this processor.
    280      */
    281     public void clearInterceptors() {
    282         clearRequestInterceptors();
    283         clearResponseInterceptors();
    284     }
    285 
    286     // non-Javadoc, see interface HttpRequestInterceptor (via HttpProcessor)
    287     public void process(
    288             final HttpRequest request,
    289             final HttpContext context)
    290             throws IOException, HttpException {
    291         if (this.requestInterceptors != null) {
    292             for (int i = 0; i < this.requestInterceptors.size(); i++) {
    293                 HttpRequestInterceptor interceptor =
    294                     (HttpRequestInterceptor) this.requestInterceptors.get(i);
    295                 interceptor.process(request, context);
    296             }
    297         }
    298     }
    299 
    300     // non-Javadoc, see interface HttpResponseInterceptor (via HttpProcessor)
    301     public void process(
    302             final HttpResponse response,
    303             final HttpContext context)
    304             throws IOException, HttpException {
    305         if (this.responseInterceptors != null) {
    306             for (int i = 0; i < this.responseInterceptors.size(); i++) {
    307                 HttpResponseInterceptor interceptor =
    308                     (HttpResponseInterceptor) this.responseInterceptors.get(i);
    309                 interceptor.process(response, context);
    310             }
    311         }
    312     }
    313 
    314     protected void copyInterceptors(final BasicHttpProcessor target) {
    315         if (this.requestInterceptors != null) {
    316             target.requestInterceptors =
    317                 new ArrayList(this.requestInterceptors);
    318         }
    319         if (this.responseInterceptors != null) {
    320             target.responseInterceptors =
    321                 new ArrayList(this.responseInterceptors);
    322         }
    323     }
    324 
    325     /**
    326      * Creates a copy of this instance
    327      *
    328      * @return new instance of the BasicHttpProcessor
    329      */
    330     public BasicHttpProcessor copy() {
    331         BasicHttpProcessor clone = new BasicHttpProcessor();
    332         copyInterceptors(clone);
    333         return clone;
    334     }
    335 
    336     public Object clone() throws CloneNotSupportedException {
    337         BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
    338         copyInterceptors(clone);
    339         return clone;
    340     }
    341 
    342 }
    343