|
| 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