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 (int i = 0; i < list.size(); i++) {
180             final Object obj = list.get(i);
181             if (obj instanceof HttpRequestInterceptor) {
182                 addInterceptor((HttpRequestInterceptor)obj);
183             }
184             if (obj instanceof HttpResponseInterceptor) {
185                 addInterceptor((HttpResponseInterceptor)obj);
186             }
187         }
188     }
189 
190     /**
191      * Clears both interceptor lists maintained by this processor.
192      */
193     public void clearInterceptors() {
194         clearRequestInterceptors();
195         clearResponseInterceptors();
196     }
197 
198     public void process(
199             final HttpRequest request,
200             final HttpContext context)
201             throws IOException, HttpException {
202         for (int i = 0; i < this.requestInterceptors.size(); i++) {
203             final HttpRequestInterceptor interceptor =
204                 this.requestInterceptors.get(i);
205             interceptor.process(request, context);
206         }
207     }
208 
209     public void process(
210             final HttpResponse response,
211             final HttpContext context)
212             throws IOException, HttpException {
213         for (int i = 0; i < this.responseInterceptors.size(); i++) {
214             final HttpResponseInterceptor interceptor =
215                 this.responseInterceptors.get(i);
216             interceptor.process(response, context);
217         }
218     }
219 
220     /**
221      * Sets up the target to have the same list of interceptors
222      * as the current instance.
223      *
224      * @param target object to be initialised
225      */
226     protected void copyInterceptors(final BasicHttpProcessor target) {
227         target.requestInterceptors.clear();
228         target.requestInterceptors.addAll(this.requestInterceptors);
229         target.responseInterceptors.clear();
230         target.responseInterceptors.addAll(this.responseInterceptors);
231     }
232 
233     /**
234      * Creates a copy of this instance
235      *
236      * @return new instance of the BasicHttpProcessor
237      */
238     public BasicHttpProcessor copy() {
239         final BasicHttpProcessor clone = new BasicHttpProcessor();
240         copyInterceptors(clone);
241         return clone;
242     }
243 
244     @Override
245     public Object clone() throws CloneNotSupportedException {
246         final BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
247         copyInterceptors(clone);
248         return clone;
249     }
250 
251 }