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.hc.core5.http.protocol;
28  
29  import java.io.IOException;
30  import java.util.List;
31  
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.http.EntityDetails;
35  import org.apache.hc.core5.http.HttpException;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.HttpRequestInterceptor;
38  import org.apache.hc.core5.http.HttpResponse;
39  import org.apache.hc.core5.http.HttpResponseInterceptor;
40  
41  /**
42   * Default immutable implementation of {@link HttpProcessor}.
43   *
44   * @since 5.0
45   */
46  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
47  public final class DefaultHttpProcessor implements HttpProcessor {
48  
49      private final HttpRequestInterceptor[] requestInterceptors;
50      private final HttpResponseInterceptor[] responseInterceptors;
51  
52      public DefaultHttpProcessor(
53              final HttpRequestInterceptor[] requestInterceptors,
54              final HttpResponseInterceptor[] responseInterceptors) {
55          super();
56          if (requestInterceptors != null) {
57              final int l = requestInterceptors.length;
58              this.requestInterceptors = new HttpRequestInterceptor[l];
59              System.arraycopy(requestInterceptors, 0, this.requestInterceptors, 0, l);
60          } else {
61              this.requestInterceptors = new HttpRequestInterceptor[0];
62          }
63          if (responseInterceptors != null) {
64              final int l = responseInterceptors.length;
65              this.responseInterceptors = new HttpResponseInterceptor[l];
66              System.arraycopy(responseInterceptors, 0, this.responseInterceptors, 0, l);
67          } else {
68              this.responseInterceptors = new HttpResponseInterceptor[0];
69          }
70      }
71  
72      /**
73       * @since 4.3
74       */
75      public DefaultHttpProcessor(
76              final List<HttpRequestInterceptor> requestInterceptors,
77              final List<HttpResponseInterceptor> responseInterceptors) {
78          super();
79          if (requestInterceptors != null) {
80              final int l = requestInterceptors.size();
81              this.requestInterceptors = requestInterceptors.toArray(new HttpRequestInterceptor[l]);
82          } else {
83              this.requestInterceptors = new HttpRequestInterceptor[0];
84          }
85          if (responseInterceptors != null) {
86              final int l = responseInterceptors.size();
87              this.responseInterceptors = responseInterceptors.toArray(new HttpResponseInterceptor[l]);
88          } else {
89              this.responseInterceptors = new HttpResponseInterceptor[0];
90          }
91      }
92  
93      public DefaultHttpProcessor(final HttpRequestInterceptor... requestInterceptors) {
94          this(requestInterceptors, null);
95      }
96  
97      public DefaultHttpProcessor(final HttpResponseInterceptor... responseInterceptors) {
98          this(null, responseInterceptors);
99      }
100 
101     @Override
102     public void process(
103             final HttpRequest request,
104             final EntityDetails entity,
105             final HttpContext context) throws IOException, HttpException {
106         for (final HttpRequestInterceptor requestInterceptor : this.requestInterceptors) {
107             requestInterceptor.process(request, entity, context);
108         }
109     }
110 
111     @Override
112     public void process(
113             final HttpResponse response,
114             final EntityDetails entity,
115             final HttpContext context) throws IOException, HttpException {
116         for (final HttpResponseInterceptor responseInterceptor : this.responseInterceptors) {
117             responseInterceptor.process(response, entity, context);
118         }
119     }
120 
121 }