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.ThreadingBehavior;
38 import org.apache.http.annotation.Contract;
39
40 /**
41  * Immutable {@link HttpProcessor}.
42  *
43  * @since 4.1
44  */

45 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
46 public final class ImmutableHttpProcessor implements HttpProcessor {
47
48     private final HttpRequestInterceptor[] requestInterceptors;
49     private final HttpResponseInterceptor[] responseInterceptors;
50
51     public ImmutableHttpProcessor(
52             final HttpRequestInterceptor[] requestInterceptors,
53             final HttpResponseInterceptor[] responseInterceptors) {
54         super();
55         if (requestInterceptors != null) {
56             final int l = requestInterceptors.length;
57             this.requestInterceptors = new HttpRequestInterceptor[l];
58             System.arraycopy(requestInterceptors, 0, this.requestInterceptors, 0, l);
59         } else {
60             this.requestInterceptors = new HttpRequestInterceptor[0];
61         }
62         if (responseInterceptors != null) {
63             final int l = responseInterceptors.length;
64             this.responseInterceptors = new HttpResponseInterceptor[l];
65             System.arraycopy(responseInterceptors, 0, this.responseInterceptors, 0, l);
66         } else {
67             this.responseInterceptors = new HttpResponseInterceptor[0];
68         }
69     }
70
71     /**
72      * @since 4.3
73      */

74     public ImmutableHttpProcessor(
75             final List<HttpRequestInterceptor> requestInterceptors,
76             final List<HttpResponseInterceptor> responseInterceptors) {
77         super();
78         if (requestInterceptors != null) {
79             final int l = requestInterceptors.size();
80             this.requestInterceptors = requestInterceptors.toArray(new HttpRequestInterceptor[l]);
81         } else {
82             this.requestInterceptors = new HttpRequestInterceptor[0];
83         }
84         if (responseInterceptors != null) {
85             final int l = responseInterceptors.size();
86             this.responseInterceptors = responseInterceptors.toArray(new HttpResponseInterceptor[l]);
87         } else {
88             this.responseInterceptors = new HttpResponseInterceptor[0];
89         }
90     }
91
92     /**
93      * @deprecated (4.3) do not use.
94      */

95     @Deprecated
96     public ImmutableHttpProcessor(
97             final HttpRequestInterceptorList requestInterceptors,
98             final HttpResponseInterceptorList responseInterceptors) {
99         super();
100         if (requestInterceptors != null) {
101             final int count = requestInterceptors.getRequestInterceptorCount();
102             this.requestInterceptors = new HttpRequestInterceptor[count];
103             for (int i = 0; i < count; i++) {
104                 this.requestInterceptors[i] = requestInterceptors.getRequestInterceptor(i);
105             }
106         } else {
107             this.requestInterceptors = new HttpRequestInterceptor[0];
108         }
109         if (responseInterceptors != null) {
110             final int count = responseInterceptors.getResponseInterceptorCount();
111             this.responseInterceptors = new HttpResponseInterceptor[count];
112             for (int i = 0; i < count; i++) {
113                 this.responseInterceptors[i] = responseInterceptors.getResponseInterceptor(i);
114             }
115         } else {
116             this.responseInterceptors = new HttpResponseInterceptor[0];
117         }
118     }
119
120     public ImmutableHttpProcessor(final HttpRequestInterceptor... requestInterceptors) {
121         this(requestInterceptors, null);
122     }
123
124     public ImmutableHttpProcessor(final HttpResponseInterceptor... responseInterceptors) {
125         this(null, responseInterceptors);
126     }
127
128     @Override
129     public void process(
130             final HttpRequest request,
131             final HttpContext context) throws IOException, HttpException {
132         for (final HttpRequestInterceptor requestInterceptor : this.requestInterceptors) {
133             requestInterceptor.process(request, context);
134         }
135     }
136
137     @Override
138     public void process(
139             final HttpResponse response,
140             final HttpContext context) throws IOException, HttpException {
141         for (final HttpResponseInterceptor responseInterceptor : this.responseInterceptors) {
142             responseInterceptor.process(response, context);
143         }
144     }
145
146 }
147