This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
/
Hc4ProviderImpl.java
466 lines (415 loc) · 18.7 KB
/
Hc4ProviderImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2012 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.apachehttpclient;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.auth.params.AuthPNames;
import org.apache.http.client.params.AuthPolicy;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.protocol.ResponseContentEncoding;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.NoConnectionReuseStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.sonatype.nexus.configuration.application.ApplicationConfiguration;
import org.sonatype.nexus.configuration.application.GlobalRemoteConnectionSettings;
import org.sonatype.nexus.logging.AbstractLoggingComponent;
import org.sonatype.nexus.proxy.events.NexusStoppedEvent;
import org.sonatype.nexus.proxy.repository.ClientSSLRemoteAuthenticationSettings;
import org.sonatype.nexus.proxy.repository.NtlmRemoteAuthenticationSettings;
import org.sonatype.nexus.proxy.repository.RemoteAuthenticationSettings;
import org.sonatype.nexus.proxy.repository.RemoteProxySettings;
import org.sonatype.nexus.proxy.repository.UsernamePasswordRemoteAuthenticationSettings;
import org.sonatype.nexus.proxy.storage.remote.RemoteStorageContext;
import org.sonatype.nexus.proxy.utils.UserAgentBuilder;
import org.sonatype.nexus.util.SystemPropertiesHelper;
import org.sonatype.plexus.appevents.ApplicationEventMulticaster;
import org.sonatype.plexus.appevents.Event;
import org.sonatype.plexus.appevents.EventListener;
import com.google.common.base.Preconditions;
/**
* Default implementation of {@link Hc4Provider}.
*
* @author cstamas
* @since 2.2
*/
@Singleton
@Named
public class Hc4ProviderImpl
extends AbstractLoggingComponent
implements Hc4Provider, EventListener
{
/**
* Key for customizing connection pool maximum size. Value should be integer equal to 0 or greater. Pool size of 0
* will actually prevent use of pool. Any positive number means the actual size of the pool to be created. This is a
* hard limit, connection pool will never contain more than this count of open sockets.
*/
private static final String CONNECTION_POOL_MAX_SIZE_KEY = "nexus.apacheHttpClient4x.connectionPoolMaxSize";
/**
* Default pool max size: 200.
*/
private static final int CONNECTION_POOL_MAX_SIZE_DEFAULT = 200;
/**
* Key for customizing connection pool size per route (usually per-repository, but not quite in case of Mirrors).
* Value should be integer equal to 0 or greater. Pool size of 0 will actually prevent use of pool. Any positive
* number means the actual size of the pool to be created.
*/
private static final String CONNECTION_POOL_SIZE_KEY = "nexus.apacheHttpClient4x.connectionPoolSize";
/**
* Default pool size: 20.
*/
private static final int CONNECTION_POOL_SIZE_DEFAULT = 20;
/**
* Key for customizing connection pool keep-alive. In other words, how long open connections (sockets) are kept in
* pool before evicted and closed. Value is milliseconds.
*/
private static final String CONNECTION_POOL_KEEPALIVE_KEY = "nexus.apacheHttpClient4x.connectionPoolKeepalive";
/**
* Default pool keep-alive: 1 minute.
*/
private static final long CONNECTION_POOL_KEEPALIVE_DEFAULT = TimeUnit.MINUTES.toMillis( 1 );
/**
* Key for customizing connection pool timeout. In other words, how long should a HTTP request execution be blocked
* when pool is depleted, for a connection. Value is milliseconds.
*/
private static final String CONNECTION_POOL_TIMEOUT_KEY = "nexus.apacheHttpClient4x.connectionPoolTimeout";
/**
* Default pool timeout: equals to {@link #CONNECTION_POOL_KEEPALIVE_DEFAULT}.
*/
private static final long CONNECTION_POOL_TIMEOUT_DEFAULT = CONNECTION_POOL_KEEPALIVE_DEFAULT;
// ==
/**
* Application configuration holding the {@link GlobalRemoteConnectionSettings}.
*/
private final ApplicationConfiguration applicationConfiguration;
/**
* UA builder component.
*/
private final UserAgentBuilder userAgentBuilder;
/**
* The low level core event multicaster.
*/
private final ApplicationEventMulticaster applicationEventMulticaster;
/**
* Shared client connection manager.
*/
private final PoolingClientConnectionManager sharedConnectionManager;
/**
* Thread evicting idle open connections from {@link #sharedConnectionManager}.
*/
private final EvictingThread evictingThread;
/**
* Used to install created {@link PoolingClientConnectionManager} into jmx.
*/
private final PoolingClientConnectionManagerMBeanInstaller jmxInstaller;
/**
* Constructor.
*
* @param applicationConfiguration the Nexus {@link ApplicationConfiguration}.
* @param userAgentBuilder UA builder component.
* @param multicaster the event multicaster
*/
@Inject
public Hc4ProviderImpl( final ApplicationConfiguration applicationConfiguration,
final UserAgentBuilder userAgentBuilder, final ApplicationEventMulticaster multicaster,
final PoolingClientConnectionManagerMBeanInstaller jmxInstaller )
{
this.applicationConfiguration = Preconditions.checkNotNull( applicationConfiguration );
this.userAgentBuilder = Preconditions.checkNotNull( userAgentBuilder );
this.jmxInstaller = Preconditions.checkNotNull( jmxInstaller );
this.sharedConnectionManager = createClientConnectionManager();
this.evictingThread = new EvictingThread( sharedConnectionManager, getConnectionPoolKeepalive() );
this.evictingThread.start();
this.applicationEventMulticaster = Preconditions.checkNotNull( multicaster );
this.applicationEventMulticaster.addEventListener( this );
this.jmxInstaller.register( sharedConnectionManager );
getLogger().info( "{} started up (keep-alive {} millis), listening for shutdown.", getClass().getSimpleName(),
getConnectionPoolKeepalive() );
}
// configuration
/**
* Returns the pool max size.
*
* @param context
* @return pool max size
*/
protected int getConnectionPoolMaxSize()
{
return SystemPropertiesHelper.getInteger( CONNECTION_POOL_MAX_SIZE_KEY, CONNECTION_POOL_MAX_SIZE_DEFAULT );
}
/**
* Returns the pool size per route.
*
* @param context
* @return pool per route sizw
*/
protected int getConnectionPoolSize()
{
return SystemPropertiesHelper.getInteger( CONNECTION_POOL_SIZE_KEY, CONNECTION_POOL_SIZE_DEFAULT );
}
/**
* Returns the keep alive (idle open) time in milliseconds.
*
* @param context
* @return keep alive in milliseconds.
*/
protected long getConnectionPoolKeepalive()
{
return SystemPropertiesHelper.getLong( CONNECTION_POOL_KEEPALIVE_KEY, CONNECTION_POOL_KEEPALIVE_DEFAULT );
}
/**
* Returns the pool timeout in milliseconds.
*
* @param context
* @return pool timeout in milliseconds.
*/
protected long getConnectionPoolTimeout()
{
return SystemPropertiesHelper.getLong( CONNECTION_POOL_TIMEOUT_KEY, CONNECTION_POOL_TIMEOUT_DEFAULT );
}
/**
* Returns the connection timeout in milliseconds. The timeout until connection is established.
*
* @param context
* @return the connection timeout in milliseconds.
*/
protected int getConnectionTimeout( final RemoteStorageContext context )
{
if ( context.getRemoteConnectionSettings() != null )
{
return context.getRemoteConnectionSettings().getConnectionTimeout();
}
else
{
// see DefaultRemoteConnectionSetting
return 1000;
}
}
/**
* Returns the SO_SOCKET timeout in milliseconds. The timeout for waiting for data on established connection.
*
* @param context
* @return the SO_SOCKET timeout in milliseconds.
*/
protected int getSoTimeout( final RemoteStorageContext context )
{
// this parameter is actually set from #getConnectionTimeout
return getConnectionTimeout( context );
}
// ==
/**
* Performs a clean shutdown on this component, it kills the evicting thread and shuts down the shared connection
* manager. Multiple invocation of this method is safe, it will not do anything.
*/
public synchronized void shutdown()
{
evictingThread.interrupt();
jmxInstaller.unregister();
sharedConnectionManager.shutdown();
applicationEventMulticaster.removeEventListener( this );
getLogger().info( "{} shut down.", getClass().getSimpleName() );
}
// == EventListener API
@Override
public void onEvent( Event<?> evt )
{
if ( evt instanceof NexusStoppedEvent )
{
shutdown();
}
}
// ==
/**
* Safety net to prevent thread leaks (in non-production environment, mainly for ITs or UTs).
*/
@Override
protected void finalize()
throws Throwable
{
try
{
shutdown();
}
finally
{
super.finalize();
}
}
// == Hc4Provider API
@Override
public DefaultHttpClient createHttpClient()
{
final DefaultHttpClient result = createHttpClient( applicationConfiguration.getGlobalRemoteStorageContext() );
// connection manager will cap the max count of connections, but with this below
// we get rid of pooling. Pooling is used in Proxy repositories only, as all other
// components using the "shared" httpClient should not produce hiw rate of requests
// anyway, as they usually happen per user interactions (GPG gets keys are staging repo is closed, if not cached
// yet, LVO gets info when UI's main window is loaded into user's browser, etc
result.setReuseStrategy( new NoConnectionReuseStrategy() );
return result;
}
@Override
public DefaultHttpClient createHttpClient( final RemoteStorageContext context )
{
return createHttpClient( context, sharedConnectionManager );
}
// ==
protected DefaultHttpClient createHttpClient( final RemoteStorageContext context,
final ClientConnectionManager clientConnectionManager )
{
final DefaultHttpClient httpClient =
new DefaultHttpClient( clientConnectionManager, createHttpParams( context ) )
{
@Override
protected BasicHttpProcessor createHttpProcessor()
{
final BasicHttpProcessor result = super.createHttpProcessor();
result.addResponseInterceptor( new ResponseContentEncoding() );
return result;
}
};
configureAuthentication( httpClient, context.getRemoteAuthenticationSettings(), null );
configureProxy( httpClient, context.getRemoteProxySettings() );
return httpClient;
}
protected HttpParams createHttpParams( final RemoteStorageContext context )
{
HttpParams params = new SyncBasicHttpParams();
params.setParameter( HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1 );
params.setBooleanParameter( HttpProtocolParams.USE_EXPECT_CONTINUE, false );
params.setBooleanParameter( HttpConnectionParams.STALE_CONNECTION_CHECK, false );
params.setIntParameter( HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024 );
params.setLongParameter( ClientPNames.CONN_MANAGER_TIMEOUT, getConnectionPoolTimeout() );
params.setIntParameter( HttpConnectionParams.CONNECTION_TIMEOUT, getConnectionTimeout( context ) );
params.setIntParameter( HttpConnectionParams.SO_TIMEOUT, getSoTimeout( context ) );
params.setParameter( HttpProtocolParams.USER_AGENT, userAgentBuilder.formatGenericUserAgentString() );
return params;
}
protected PoolingClientConnectionManager createClientConnectionManager()
throws IllegalStateException
{
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register( new Scheme( "http", 80, PlainSocketFactory.getSocketFactory() ) );
schemeRegistry.register( new Scheme( "https", 443, SSLSocketFactory.getSocketFactory() ) );
final PoolingClientConnectionManager connManager = new PoolingClientConnectionManager( schemeRegistry );
final int maxConnectionCount = getConnectionPoolMaxSize();
final int perRouteConnectionCount = Math.min( getConnectionPoolSize(), maxConnectionCount );
connManager.setMaxTotal( maxConnectionCount );
connManager.setDefaultMaxPerRoute( perRouteConnectionCount );
return connManager;
}
// ==
protected void configureAuthentication( final DefaultHttpClient httpClient, final RemoteAuthenticationSettings ras,
final HttpHost proxyHost )
{
if ( ras != null )
{
String authScope = "target";
if ( proxyHost != null )
{
authScope = proxyHost.toHostString() + " proxy";
}
List<String> authorisationPreference = new ArrayList<String>( 2 );
authorisationPreference.add( AuthPolicy.DIGEST );
authorisationPreference.add( AuthPolicy.BASIC );
Credentials credentials = null;
if ( ras instanceof ClientSSLRemoteAuthenticationSettings )
{
throw new IllegalArgumentException( "SSL client authentication not yet supported!" );
}
else if ( ras instanceof NtlmRemoteAuthenticationSettings )
{
final NtlmRemoteAuthenticationSettings nras = (NtlmRemoteAuthenticationSettings) ras;
// Using NTLM auth, adding it as first in policies
authorisationPreference.add( 0, AuthPolicy.NTLM );
getLogger().info( "... {} authentication setup for NTLM domain '{}'", authScope, nras.getNtlmDomain() );
credentials =
new NTCredentials( nras.getUsername(), nras.getPassword(), nras.getNtlmHost(), nras.getNtlmDomain() );
}
else if ( ras instanceof UsernamePasswordRemoteAuthenticationSettings )
{
final UsernamePasswordRemoteAuthenticationSettings uras =
(UsernamePasswordRemoteAuthenticationSettings) ras;
getLogger().info( "... {} authentication setup for remote storage with username '{}'", authScope,
uras.getUsername() );
credentials = new UsernamePasswordCredentials( uras.getUsername(), uras.getPassword() );
}
if ( credentials != null )
{
if ( proxyHost != null )
{
httpClient.getCredentialsProvider().setCredentials( new AuthScope( proxyHost ), credentials );
httpClient.getParams().setParameter( AuthPNames.PROXY_AUTH_PREF, authorisationPreference );
}
else
{
httpClient.getCredentialsProvider().setCredentials( AuthScope.ANY, credentials );
httpClient.getParams().setParameter( AuthPNames.TARGET_AUTH_PREF, authorisationPreference );
}
}
}
}
protected void configureProxy( final DefaultHttpClient httpClient, final RemoteProxySettings remoteProxySettings )
{
if ( remoteProxySettings.isEnabled() )
{
getLogger().info( "... proxy setup with host '{}'", remoteProxySettings.getHostname() );
final HttpHost proxy = new HttpHost( remoteProxySettings.getHostname(), remoteProxySettings.getPort() );
httpClient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy );
// check if we have non-proxy hosts
if ( remoteProxySettings.getNonProxyHosts() != null && !remoteProxySettings.getNonProxyHosts().isEmpty() )
{
final Set<Pattern> nonProxyHostPatterns =
new HashSet<Pattern>( remoteProxySettings.getNonProxyHosts().size() );
for ( String nonProxyHostRegex : remoteProxySettings.getNonProxyHosts() )
{
try
{
nonProxyHostPatterns.add( Pattern.compile( nonProxyHostRegex, Pattern.CASE_INSENSITIVE ) );
}
catch ( PatternSyntaxException e )
{
getLogger().warn( "Invalid non proxy host regex: {}", nonProxyHostRegex, e );
}
}
httpClient.setRoutePlanner( new NonProxyHostsAwareHttpRoutePlanner(
httpClient.getConnectionManager().getSchemeRegistry(), nonProxyHostPatterns ) );
}
configureAuthentication( httpClient, remoteProxySettings.getProxyAuthentication(), proxy );
}
}
}