下面列出了怎么用com.google.api.client.http.apache.v2.ApacheHttpTransport的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected Drive connect(final Proxy proxy, final HostKeyCallback callback, final LoginCallback prompt) {
final HttpClientBuilder configuration = builder.build(proxy, this, prompt);
authorizationService = new OAuth2RequestInterceptor(configuration.build(), host.getProtocol())
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl());
configuration.addInterceptorLast(authorizationService);
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt));
configuration.addInterceptorLast(new RateLimitingHttpRequestInterceptor(new DefaultHttpRateLimiter(
PreferencesFactory.get().getInteger("googledrive.limit.requests.second")
)));
this.transport = new ApacheHttpTransport(configuration.build());
final UseragentProvider ua = new PreferencesUseragentProvider();
return new Drive.Builder(transport, new JacksonFactory(), new UserAgentHttpRequestInitializer(ua))
.setApplicationName(ua.get())
.build();
}
/**
* Validates and builds a {@link HttpEventPublisher} object.
*
* @return {@link HttpEventPublisher}
*/
public HttpEventPublisher build()
throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
checkNotNull(token(), "Authentication token needs to be specified via withToken(token).");
checkNotNull(genericUrl(), "URL needs to be specified via withUrl(url).");
if (disableCertificateValidation() == null) {
LOG.info("Certificate validation disabled: {}", DEFAULT_DISABLE_CERTIFICATE_VALIDATION);
setDisableCertificateValidation(DEFAULT_DISABLE_CERTIFICATE_VALIDATION);
}
if (maxElapsedMillis() == null) {
LOG.info(
"Defaulting max backoff time to: {} milliseconds ",
ExponentialBackOff.DEFAULT_MAX_ELAPSED_TIME_MILLIS);
setMaxElapsedMillis(ExponentialBackOff.DEFAULT_MAX_ELAPSED_TIME_MILLIS);
}
CloseableHttpClient httpClient =
getHttpClient(DEFAULT_MAX_CONNECTIONS, disableCertificateValidation());
setTransport(new ApacheHttpTransport(httpClient));
setRequestFactory(transport().createRequestFactory());
return autoBuild();
}
/**
* Utility method to create a {@link CloseableHttpClient} to make http POSTs against Splunk's
* HEC.
*
* @param maxConnections max number of parallel connections.
* @param disableCertificateValidation should disable certificate validation.
*/
private CloseableHttpClient getHttpClient(
int maxConnections, boolean disableCertificateValidation)
throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
HttpClientBuilder builder = ApacheHttpTransport.newDefaultHttpClientBuilder();
if (genericUrl().getScheme().equalsIgnoreCase(HTTPS_PROTOCOL_PREFIX)) {
LOG.info("SSL connection requested");
HostnameVerifier hostnameVerifier =
disableCertificateValidation
? NoopHostnameVerifier.INSTANCE
: new DefaultHostnameVerifier();
SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
if (disableCertificateValidation) {
LOG.info("Certificate validation is disabled");
sslContextBuilder.loadTrustMaterial((TrustStrategy) (chain, authType) -> true);
}
SSLConnectionSocketFactory connectionSocketFactory =
new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier);
builder.setSSLSocketFactory(connectionSocketFactory);
}
builder.setMaxConnTotal(maxConnections);
builder.setDefaultRequestConfig(
RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build());
return builder.build();
}
@Override
public Storage connect(final Proxy proxy, final HostKeyCallback key, final LoginCallback prompt) {
final HttpClientBuilder configuration = builder.build(proxy, this, prompt);
authorizationService = new OAuth2RequestInterceptor(configuration.build(), host.getProtocol())
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl());
configuration.addInterceptorLast(authorizationService);
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt));
this.transport = new ApacheHttpTransport(configuration.build());
final UseragentProvider ua = new PreferencesUseragentProvider();
return new Storage.Builder(transport, new JacksonFactory(), new UserAgentHttpRequestInitializer(ua))
.setApplicationName(ua.get())
.build();
}
/**
* Validates and builds a {@link HttpEventPublisher} object.
*
* @return {@link HttpEventPublisher}
*/
HttpEventPublisher build()
throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
checkNotNull(token(), "Authentication token needs to be specified via withToken(token).");
checkNotNull(genericUrl(), "URL needs to be specified via withUrl(url).");
if (disableCertificateValidation() == null) {
LOG.info("Certificate validation disabled: {}", DEFAULT_DISABLE_CERTIFICATE_VALIDATION);
setDisableCertificateValidation(DEFAULT_DISABLE_CERTIFICATE_VALIDATION);
}
if (maxElapsedMillis() == null) {
LOG.info(
"Defaulting max backoff time to: {} milliseconds ",
ExponentialBackOff.DEFAULT_MAX_ELAPSED_TIME_MILLIS);
setMaxElapsedMillis(ExponentialBackOff.DEFAULT_MAX_ELAPSED_TIME_MILLIS);
}
CloseableHttpClient httpClient =
getHttpClient(DEFAULT_MAX_CONNECTIONS, disableCertificateValidation());
setTransport(new ApacheHttpTransport(httpClient));
setRequestFactory(transport().createRequestFactory());
return autoBuild();
}
/**
* Creates a {@link CloseableHttpClient} to make HTTP POSTs against Splunk's HEC.
*
* @param maxConnections max number of parallel connections
* @param disableCertificateValidation should disable certificate validation
*/
private CloseableHttpClient getHttpClient(
int maxConnections, boolean disableCertificateValidation)
throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
HttpClientBuilder builder = ApacheHttpTransport.newDefaultHttpClientBuilder();
if (genericUrl().getScheme().equalsIgnoreCase(HTTPS_PROTOCOL_PREFIX)) {
LOG.info("SSL connection requested");
HostnameVerifier hostnameVerifier =
disableCertificateValidation
? NoopHostnameVerifier.INSTANCE
: new DefaultHostnameVerifier();
SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
if (disableCertificateValidation) {
LOG.info("Certificate validation is disabled");
sslContextBuilder.loadTrustMaterial((TrustStrategy) (chain, authType) -> true);
}
SSLConnectionSocketFactory connectionSocketFactory =
new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier);
builder.setSSLSocketFactory(connectionSocketFactory);
}
builder.setMaxConnTotal(maxConnections);
builder.setDefaultRequestConfig(
RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build());
return builder.build();
}
public OAuth2AuthorizationService(final HttpClient client,
final String tokenServerUrl, final String authorizationServerUrl,
final String clientid, final String clientsecret, final List<String> scopes) {
this(new ApacheHttpTransport(client),
tokenServerUrl, authorizationServerUrl, clientid, clientsecret, scopes);
}
abstract ApacheHttpTransport transport();
abstract Builder setTransport(ApacheHttpTransport transport);
abstract ApacheHttpTransport transport();
abstract ApacheHttpTransport transport();
abstract Builder setTransport(ApacheHttpTransport transport);
abstract ApacheHttpTransport transport();