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      public void addRequestInterceptor(final HttpRequestInterceptor itcp) {
61          if (itcp == null) {
62              return;
63          }
64          this.requestInterceptors.add(itcp);
65      }
66  
67      public void addRequestInterceptor(
68              final HttpRequestInterceptor itcp, final int index) {
69          if (itcp == null) {
70              return;
71          }
72          this.requestInterceptors.add(index, itcp);
73      }
74  
75      public void addResponseInterceptor(
76              final HttpResponseInterceptor itcp, final int index) {
77          if (itcp == null) {
78              return;
79          }
80          this.responseInterceptors.add(index, itcp);
81      }
82  
83      public void removeRequestInterceptorByClass(final Class<? extends HttpRequestInterceptor> clazz) {
84          for (final Iterator<HttpRequestInterceptor> it = this.requestInterceptors.iterator();
85               it.hasNext(); ) {
86              final Object request = it.next();
87              if (request.getClass().equals(clazz)) {
88                  it.remove();
89              }
90          }
91      }
92  
93      public void removeResponseInterceptorByClass(final Class<? extends HttpResponseInterceptor> clazz) {
94          for (final Iterator<HttpResponseInterceptor> it = this.responseInterceptors.iterator();
95               it.hasNext(); ) {
96              final Object request = it.next();
97              if (request.getClass().equals(clazz)) {
98                  it.remove();
99              }
100         }
101     }
102 
103     public final void addInterceptor(final HttpRequestInterceptor interceptor) {
104         addRequestInterceptor(interceptor);
105     }
106 
107      public final void addInterceptor(final HttpRequestInterceptor interceptor, final int index) {
108         addRequestInterceptor(interceptor, index);
109     }
110 
111     public int getRequestInterceptorCount() {
112         return this.requestInterceptors.size();
113     }
114 
115     public HttpRequestInterceptor getRequestInterceptor(final int index) {
116         if ((index < 0) || (index >= this.requestInterceptors.size())) {
117             return null;
118         }
119         return this.requestInterceptors.get(index);
120     }
121 
122     public void clearRequestInterceptors() {
123         this.requestInterceptors.clear();
124     }
125 
126     public void addResponseInterceptor(final HttpResponseInterceptor itcp) {
127         if (itcp == null) {
128             return;
129         }
130         this.responseInterceptors.add(itcp);
131     }
132 
133     public final void addInterceptor(final HttpResponseInterceptor interceptor) {
134         addResponseInterceptor(interceptor);
135     }
136 
137     public final void addInterceptor(final HttpResponseInterceptor interceptor, final int index) {
138         addResponseInterceptor(interceptor, index);
139     }
140 
141     public int getResponseInterceptorCount() {
142         return this.responseInterceptors.size();
143     }
144 
145     public HttpResponseInterceptor getResponseInterceptor(final int index) {
146         if ((index < 0) || (index >= this.responseInterceptors.size())) {
147             return null;
148         }
149         return this.responseInterceptors.get(index);
150     }
151 
152     public void clearResponseInterceptors() {
153         this.responseInterceptors.clear();
154     }
155 
156     /**
157      * Sets the interceptor lists.
158      * First, both interceptor lists maintained by this processor
159      * will be cleared.
160      * Subsequently,
161      * elements of the argument list that are request interceptors will be
162      * added to the request interceptor list.
163      * Elements that are response interceptors will be
164      * added to the response interceptor list.
165      * Elements that are both request and response interceptor will be
166      * added to both lists.
167      * Elements that are neither request nor response interceptor
168      * will be ignored.
169      *
170      * @param list      the list of request and response interceptors
171      *                  from which to initialize
172      */
173     public void setInterceptors(final List<?> list) {
174         Args.notNull(list, "Inteceptor list");
175         this.requestInterceptors.clear();
176         this.responseInterceptors.clear();
177         for (final Object obj : list) {
178             if (obj instanceof HttpRequestInterceptor) {
179                 addInterceptor((HttpRequestInterceptor) obj);
180             }
181             if (obj instanceof HttpResponseInterceptor) {
182                 addInterceptor((HttpResponseInterceptor) obj);
183             }
184         }
185     }
186 
187     /**
188      * Clears both interceptor lists maintained by this processor.
189      */
190     public void clearInterceptors() {
191         clearRequestInterceptors();
192         clearResponseInterceptors();
193     }
194 
195     public void process(
196             final HttpRequest request,
197             final HttpContext context)
198             throws IOException, HttpException {
199         for (final HttpRequestInterceptor interceptor : this.requestInterceptors) {
200             interceptor.process(request, context);
201         }
202     }
203 
204     public void process(
205             final HttpResponse response,
206             final HttpContext context)
207             throws IOException, HttpException {
208         for (final HttpResponseInterceptor interceptor : this.responseInterceptors) {
209             interceptor.process(response, context);
210         }
211     }
212 
213     /**
214      * Sets up the target to have the same list of interceptors
215      * as the current instance.
216      *
217      * @param target object to be initialised
218      */
219     protected void copyInterceptors(final BasicHttpProcessor target) {
220         target.requestInterceptors.clear();
221         target.requestInterceptors.addAll(this.requestInterceptors);
222         target.responseInterceptors.clear();
223         target.responseInterceptors.addAll(this.responseInterceptors);
224     }
225 
226     /**
227      * Creates a copy of this instance
228      *
229      * @return new instance of the BasicHttpProcessor
230      */
231     public BasicHttpProcessor copy() {
232         final BasicHttpProcessor clone = new BasicHttpProcessor();
233         copyInterceptors(clone);
234         return clone;
235     }
236 
237     @Override
238     public Object clone() throws CloneNotSupportedException {
239         final BasicHttpProcessor clone = (BasicHttpProcessor) super.clone();
240         copyInterceptors(clone);
241         return clone;
242     }
243 
244 }