Skip to content

Commit a86eb88

Browse files
committed
("AsyncHttpClientConfig - Being able to create using pure java bean style")
1 parent ed1be29 commit a86eb88

File tree

2 files changed

+271
-30
lines changed

2 files changed

+271
-30
lines changed

src/main/java/com/ning/http/client/AsyncHttpClientConfig.java

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,39 @@
5151
*/
5252
public class AsyncHttpClientConfig {
5353

54-
private final static String ASYNC_CLIENT = AsyncHttpClientConfig.class.getName() + ".";
55-
56-
private final int maxTotalConnections;
57-
private final int maxConnectionPerHost;
58-
private final int connectionTimeOutInMs;
59-
private final int idleConnectionInPoolTimeoutInMs;
60-
private final int requestTimeoutInMs;
61-
private final boolean redirectEnabled;
62-
private final int maxDefaultRedirects;
63-
private final boolean compressionEnabled;
64-
private final String userAgent;
65-
private final boolean allowPoolingConnection;
66-
private final ScheduledExecutorService reaper;
67-
private final ExecutorService applicationThreadPool;
68-
private final ProxyServer proxyServer;
69-
private final SSLContext sslContext;
70-
private final SSLEngineFactory sslEngineFactory;
71-
private final AsyncHttpProviderConfig<?, ?> providerConfig;
72-
private final ConnectionsPool<?, ?> connectionsPool;
73-
private final Realm realm;
74-
private final List<RequestFilter> requestFilters;
75-
private final List<ResponseFilter> responseFilters;
76-
private final List<IOExceptionFilter> ioExceptionFilters;
77-
private final int requestCompressionLevel;
78-
private final int maxRequestRetry;
79-
private final boolean allowSslConnectionPool;
80-
private final boolean useRawUrl;
81-
private final boolean removeQueryParamOnRedirect;
82-
private final HostnameVerifier hostnameVerifier;
83-
private final int ioThreadMultiplier;
54+
protected final static String ASYNC_CLIENT = AsyncHttpClientConfig.class.getName() + ".";
55+
56+
protected int maxTotalConnections;
57+
protected int maxConnectionPerHost;
58+
protected int connectionTimeOutInMs;
59+
protected int idleConnectionInPoolTimeoutInMs;
60+
protected int requestTimeoutInMs;
61+
protected boolean redirectEnabled;
62+
protected int maxDefaultRedirects;
63+
protected boolean compressionEnabled;
64+
protected String userAgent;
65+
protected boolean allowPoolingConnection;
66+
protected ScheduledExecutorService reaper;
67+
protected ExecutorService applicationThreadPool;
68+
protected ProxyServer proxyServer;
69+
protected SSLContext sslContext;
70+
protected SSLEngineFactory sslEngineFactory;
71+
protected AsyncHttpProviderConfig<?, ?> providerConfig;
72+
protected ConnectionsPool<?, ?> connectionsPool;
73+
protected Realm realm;
74+
protected List<RequestFilter> requestFilters;
75+
protected List<ResponseFilter> responseFilters;
76+
protected List<IOExceptionFilter> ioExceptionFilters;
77+
protected int requestCompressionLevel;
78+
protected int maxRequestRetry;
79+
protected boolean allowSslConnectionPool;
80+
protected boolean useRawUrl;
81+
protected boolean removeQueryParamOnRedirect;
82+
protected HostnameVerifier hostnameVerifier;
83+
protected int ioThreadMultiplier;
84+
85+
protected AsyncHttpClientConfig(){
86+
}
8487

8588
private AsyncHttpClientConfig(int maxTotalConnections,
8689
int maxConnectionPerHost,
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/*
2+
* Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.client;
14+
15+
import com.ning.http.client.filter.IOExceptionFilter;
16+
import com.ning.http.client.filter.RequestFilter;
17+
import com.ning.http.client.filter.ResponseFilter;
18+
import com.ning.http.util.ProxyUtils;
19+
20+
import javax.net.ssl.HostnameVerifier;
21+
import javax.net.ssl.SSLContext;
22+
import javax.net.ssl.SSLSession;
23+
import java.util.LinkedList;
24+
import java.util.concurrent.ExecutorService;
25+
import java.util.concurrent.Executors;
26+
import java.util.concurrent.ScheduledExecutorService;
27+
import java.util.concurrent.ThreadFactory;
28+
29+
/**
30+
* Simple JavaBean version of {@link AsyncHttpClientConfig}
31+
*/
32+
public class AsyncHttpClientConfigBean extends AsyncHttpClientConfig {
33+
34+
public AsyncHttpClientConfigBean() {
35+
configureExecutors();
36+
configureDefaults();
37+
configureFilters();
38+
}
39+
40+
void configureFilters() {
41+
requestFilters = new LinkedList<RequestFilter>();
42+
responseFilters = new LinkedList<ResponseFilter>();
43+
ioExceptionFilters = new LinkedList<IOExceptionFilter>();
44+
}
45+
46+
void configureDefaults() {
47+
maxTotalConnections = Integer.getInteger(ASYNC_CLIENT + "defaultMaxTotalConnections", -1);
48+
maxConnectionPerHost = Integer.getInteger(ASYNC_CLIENT + "defaultMaxConnectionsPerHost", -1);
49+
connectionTimeOutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultConnectionTimeoutInMS", 60 * 1000);
50+
idleConnectionInPoolTimeoutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultIdleConnectionInPoolTimeoutInMS", 60 * 1000);
51+
requestTimeoutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultRequestTimeoutInMS", 60 * 1000);
52+
redirectEnabled = Boolean.getBoolean(ASYNC_CLIENT + "defaultRedirectsEnabled");
53+
maxDefaultRedirects = Integer.getInteger(ASYNC_CLIENT + "defaultMaxRedirects", 5);
54+
compressionEnabled = Boolean.getBoolean(ASYNC_CLIENT + "compressionEnabled");
55+
userAgent = System.getProperty(ASYNC_CLIENT + "userAgent", "NING/1.0");
56+
57+
boolean useProxyProperties = Boolean.getBoolean(ASYNC_CLIENT + "useProxyProperties");
58+
if (useProxyProperties) {
59+
proxyServer = ProxyUtils.createProxy(System.getProperties());
60+
}
61+
62+
allowPoolingConnection = true;
63+
requestCompressionLevel = -1;
64+
maxRequestRetry = 5;
65+
allowSslConnectionPool = true;
66+
useRawUrl = false;
67+
removeQueryParamOnRedirect = true;
68+
hostnameVerifier = new HostnameVerifier() {
69+
70+
public boolean verify(String s, SSLSession sslSession) {
71+
return true;
72+
}
73+
};
74+
}
75+
76+
void configureExecutors() {
77+
reaper = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
78+
public Thread newThread(Runnable r) {
79+
Thread t = new Thread(r, "AsyncHttpClient-Reaper");
80+
t.setDaemon(true);
81+
return t;
82+
}
83+
});
84+
applicationThreadPool = Executors.newCachedThreadPool(new ThreadFactory() {
85+
public Thread newThread(Runnable r) {
86+
Thread t = new Thread(r, "AsyncHttpClient-Callback");
87+
t.setDaemon(true);
88+
return t;
89+
}
90+
});
91+
}
92+
93+
public AsyncHttpClientConfigBean setMaxTotalConnections(int maxTotalConnections) {
94+
this.maxTotalConnections = maxTotalConnections;
95+
return this;
96+
}
97+
98+
public AsyncHttpClientConfigBean setMaxConnectionPerHost(int maxConnectionPerHost) {
99+
this.maxConnectionPerHost = maxConnectionPerHost;
100+
return this;
101+
}
102+
103+
public AsyncHttpClientConfigBean setConnectionTimeOutInMs(int connectionTimeOutInMs) {
104+
this.connectionTimeOutInMs = connectionTimeOutInMs;
105+
return this;
106+
}
107+
108+
public AsyncHttpClientConfigBean setIdleConnectionInPoolTimeoutInMs(int idleConnectionInPoolTimeoutInMs) {
109+
this.idleConnectionInPoolTimeoutInMs = idleConnectionInPoolTimeoutInMs;
110+
return this;
111+
}
112+
113+
public AsyncHttpClientConfigBean setRequestTimeoutInMs(int requestTimeoutInMs) {
114+
this.requestTimeoutInMs = requestTimeoutInMs;
115+
return this;
116+
}
117+
118+
public AsyncHttpClientConfigBean setRedirectEnabled(boolean redirectEnabled) {
119+
this.redirectEnabled = redirectEnabled;
120+
return this;
121+
}
122+
123+
public AsyncHttpClientConfigBean setMaxDefaultRedirects(int maxDefaultRedirects) {
124+
this.maxDefaultRedirects = maxDefaultRedirects;
125+
return this;
126+
}
127+
128+
public AsyncHttpClientConfigBean setCompressionEnabled(boolean compressionEnabled) {
129+
this.compressionEnabled = compressionEnabled;
130+
return this;
131+
}
132+
133+
public AsyncHttpClientConfigBean setUserAgent(String userAgent) {
134+
this.userAgent = userAgent;
135+
return this;
136+
}
137+
138+
public AsyncHttpClientConfigBean setAllowPoolingConnection(boolean allowPoolingConnection) {
139+
this.allowPoolingConnection = allowPoolingConnection;
140+
return this;
141+
}
142+
143+
public AsyncHttpClientConfigBean setReaper(ScheduledExecutorService reaper) {
144+
if (this.reaper != null) {
145+
this.reaper.shutdownNow();
146+
}
147+
this.reaper = reaper;
148+
return this;
149+
}
150+
151+
public AsyncHttpClientConfigBean setApplicationThreadPool(ExecutorService applicationThreadPool) {
152+
if (this.applicationThreadPool != null) {
153+
this.applicationThreadPool.shutdownNow();
154+
}
155+
this.applicationThreadPool = applicationThreadPool;
156+
return this;
157+
}
158+
159+
public AsyncHttpClientConfigBean setProxyServer(ProxyServer proxyServer) {
160+
this.proxyServer = proxyServer;
161+
return this;
162+
}
163+
164+
public AsyncHttpClientConfigBean setSslContext(SSLContext sslContext) {
165+
this.sslContext = sslContext;
166+
return this;
167+
}
168+
169+
public AsyncHttpClientConfigBean setSslEngineFactory(SSLEngineFactory sslEngineFactory) {
170+
this.sslEngineFactory = sslEngineFactory;
171+
return this;
172+
}
173+
174+
public AsyncHttpClientConfigBean setProviderConfig(AsyncHttpProviderConfig<?, ?> providerConfig) {
175+
this.providerConfig = providerConfig;
176+
return this;
177+
}
178+
179+
public AsyncHttpClientConfigBean setConnectionsPool(ConnectionsPool<?, ?> connectionsPool) {
180+
this.connectionsPool = connectionsPool;
181+
return this;
182+
}
183+
184+
public AsyncHttpClientConfigBean setRealm(Realm realm) {
185+
this.realm = realm;
186+
return this;
187+
}
188+
189+
public AsyncHttpClientConfigBean addRequestFilter(RequestFilter requestFilter) {
190+
requestFilters.add(requestFilter);
191+
return this;
192+
}
193+
194+
public AsyncHttpClientConfigBean addResponseFilters(ResponseFilter responseFilter) {
195+
responseFilters.add(responseFilter);
196+
return this;
197+
}
198+
199+
public AsyncHttpClientConfigBean addIoExceptionFilters(IOExceptionFilter ioExceptionFilter) {
200+
ioExceptionFilters.add(ioExceptionFilter);
201+
return this;
202+
}
203+
204+
public AsyncHttpClientConfigBean setRequestCompressionLevel(int requestCompressionLevel) {
205+
this.requestCompressionLevel = requestCompressionLevel;
206+
return this;
207+
}
208+
209+
public AsyncHttpClientConfigBean setMaxRequestRetry(int maxRequestRetry) {
210+
this.maxRequestRetry = maxRequestRetry;
211+
return this;
212+
}
213+
214+
public AsyncHttpClientConfigBean setAllowSslConnectionPool(boolean allowSslConnectionPool) {
215+
this.allowSslConnectionPool = allowSslConnectionPool;
216+
return this;
217+
}
218+
219+
public AsyncHttpClientConfigBean setUseRawUrl(boolean useRawUrl) {
220+
this.useRawUrl = useRawUrl;
221+
return this;
222+
}
223+
224+
public AsyncHttpClientConfigBean setRemoveQueryParamOnRedirect(boolean removeQueryParamOnRedirect) {
225+
this.removeQueryParamOnRedirect = removeQueryParamOnRedirect;
226+
return this;
227+
}
228+
229+
public AsyncHttpClientConfigBean setHostnameVerifier(HostnameVerifier hostnameVerifier) {
230+
this.hostnameVerifier = hostnameVerifier;
231+
return this;
232+
}
233+
234+
public AsyncHttpClientConfigBean setIoThreadMultiplier(int ioThreadMultiplier) {
235+
this.ioThreadMultiplier = ioThreadMultiplier;
236+
return this;
237+
}
238+
}

0 commit comments

Comments
 (0)