下面列出了org.mockito.internal.verification.Times#software.amazon.awssdk.http.SdkHttpConfigurationOption 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private NettyNioAsyncHttpClient(DefaultBuilder builder, AttributeMap serviceDefaultsMap) {
this.configuration = new NettyConfiguration(serviceDefaultsMap);
Protocol protocol = serviceDefaultsMap.get(SdkHttpConfigurationOption.PROTOCOL);
this.sdkEventLoopGroup = eventLoopGroup(builder);
Http2Configuration http2Configuration = builder.http2Configuration;
long maxStreams = resolveMaxHttp2Streams(builder.maxHttp2Streams, http2Configuration);
int initialWindowSize = resolveInitialWindowSize(http2Configuration);
this.pools = AwaitCloseChannelPoolMap.builder()
.sdkChannelOptions(builder.sdkChannelOptions)
.configuration(configuration)
.protocol(protocol)
.maxStreams(maxStreams)
.initialWindowSize(initialWindowSize)
.healthCheckPingPeriod(resolveHealthCheckPingPeriod(http2Configuration))
.sdkEventLoopGroup(sdkEventLoopGroup)
.sslProvider(resolveSslProvider(builder))
.proxyConfiguration(builder.proxyConfiguration)
.build();
}
@Test
public void idleConnectionReaperDoesNotReapActiveConnections() throws InterruptedException {
Duration maxIdleTime = Duration.ofSeconds(2);
try(SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder()
.connectionMaxIdleTime(maxIdleTime)
.buildWithDefaults(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS)) {
Instant end = Instant.now().plus(maxIdleTime.plusSeconds(1));
// Send requests for longer than the max-idle time, ensuring no connections are closed.
while (Instant.now().isBefore(end)) {
makeRequest(client);
Thread.sleep(100);
verify(TRAFFIC_LISTENER, new Times(0)).closed(any());
}
// Do nothing for longer than the max-idle time, ensuring connections are closed.
Thread.sleep(maxIdleTime.plusSeconds(1).toMillis());
verify(TRAFFIC_LISTENER, new AtLeast(1)).closed(any());
}
}
@Test
public void oldConnectionReaperReapsActiveConnections() throws InterruptedException {
Duration connectionTtl = Duration.ofMillis(200);
try (SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder()
.connectionTimeToLive(connectionTtl)
.buildWithDefaults(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS)) {
Instant end = Instant.now().plus(Duration.ofSeconds(5));
verify(TRAFFIC_LISTENER, new Times(0)).closed(any());
// Send requests frequently, validating that connections are still being closed.
while (Instant.now().isBefore(end)) {
makeRequest(client);
Thread.sleep(100);
}
verify(TRAFFIC_LISTENER, new AtLeast(20)).closed(any());
}
}
public HttpClientConnectionManager create(ApacheHttpClient.DefaultBuilder configuration,
AttributeMap standardOptions) {
ConnectionSocketFactory sslsf = getPreferredSocketFactory(configuration, standardOptions);
PoolingHttpClientConnectionManager cm = new
PoolingHttpClientConnectionManager(
createSocketFactoryRegistry(sslsf),
null,
DefaultSchemePortResolver.INSTANCE,
null,
standardOptions.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE).toMillis(),
TimeUnit.MILLISECONDS);
cm.setDefaultMaxPerRoute(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS));
cm.setMaxTotal(standardOptions.get(SdkHttpConfigurationOption.MAX_CONNECTIONS));
cm.setDefaultSocketConfig(buildSocketConfig(standardOptions));
return cm;
}
private HttpURLConnection createDefaultConnection(URI uri) {
HttpURLConnection connection = invokeSafely(() -> (HttpURLConnection) uri.toURL().openConnection());
if (connection instanceof HttpsURLConnection) {
HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
if (options.get(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES)) {
httpsConnection.setHostnameVerifier(NoOpHostNameVerifier.INSTANCE);
}
httpsConnection.setSSLSocketFactory(sslContext.getSocketFactory());
}
connection.setConnectTimeout(saturatedCast(options.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT).toMillis()));
connection.setReadTimeout(saturatedCast(options.get(SdkHttpConfigurationOption.READ_TIMEOUT).toMillis()));
return connection;
}
private CodeBlock serviceSpecificHttpConfigMethodBody(String serviceDefaultFqcn, boolean supportsH2) {
CodeBlock.Builder builder = CodeBlock.builder();
if (serviceDefaultFqcn != null) {
builder.addStatement("$T result = $T.defaultHttpConfig()",
AttributeMap.class,
PoetUtils.classNameFromFqcn(model.getCustomizationConfig().getServiceSpecificHttpConfig()));
} else {
builder.addStatement("$1T result = $1T.empty()", AttributeMap.class);
}
if (supportsH2) {
builder.addStatement("return result.merge(AttributeMap.builder()"
+ ".put($T.PROTOCOL, $T.HTTP2)"
+ ".build())",
SdkHttpConfigurationOption.class, Protocol.class);
} else {
builder.addStatement("return result");
}
return builder.build();
}
@Test
public void testHttpClientConfigurationSerializationDeserialization() throws Exception {
AttributeMap attributeMap =
AttributeMap.builder()
.put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, Duration.parse("PT100S"))
.put(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE, Duration.parse("PT30S"))
.put(SdkHttpConfigurationOption.MAX_CONNECTIONS, 15)
.build();
String valueAsJson = objectMapper.writeValueAsString(attributeMap);
AttributeMap deserializedAttributeMap = objectMapper.readValue(valueAsJson, AttributeMap.class);
assertEquals(
Duration.parse("PT100S"),
deserializedAttributeMap.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT));
assertEquals(
Duration.parse("PT30S"),
deserializedAttributeMap.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE));
assertEquals(
(Integer) 15, deserializedAttributeMap.get(SdkHttpConfigurationOption.MAX_CONNECTIONS));
}
@Override
public SdkAsyncHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
return new NettyNioAsyncHttpClient(this, standardOptions.build()
.merge(serviceDefaults)
.merge(NETTY_HTTP_DEFAULTS)
.merge(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS));
}
private ConnectionManagerAwareHttpClient createClient(ApacheHttpClient.DefaultBuilder configuration,
AttributeMap standardOptions) {
ApacheConnectionManagerFactory cmFactory = new ApacheConnectionManagerFactory();
HttpClientBuilder builder = HttpClients.custom();
// Note that it is important we register the original connection manager with the
// IdleConnectionReaper as it's required for the successful deregistration of managers
// from the reaper. See https://github.com/aws/aws-sdk-java/issues/722.
HttpClientConnectionManager cm = cmFactory.create(configuration, standardOptions);
builder.setRequestExecutor(new HttpRequestExecutor())
// SDK handles decompression
.disableContentCompression()
.setKeepAliveStrategy(buildKeepAliveStrategy(standardOptions))
.disableRedirectHandling()
.disableAutomaticRetries()
.setUserAgent("") // SDK will set the user agent header in the pipeline. Don't let Apache waste time
.setConnectionManager(ClientConnectionManagerFactory.wrap(cm));
addProxyConfig(builder, configuration);
if (useIdleConnectionReaper(standardOptions)) {
IdleConnectionReaper.getInstance().registerConnectionManager(
cm, standardOptions.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toMillis());
}
return new ApacheSdkHttpClient(builder.build(), cm);
}
private ApacheHttpRequestConfig createRequestConfig(DefaultBuilder builder,
AttributeMap resolvedOptions) {
return ApacheHttpRequestConfig.builder()
.socketTimeout(resolvedOptions.get(SdkHttpConfigurationOption.READ_TIMEOUT))
.connectionTimeout(resolvedOptions.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT))
.connectionAcquireTimeout(
resolvedOptions.get(SdkHttpConfigurationOption.CONNECTION_ACQUIRE_TIMEOUT))
.proxyConfiguration(builder.proxyConfiguration)
.localAddress(Optional.ofNullable(builder.localAddress).orElse(null))
.expectContinueEnabled(Optional.ofNullable(builder.expectContinueEnabled)
.orElse(DefaultConfiguration.EXPECT_CONTINUE_ENABLED))
.build();
}
private SSLContext getSslContext(AttributeMap standardOptions) {
Validate.isTrue(standardOptions.get(SdkHttpConfigurationOption.TLS_TRUST_MANAGERS_PROVIDER) == null ||
!standardOptions.get(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES),
"A TlsTrustManagerProvider can't be provided if TrustAllCertificates is also set");
TrustManager[] trustManagers = null;
if (standardOptions.get(SdkHttpConfigurationOption.TLS_TRUST_MANAGERS_PROVIDER) != null) {
trustManagers = standardOptions.get(SdkHttpConfigurationOption.TLS_TRUST_MANAGERS_PROVIDER).trustManagers();
}
if (standardOptions.get(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES)) {
log.warn(() -> "SSL Certificate verification is disabled. This is not a safe setting and should only be "
+ "used for testing.");
trustManagers = trustAllTrustManager();
}
TlsKeyManagersProvider provider = standardOptions.get(SdkHttpConfigurationOption.TLS_KEY_MANAGERS_PROVIDER);
KeyManager[] keyManagers = provider.keyManagers();
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
// http://download.java.net/jdk9/docs/technotes/guides/security/jsse/JSSERefGuide.html
sslcontext.init(keyManagers, trustManagers, null);
return sslcontext;
} catch (final NoSuchAlgorithmException | KeyManagementException ex) {
throw new SSLInitializationException(ex.getMessage(), ex);
}
}
private SocketConfig buildSocketConfig(AttributeMap standardOptions) {
return SocketConfig.custom()
// TODO do we want to keep SO keep alive
.setSoKeepAlive(false)
.setSoTimeout(
saturatedCast(standardOptions.get(SdkHttpConfigurationOption.READ_TIMEOUT)
.toMillis()))
.setTcpNoDelay(true)
.build();
}
private SSLContext getSslContext(AttributeMap options) {
Validate.isTrue(options.get(SdkHttpConfigurationOption.TLS_TRUST_MANAGERS_PROVIDER) == null ||
!options.get(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES),
"A TlsTrustManagerProvider can't be provided if TrustAllCertificates is also set");
TrustManager[] trustManagers = null;
if (options.get(SdkHttpConfigurationOption.TLS_TRUST_MANAGERS_PROVIDER) != null) {
trustManagers = options.get(SdkHttpConfigurationOption.TLS_TRUST_MANAGERS_PROVIDER).trustManagers();
}
if (options.get(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES)) {
log.warn(() -> "SSL Certificate verification is disabled. This is not a safe setting and should only be "
+ "used for testing.");
trustManagers = new TrustManager[] { TrustAllManager.INSTANCE };
}
TlsKeyManagersProvider provider = this.options.get(SdkHttpConfigurationOption.TLS_KEY_MANAGERS_PROVIDER);
KeyManager[] keyManagers = provider.keyManagers();
SSLContext context;
try {
context = SSLContext.getInstance("TLS");
context.init(keyManagers, trustManagers, null);
return context;
} catch (NoSuchAlgorithmException | KeyManagementException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
/**
* Used by the SDK to create a {@link SdkHttpClient} with service-default values if no other values have been configured
*
* @param serviceDefaults Service specific defaults. Keys will be one of the constants defined in
* {@link SdkHttpConfigurationOption}.
* @return an instance of {@link SdkHttpClient}
*/
@Override
public SdkHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
return new UrlConnectionHttpClient(standardOptions.build()
.merge(serviceDefaults)
.merge(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS),
null);
}
@Override
public AttributeMap deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException {
Map<String, String> map = jsonParser.readValueAs(new TypeReference<Map<String, String>>() {});
// Add new attributes below.
final AttributeMap.Builder attributeMapBuilder = AttributeMap.builder();
if (map.containsKey(CONNECTION_ACQUIRE_TIMEOUT)) {
attributeMapBuilder.put(
SdkHttpConfigurationOption.CONNECTION_ACQUIRE_TIMEOUT,
Duration.parse(map.get(CONNECTION_ACQUIRE_TIMEOUT)));
}
if (map.containsKey(CONNECTION_MAX_IDLE_TIMEOUT)) {
attributeMapBuilder.put(
SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT,
Duration.parse(map.get(CONNECTION_MAX_IDLE_TIMEOUT)));
}
if (map.containsKey(CONNECTION_TIMEOUT)) {
attributeMapBuilder.put(
SdkHttpConfigurationOption.CONNECTION_TIMEOUT,
Duration.parse(map.get(CONNECTION_TIMEOUT)));
}
if (map.containsKey(CONNECTION_TIME_TO_LIVE)) {
attributeMapBuilder.put(
SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE,
Duration.parse(map.get(CONNECTION_TIME_TO_LIVE)));
}
if (map.containsKey(MAX_CONNECTIONS)) {
attributeMapBuilder.put(
SdkHttpConfigurationOption.MAX_CONNECTIONS, Integer.parseInt(map.get(MAX_CONNECTIONS)));
}
if (map.containsKey(READ_TIMEOUT)) {
attributeMapBuilder.put(
SdkHttpConfigurationOption.READ_TIMEOUT, Duration.parse(map.get(READ_TIMEOUT)));
}
return attributeMapBuilder.build();
}
@Override
public void serialize(
AttributeMap attributeMap, JsonGenerator jsonGenerator, SerializerProvider serializer)
throws IOException {
jsonGenerator.writeStartObject();
if (attributeMap.containsKey(SdkHttpConfigurationOption.CONNECTION_ACQUIRE_TIMEOUT)) {
jsonGenerator.writeStringField(
CONNECTION_ACQUIRE_TIMEOUT,
String.valueOf(
attributeMap.get(SdkHttpConfigurationOption.CONNECTION_ACQUIRE_TIMEOUT)));
}
if (attributeMap.containsKey(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT)) {
jsonGenerator.writeStringField(
CONNECTION_MAX_IDLE_TIMEOUT,
String.valueOf(
attributeMap.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT)));
}
if (attributeMap.containsKey(SdkHttpConfigurationOption.CONNECTION_TIMEOUT)) {
jsonGenerator.writeStringField(
CONNECTION_TIMEOUT,
String.valueOf(attributeMap.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT)));
}
if (attributeMap.containsKey(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE)) {
jsonGenerator.writeStringField(
CONNECTION_TIME_TO_LIVE,
String.valueOf(attributeMap.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE)));
}
if (attributeMap.containsKey(SdkHttpConfigurationOption.MAX_CONNECTIONS)) {
jsonGenerator.writeStringField(
MAX_CONNECTIONS,
String.valueOf(attributeMap.get(SdkHttpConfigurationOption.MAX_CONNECTIONS)));
}
if (attributeMap.containsKey(SdkHttpConfigurationOption.READ_TIMEOUT)) {
jsonGenerator.writeStringField(
READ_TIMEOUT,
String.valueOf(attributeMap.get(SdkHttpConfigurationOption.READ_TIMEOUT)));
}
jsonGenerator.writeEndObject();
}
@Override
public Builder maxPendingConnectionAcquires(Integer maxPendingAcquires) {
standardOptions.put(SdkHttpConfigurationOption.MAX_PENDING_CONNECTION_ACQUIRES, maxPendingAcquires);
return this;
}
@Override
public Builder connectionTimeout(Duration timeout) {
Validate.isPositive(timeout, "connectionTimeout");
standardOptions.put(SdkHttpConfigurationOption.CONNECTION_TIMEOUT, timeout);
return this;
}
public int readTimeoutMillis() {
return saturatedCast(configuration.get(SdkHttpConfigurationOption.READ_TIMEOUT).toMillis());
}
public int writeTimeoutMillis() {
return saturatedCast(configuration.get(SdkHttpConfigurationOption.WRITE_TIMEOUT).toMillis());
}
public int idleTimeoutMillis() {
return saturatedCast(configuration.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toMillis());
}
public int connectionTtlMillis() {
return saturatedCast(configuration.get(SdkHttpConfigurationOption.CONNECTION_TIME_TO_LIVE).toMillis());
}
public boolean reapIdleConnections() {
return configuration.get(SdkHttpConfigurationOption.REAP_IDLE_CONNECTIONS);
}
public TlsKeyManagersProvider tlsKeyManagersProvider() {
return configuration.get(SdkHttpConfigurationOption.TLS_KEY_MANAGERS_PROVIDER);
}
public TlsTrustManagersProvider tlsTrustManagersProvider() {
return configuration.get(SdkHttpConfigurationOption.TLS_TRUST_MANAGERS_PROVIDER);
}
private static AttributeMap mapWithTrustAllCerts() {
return AttributeMap.builder()
.put(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, true)
.build();
}
private static AttributeMap mapWithTrustAllCerts() {
return AttributeMap.builder()
.put(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, true)
.build();
}
private ConnectionKeepAliveStrategy buildKeepAliveStrategy(AttributeMap standardOptions) {
long maxIdle = standardOptions.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toMillis();
return maxIdle > 0 ? new SdkConnectionKeepAliveStrategy(maxIdle) : null;
}
private boolean useIdleConnectionReaper(AttributeMap standardOptions) {
return Boolean.TRUE.equals(standardOptions.get(SdkHttpConfigurationOption.REAP_IDLE_CONNECTIONS));
}
@Override
public Builder connectionMaxIdleTime(Duration maxIdleConnectionTimeout) {
standardOptions.put(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT, maxIdleConnectionTimeout);
return this;
}