View Javadoc

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