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  package org.apache.http.protocol;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.HttpException;
32  import org.apache.http.HttpRequest;
33  import org.apache.http.HttpRequestInterceptor;
34  import org.apache.http.HttpResponse;
35  import org.apache.http.HttpResponseInterceptor;
36  import org.apache.http.annotation.ThreadSafe;
37  
38  /**
39   * Immutable {@link HttpProcessor}.
40   *
41   * @since 4.1
42   */
43  @ThreadSafe // provided injected dependencies are immutable
44  public final class ImmutableHttpProcessor implements HttpProcessor {
45  
46      private final HttpRequestInterceptor[] requestInterceptors;
47      private final HttpResponseInterceptor[] responseInterceptors;
48  
49      public ImmutableHttpProcessor(
50              final HttpRequestInterceptor[] requestInterceptors,
51              final HttpResponseInterceptor[] responseInterceptors) {
52          super();
53          if (requestInterceptors != null) {
54              int count = requestInterceptors.length;
55              this.requestInterceptors = new HttpRequestInterceptor[count];
56              for (int i = 0; i < count; i++) {
57                  this.requestInterceptors[i] = requestInterceptors[i];
58              }
59          } else {
60              this.requestInterceptors = new HttpRequestInterceptor[0];
61          }
62          if (responseInterceptors != null) {
63              int count = responseInterceptors.length;
64              this.responseInterceptors = new HttpResponseInterceptor[count];
65              for (int i = 0; i < count; i++) {
66                  this.responseInterceptors[i] = responseInterceptors[i];
67              }
68          } else {
69              this.responseInterceptors = new HttpResponseInterceptor[0];
70          }
71      }
72  
73      public ImmutableHttpProcessor(
74              final HttpRequestInterceptorList requestInterceptors,
75              final HttpResponseInterceptorList responseInterceptors) {
76          super();
77          if (requestInterceptors != null) {
78              int count = requestInterceptors.getRequestInterceptorCount();
79              this.requestInterceptors = new HttpRequestInterceptor[count];
80              for (int i = 0; i < count; i++) {
81                  this.requestInterceptors[i] = requestInterceptors.getRequestInterceptor(i);
82              }
83          } else {
84              this.requestInterceptors = new HttpRequestInterceptor[0];
85          }
86          if (responseInterceptors != null) {
87              int count = responseInterceptors.getResponseInterceptorCount();
88              this.responseInterceptors = new HttpResponseInterceptor[count];
89              for (int i = 0; i < count; i++) {
90                  this.responseInterceptors[i] = responseInterceptors.getResponseInterceptor(i);
91              }
92          } else {
93              this.responseInterceptors = new HttpResponseInterceptor[0];
94          }
95      }
96  
97      public ImmutableHttpProcessor(final HttpRequestInterceptor[] requestInterceptors) {
98          this(requestInterceptors, null);
99      }
100 
101     public ImmutableHttpProcessor(final HttpResponseInterceptor[] responseInterceptors) {
102         this(null, responseInterceptors);
103     }
104 
105     public void process(
106             final HttpRequest request,
107             final HttpContext context) throws IOException, HttpException {
108         for (int i = 0; i < this.requestInterceptors.length; i++) {
109             this.requestInterceptors[i].process(request, context);
110         }
111     }
112 
113     public void process(
114             final HttpResponse response,
115             final HttpContext context) throws IOException, HttpException {
116         for (int i = 0; i < this.responseInterceptors.length; i++) {
117             this.responseInterceptors[i].process(response, context);
118         }
119     }
120 
121 }