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  import java.util.List;
31  
32  import org.apache.http.HttpException;
33  import org.apache.http.HttpRequest;
34  import org.apache.http.HttpRequestInterceptor;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.HttpResponseInterceptor;
37  import org.apache.http.annotation.ThreadSafe;
38  
39  /**
40   * Immutable {@link HttpProcessor}.
41   *
42   * @since 4.1
43   */
44  @ThreadSafe // provided injected dependencies are immutable
45  public final class ImmutableHttpProcessor implements HttpProcessor {
46  
47      private final HttpRequestInterceptor[] requestInterceptors;
48      private final HttpResponseInterceptor[] responseInterceptors;
49  
50      public ImmutableHttpProcessor(
51              final HttpRequestInterceptor[] requestInterceptors,
52              final HttpResponseInterceptor[] responseInterceptors) {
53          super();
54          if (requestInterceptors != null) {
55              final int l = requestInterceptors.length;
56              this.requestInterceptors = new HttpRequestInterceptor[l];
57              System.arraycopy(requestInterceptors, 0, this.requestInterceptors, 0, l);
58          } else {
59              this.requestInterceptors = new HttpRequestInterceptor[0];
60          }
61          if (responseInterceptors != null) {
62              final int l = responseInterceptors.length;
63              this.responseInterceptors = new HttpResponseInterceptor[l];
64              System.arraycopy(responseInterceptors, 0, this.responseInterceptors, 0, l);
65          } else {
66              this.responseInterceptors = new HttpResponseInterceptor[0];
67          }
68      }
69  
70      /**
71       * @since 4.3
72       */
73      public ImmutableHttpProcessor(
74              final List<HttpRequestInterceptor> requestInterceptors,
75              final List<HttpResponseInterceptor> responseInterceptors) {
76          super();
77          if (requestInterceptors != null) {
78              final int l = requestInterceptors.size();
79              this.requestInterceptors = requestInterceptors.toArray(new HttpRequestInterceptor[l]);
80          } else {
81              this.requestInterceptors = new HttpRequestInterceptor[0];
82          }
83          if (responseInterceptors != null) {
84              final int l = responseInterceptors.size();
85              this.responseInterceptors = responseInterceptors.toArray(new HttpResponseInterceptor[l]);
86          } else {
87              this.responseInterceptors = new HttpResponseInterceptor[0];
88          }
89      }
90  
91      /**
92       * @deprecated (4.3) do not use.
93       */
94      @Deprecated
95      public ImmutableHttpProcessor(
96              final HttpRequestInterceptorList requestInterceptors,
97              final HttpResponseInterceptorList responseInterceptors) {
98          super();
99          if (requestInterceptors != null) {
100             final int count = requestInterceptors.getRequestInterceptorCount();
101             this.requestInterceptors = new HttpRequestInterceptor[count];
102             for (int i = 0; i < count; i++) {
103                 this.requestInterceptors[i] = requestInterceptors.getRequestInterceptor(i);
104             }
105         } else {
106             this.requestInterceptors = new HttpRequestInterceptor[0];
107         }
108         if (responseInterceptors != null) {
109             final int count = responseInterceptors.getResponseInterceptorCount();
110             this.responseInterceptors = new HttpResponseInterceptor[count];
111             for (int i = 0; i < count; i++) {
112                 this.responseInterceptors[i] = responseInterceptors.getResponseInterceptor(i);
113             }
114         } else {
115             this.responseInterceptors = new HttpResponseInterceptor[0];
116         }
117     }
118 
119     public ImmutableHttpProcessor(final HttpRequestInterceptor... requestInterceptors) {
120         this(requestInterceptors, null);
121     }
122 
123     public ImmutableHttpProcessor(final HttpResponseInterceptor... responseInterceptors) {
124         this(null, responseInterceptors);
125     }
126 
127     @Override
128     public void process(
129             final HttpRequest request,
130             final HttpContext context) throws IOException, HttpException {
131         for (final HttpRequestInterceptor requestInterceptor : this.requestInterceptors) {
132             requestInterceptor.process(request, context);
133         }
134     }
135 
136     @Override
137     public void process(
138             final HttpResponse response,
139             final HttpContext context) throws IOException, HttpException {
140         for (final HttpResponseInterceptor responseInterceptor : this.responseInterceptors) {
141             responseInterceptor.process(response, context);
142         }
143     }
144 
145 }