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.hc.core5.http.protocol;
29  
30  import org.apache.hc.core5.http.HttpRequestInterceptor;
31  import org.apache.hc.core5.http.HttpResponseInterceptor;
32  
33  /**
34   * Builder for {@link HttpProcessor} instances.
35   *
36   * @since 4.3
37   */
38  public class HttpProcessorBuilder {
39  
40      private ChainBuilder<HttpRequestInterceptor> requestChainBuilder;
41      private ChainBuilder<HttpResponseInterceptor> responseChainBuilder;
42  
43      public static HttpProcessorBuilder create() {
44          return new HttpProcessorBuilder();
45      }
46  
47      HttpProcessorBuilder() {
48          super();
49      }
50  
51      private ChainBuilder<HttpRequestInterceptor> getRequestChainBuilder() {
52          if (requestChainBuilder == null) {
53              requestChainBuilder = new ChainBuilder<>();
54          }
55          return requestChainBuilder;
56      }
57  
58      private ChainBuilder<HttpResponseInterceptor> getResponseChainBuilder() {
59          if (responseChainBuilder == null) {
60              responseChainBuilder = new ChainBuilder<>();
61          }
62          return responseChainBuilder;
63      }
64  
65      public HttpProcessorBuilder addFirst(final HttpRequestInterceptor e) {
66          if (e == null) {
67              return this;
68          }
69          getRequestChainBuilder().addFirst(e);
70          return this;
71      }
72  
73      public HttpProcessorBuilder addLast(final HttpRequestInterceptor e) {
74          if (e == null) {
75              return this;
76          }
77          getRequestChainBuilder().addLast(e);
78          return this;
79      }
80  
81      public HttpProcessorBuilder add(final HttpRequestInterceptor e) {
82          return addLast(e);
83      }
84  
85      public HttpProcessorBuilder addAllFirst(final HttpRequestInterceptor... e) {
86          if (e == null) {
87              return this;
88          }
89          getRequestChainBuilder().addAllFirst(e);
90          return this;
91      }
92  
93      public HttpProcessorBuilder addAllLast(final HttpRequestInterceptor... e) {
94          if (e == null) {
95              return this;
96          }
97          getRequestChainBuilder().addAllLast(e);
98          return this;
99      }
100 
101     public HttpProcessorBuilder addAll(final HttpRequestInterceptor... e) {
102         return addAllLast(e);
103     }
104 
105     public HttpProcessorBuilder addFirst(final HttpResponseInterceptor e) {
106         if (e == null) {
107             return this;
108         }
109         getResponseChainBuilder().addFirst(e);
110         return this;
111     }
112 
113     public HttpProcessorBuilder addLast(final HttpResponseInterceptor e) {
114         if (e == null) {
115             return this;
116         }
117         getResponseChainBuilder().addLast(e);
118         return this;
119     }
120 
121     public HttpProcessorBuilder add(final HttpResponseInterceptor e) {
122         return addLast(e);
123     }
124 
125     public HttpProcessorBuilder addAllFirst(final HttpResponseInterceptor... e) {
126         if (e == null) {
127             return this;
128         }
129         getResponseChainBuilder().addAllFirst(e);
130         return this;
131     }
132 
133     public HttpProcessorBuilder addAllLast(final HttpResponseInterceptor... e) {
134         if (e == null) {
135             return this;
136         }
137         getResponseChainBuilder().addAllLast(e);
138         return this;
139     }
140 
141     public HttpProcessorBuilder addAll(final HttpResponseInterceptor... e) {
142         return addAllLast(e);
143     }
144 
145     public HttpProcessor build() {
146         return new DefaultHttpProcessor(
147                 requestChainBuilder != null ? requestChainBuilder.build() : null,
148                 responseChainBuilder != null ? responseChainBuilder.build() : null);
149     }
150 
151 }