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.util.Args;
41  
42  /**
43   * Default implementation of {@link HttpProcessor}.
44   * <p>
45   * Please note access to the internal structures of this class is not
46   * synchronized and therefore this class may be thread-unsafe.
47   *
48   * @since 4.0
49   *
50   * @deprecated (4.3)
51   */
52  @Deprecated
53  public final class BasicHttpProcessor implements
54      HttpProcessor, HttpRequestInterceptorList, HttpResponseInterceptorList, Cloneable {
55  
56      // Don't allow direct access, as nulls are not allowed
57      protected final List<HttpRequestInterceptor> requestInterceptors = new ArrayList<HttpRequestInterceptor>();
58      protected final List<HttpResponseInterceptor> responseInterceptors = new ArrayList<HttpResponseInterceptor>();
59  
60      @Override
61      public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
62          if (itcp == null) {
63              return;
64          }
65          this.requestInterceptors.add(itcp);
66      }
67  
68      @Override
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      @Override
78      public void addResponseInterceptor(
79              final HttpResponseInterceptor itcp, final int index) {
80          if (itcp == null) {
81              return;
82          }
83          this.responseInterceptors.add(index, itcp);
84      }
85  
86      @Override
87      public void removeRequestInterceptorByClass(final Class<? extends HttpRequestInterceptor> clazz) {
88          for (final Iterator<HttpRequestInterceptor> it = this.requestInterceptors.iterator();
89               it.hasNext(); ) {
90              final Object request = it.next();
91              if (request.getClass().equals(clazz)) {
92                  it.remove();
93              }
94          }
95      }
96  
97      @Override
98      public void removeResponseInterceptorByClass(final Class<? extends HttpResponseInterceptor> clazz) {
99          for (final Iterator<HttpResponseInterceptor> it = this.responseInterceptors.iterator();
100              it.hasNext(); ) {
101             final Object request = it.next();
102             if (request.getClass().equals(clazz)) {
103                 it.remove();
104             }
105         }
106     }
107 
108     public void addInterceptor(final HttpRequestInterceptor interceptor) {
109         addRequestInterceptor(interceptor);
110     }
111 
112      public void addInterceptor(final HttpRequestInterceptor interceptor, final int index) {
113         addRequestInterceptor(interceptor, index);
114     }
115 
116     @Override
117     public int getRequestInterceptorCount() {
118         return this.requestInterceptors.size();
119     }
120 
121     @Override
122     public HttpRequestInterceptor getRequestInterceptor(final int index) {
123         if ((index < 0) || (index >= this.requestInterceptors.size())) {
124             return null;
125         }
126         return this.requestInterceptors.get(index);
127     }
128 
129     @Override
130     public void clearRequestInterceptors() {
131         this.requestInterceptors.clear();
132     }
133 
134     @Override
135     public void addResponseInterceptor(final HttpResponseInterceptor itcp) {
136         if (itcp == null) {
137             return;
138         }
139         this.responseInterceptors.add(itcp);
140     }
141 
142     public void addInterceptor(final HttpResponseInterceptor interceptor) {
143         addResponseInterceptor(interceptor);
144     }
145 
146     public void addInterceptor(final HttpResponseInterceptor interceptor, final int index) {
147         addResponseInterceptor(interceptor, index);
148     }
149 
150     @Override
151     public int getResponseInterceptorCount() {
152         return this.responseInterceptors.size();
153     }
154 
155     @Override
156     public HttpResponseInterceptor getResponseInterceptor(final int index) {
157         if ((index < 0) || (index >= this.responseInterceptors.size())) {
158             return null;
159         }
160         return this.responseInterceptors.get(index);
161     }
162 
163     @Override
164     public void clearResponseInterceptors() {
165         this.responseInterceptors.clear();
166     }
167 
168     /**
169      * Sets the interceptor lists.
170      * First, both interceptor lists maintained by this processor
171      * will be cleared.
172      * Subsequently,
173      * elements of the argument list that are request interceptors will be
174      * added to the request interceptor list.
175      * Elements that are response interceptors will be
176      * added to the response interceptor list.
177      * Elements that are both request and response interceptor will be
178      * added to both lists.
179      * Elements that are neither request nor response interceptor
180      * will be ignored.
181      *
182      * @param list      the list of request and response interceptors
183      *                  from which to initialize
184      */
185     @Override
186     public void setInterceptors(final List<?> list) {
187         Args.notNull(list, "Inteceptor list");
188         this.requestInterceptors.clear();
189         this.responseInterceptors.clear();
190         for (final Object obj : list) {
191             if (obj instanceof HttpRequestInterceptor) {
192                 addInterceptor((HttpRequestInterceptor) obj);
193             }
194             if (obj instanceof HttpResponseInterceptor) {
195                 addInterceptor((HttpResponseInterceptor) obj);
196             }
197         }
198     }
199 
200     /**
201      * Clears both interceptor lists maintained by this processor.
202      */
203     public void clearInterceptors() {
204         clearRequestInterceptors();
205         clearResponseInterceptors();
206     }
207 
208     @Override
209     public void process(
210             final HttpRequest request,
211             final HttpContext context)
212             throws IOException, HttpException {
213         for (final HttpRequestInterceptor interceptor : this.requestInterceptors) {
214             interceptor.process(request, context);
215         }
216     }
217 
218     @Override
219     public void process(
220             final HttpResponse response,
221             final HttpContext context)
222             throws IOException, HttpException {
223         for (final HttpResponseInterceptor interceptor : this.responseInterceptors) {
224             interceptor.process(response, context);
225         }
226     }
227 
228     /**
229      * Sets up the target to have the same list of interceptors
230      * as the current instance.
231      *
232      * @param target object to be initialised
233      */
234     protected void copyInterceptors(final BasicHttpProcessor target) {
235         target.requestInterceptors.clear();
236         target.requestInterceptors.addAll(this.requestInterceptors);
237         target.responseInterceptors.clear();
238         target.responseInterceptors.addAll(this.responseInterceptors);
239     }
240 
241     /**
242      * Creates a copy of this instance
243      *
244      * @return new instance of the BasicHttpProcessor
245      */
246     public BasicHttpProcessor copy() {
247         final BasicHttpProcessorssor.html#BasicHttpProcessor">BasicHttpProcessor clone = new BasicHttpProcessor();
248         copyInterceptors(clone);
249         return clone;
250     }
251 
252     @Override
253     public Object clone() throws CloneNotSupportedException {
254         final BasicHttpProcessor/org/apache/http/protocol/BasicHttpProcessor.html#BasicHttpProcessor">BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
255         copyInterceptors(clone);
256         return clone;
257     }
258 
259 }