Home » HttpComponents-Core-4.0.1 » org.apache.http.protocol » [javadoc | source]

    1   /*
    2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/protocol/AbstractHttpProcessor.java $
    3    * $Revision: 378645 $
    4    * $Date: 2006-02-17 23:34:54 +0100 (Fri, 17 Feb 2006) $
    5    *
    6    * ====================================================================
    7    *
    8    *  Copyright 1999-2006 The Apache Software Foundation
    9    *
   10    *  Licensed under the Apache License, Version 2.0 (the "License");
   11    *  you may not use this file except in compliance with the License.
   12    *  You may obtain a copy of the License at
   13    *
   14    *      http://www.apache.org/licenses/LICENSE-2.0
   15    *
   16    *  Unless required by applicable law or agreed to in writing, software
   17    *  distributed under the License is distributed on an "AS IS" BASIS,
   18    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   19    *  See the License for the specific language governing permissions and
   20    *  limitations under the License.
   21    * ====================================================================
   22    *
   23    * This software consists of voluntary contributions made by many
   24    * individuals on behalf of the Apache Software Foundation.  For more
   25    * information on the Apache Software Foundation, please see
   26    * <http://www.apache.org/>.
   27    *
   28    */
   29   
   30   package org.apache.http.protocol;
   31   
   32   import java.io.IOException;
   33   import java.util.ArrayList;
   34   import java.util.Iterator;
   35   import java.util.List;
   36   
   37   import org.apache.http.HttpException;
   38   import org.apache.http.HttpRequest;
   39   import org.apache.http.HttpRequestInterceptor;
   40   import org.apache.http.HttpResponse;
   41   import org.apache.http.HttpResponseInterceptor;
   42   
   43   /**
   44    * Keeps lists of interceptors for processing requests and responses.
   45    *
   46    * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
   47    *
   48    * @version $Revision: 378645 $
   49    * 
   50    * @since 4.0
   51    */
   52   public abstract class AbstractHttpProcessor {
   53   
   54       private List requestInterceptors = null; 
   55       private List responseInterceptors = null; 
   56       
   57       public void addInterceptor(final HttpRequestInterceptor interceptor) {
   58           if (interceptor == null) {
   59               return;
   60           }
   61           if (this.requestInterceptors == null) {
   62               this.requestInterceptors = new ArrayList();
   63           }
   64           this.requestInterceptors.add(interceptor);
   65       }
   66       
   67       public void removeInterceptor(final HttpRequestInterceptor interceptor) {
   68           if (interceptor == null) {
   69               return;
   70           }
   71           if (this.requestInterceptors == null) {
   72               return;
   73           }
   74           this.requestInterceptors.remove(interceptor);
   75           if (this.requestInterceptors.isEmpty()) {
   76               this.requestInterceptors = null;
   77           }
   78       }
   79       
   80       public void addInterceptor(final HttpResponseInterceptor interceptor) {
   81           if (interceptor == null) {
   82               return;
   83           }
   84           if (this.responseInterceptors == null) {
   85               this.responseInterceptors = new ArrayList();
   86           }
   87           this.responseInterceptors.add(interceptor);
   88       }
   89       
   90       public void removeInterceptor(final HttpResponseInterceptor interceptor) {
   91           if (interceptor == null) {
   92               return;
   93           }
   94           if (this.responseInterceptors == null) {
   95               return;
   96           }
   97           this.responseInterceptors.remove(interceptor);
   98           if (this.responseInterceptors.isEmpty()) {
   99               this.responseInterceptors = null;
  100           }
  101       }
  102       
  103       public void removeInterceptors(final Class clazz) {
  104           if (clazz == null) {
  105               return;
  106           }
  107           if (this.requestInterceptors != null) {
  108               for (Iterator i = this.requestInterceptors.iterator(); i.hasNext(); ) {
  109                   if (clazz.isInstance(i.next())) {
  110                       i.remove();
  111                   }
  112               }
  113           }
  114           if (this.responseInterceptors != null) {
  115               for (Iterator i = this.responseInterceptors.iterator(); i.hasNext(); ) {
  116                   if (clazz.isInstance(i.next())) {
  117                       i.remove();
  118                   }
  119               }
  120           }
  121       }
  122   
  123       public void setInterceptors(final List list) {
  124           if (list == null) {
  125               return;
  126           }
  127           if (this.requestInterceptors != null) {
  128               this.requestInterceptors.clear();
  129           }
  130           if (this.responseInterceptors != null) {
  131               this.responseInterceptors.clear();
  132           }
  133           for (int i = 0; i < list.size(); i++) {
  134               Object obj = list.get(i);
  135               if (obj instanceof HttpRequestInterceptor) {
  136                   addInterceptor((HttpRequestInterceptor)obj);
  137               }
  138               if (obj instanceof HttpResponseInterceptor) {
  139                   addInterceptor((HttpResponseInterceptor)obj);
  140               }
  141           }
  142       }
  143       
  144       public void clearInterceptors() {
  145           this.requestInterceptors = null;
  146           this.responseInterceptors = null;
  147       }
  148       
  149       protected void preprocessRequest(
  150               final HttpRequest request,
  151               final HttpContext context) 
  152                   throws IOException, HttpException {
  153           if (this.requestInterceptors != null) {
  154               for (int i = 0; i < this.requestInterceptors.size(); i++) {
  155                   HttpRequestInterceptor interceptor = (HttpRequestInterceptor) this.requestInterceptors.get(i);
  156                   interceptor.process(request, context);
  157               }
  158           }
  159       }
  160   
  161       protected void postprocessResponse(
  162               final HttpResponse response,
  163               final HttpContext context) 
  164                   throws IOException, HttpException {
  165           if (this.responseInterceptors != null) {
  166               for (int i = 0; i < this.responseInterceptors.size(); i++) {
  167                   HttpResponseInterceptor interceptor = (HttpResponseInterceptor) this.responseInterceptors.get(i);
  168                   interceptor.process(response, context);
  169               }
  170           }
  171       }
  172       
  173   }

Home » HttpComponents-Core-4.0.1 » org.apache.http.protocol » [javadoc | source]