Debian Codegraph @

httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MinimalHttpAsyncClientBuilder.java

raw
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.http.impl.nio.client;
29
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.ThreadFactory;
32
33 import org.apache.http.ConnectionReuseStrategy;
34 import org.apache.http.client.protocol.RequestAddCookies;
35 import org.apache.http.client.protocol.RequestClientConnControl;
36 import org.apache.http.client.protocol.ResponseProcessCookies;
37 import org.apache.http.conn.ConnectionKeepAliveStrategy;
38 import org.apache.http.impl.DefaultConnectionReuseStrategy;
39 import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
40 import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
41 import org.apache.http.impl.nio.reactor.IOReactorConfig;
42 import org.apache.http.nio.NHttpClientEventHandler;
43 import org.apache.http.nio.conn.NHttpClientConnectionManager;
44 import org.apache.http.nio.protocol.HttpAsyncRequestExecutor;
45 import org.apache.http.protocol.HttpProcessor;
46 import org.apache.http.protocol.HttpProcessorBuilder;
47 import org.apache.http.protocol.RequestContent;
48 import org.apache.http.protocol.RequestTargetHost;
49 import org.apache.http.protocol.RequestUserAgent;
50 import org.apache.http.util.VersionInfo;
51
52 /**
53 * Builder for {@link org.apache.http.impl.nio.client.MinimalHttpAsyncClient} instances.
54 *
55 * @since 4.1
56 */
57 class MinimalHttpAsyncClientBuilder {
58
59 private NHttpClientConnectionManager connManager;
60 private boolean connManagerShared;
61 private ConnectionReuseStrategy reuseStrategy;
62 private ConnectionKeepAliveStrategy keepAliveStrategy;
63 private String userAgent;
64 private ThreadFactory threadFactory;
65 private boolean cookieManagementDisabled;
66
67 public static MinimalHttpAsyncClientBuilder create() {
68 return new MinimalHttpAsyncClientBuilder();
69 }
70
71 protected MinimalHttpAsyncClientBuilder() {
72 super();
73 }
74
75 public final MinimalHttpAsyncClientBuilder setConnectionManager(
76 final NHttpClientConnectionManager connManager) {
77 this.connManager = connManager;
78 return this;
79 }
80
81 public final MinimalHttpAsyncClientBuilder setConnectionManagerShared(
82 final boolean shared) {
83 this.connManagerShared = shared;
84 return this;
85 }
86
87 public final MinimalHttpAsyncClientBuilder setConnectionReuseStrategy(
88 final ConnectionReuseStrategy reuseStrategy) {
89 this.reuseStrategy = reuseStrategy;
90 return this;
91 }
92
93 public final MinimalHttpAsyncClientBuilder setKeepAliveStrategy(
94 final ConnectionKeepAliveStrategy keepAliveStrategy) {
95 this.keepAliveStrategy = keepAliveStrategy;
96 return this;
97 }
98
99 public final MinimalHttpAsyncClientBuilder setUserAgent(final String userAgent) {
100 this.userAgent = userAgent;
101 return this;
102 }
103
104 public final MinimalHttpAsyncClientBuilder setThreadFactory(final ThreadFactory threadFactory) {
105 this.threadFactory = threadFactory;
106 return this;
107 }
108
109 public final MinimalHttpAsyncClientBuilder disableCookieManagement() {
110 cookieManagementDisabled = true;
111 return this;
112 }
113
114 public MinimalHttpAsyncClient build() {
115
116 NHttpClientConnectionManager connManager = this.connManager;
117 if (connManager == null) {
118 connManager = new PoolingNHttpClientConnectionManager(IOReactorUtils.create(IOReactorConfig.DEFAULT,
119 threadFactory));
120 }
121 ConnectionReuseStrategy reuseStrategy = this.reuseStrategy;
122 if (reuseStrategy == null) {
123 reuseStrategy = DefaultConnectionReuseStrategy.INSTANCE;
124 }
125 ConnectionKeepAliveStrategy keepAliveStrategy = this.keepAliveStrategy;
126 if (keepAliveStrategy == null) {
127 keepAliveStrategy = DefaultConnectionKeepAliveStrategy.INSTANCE;
128 }
129 String userAgent = this.userAgent;
130 if (userAgent == null) {
131 userAgent = VersionInfo.getUserAgent(
132 "Apache-HttpAsyncClient", "org.apache.http.nio.client", getClass());
133 }
134 final HttpProcessorBuilder b = HttpProcessorBuilder.create();
135 b.addAll(
136 new RequestContent(),
137 new RequestTargetHost(),
138 new RequestClientConnControl(),
139 new RequestUserAgent(userAgent));
140 if (!cookieManagementDisabled) {
141 b.add(new RequestAddCookies());
142 b.add(new ResponseProcessCookies());
143 }
144 final HttpProcessor httpprocessor = b.build();
145
146 ThreadFactory threadFactory = null;
147 NHttpClientEventHandler eventHandler = null;
148 if (!this.connManagerShared) {
149 threadFactory = this.threadFactory;
150 if (threadFactory == null) {
151 threadFactory = Executors.defaultThreadFactory();
152 }
153 eventHandler = new HttpAsyncRequestExecutor();
154 }
155 return new MinimalHttpAsyncClient(
156 connManager,
157 threadFactory,
158 eventHandler,
159 httpprocessor,
160 reuseStrategy,
161 keepAliveStrategy);
162 }
163
164 }