use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class DefaultServiceClient method createHttpClientConnectionManager.
protected HttpClientConnectionManager createHttpClientConnectionManager() {
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
} catch (Exception e) {
throw new ClientException(e.getMessage());
}
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register(Protocol.HTTP.toString(), PlainConnectionSocketFactory.getSocketFactory()).register(Protocol.HTTPS.toString(), sslSocketFactory).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setDefaultMaxPerRoute(config.getMaxConnections());
connectionManager.setMaxTotal(config.getMaxConnections());
connectionManager.setValidateAfterInactivity(config.getValidateAfterInactivity());
connectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(config.getSocketTimeout()).setTcpNoDelay(true).build());
if (config.isUseReaper()) {
IdleConnectionReaper.setIdleConnectionTime(config.getIdleConnectionTime());
IdleConnectionReaper.registerConnectionManager(connectionManager);
}
return connectionManager;
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class HttpRequestFactory method createHttpRequest.
public HttpRequestBase createHttpRequest(ServiceClient.Request request, ExecutionContext context) {
String uri = request.getUri();
HttpRequestBase httpRequest;
HttpMethod method = request.getMethod();
if (method == HttpMethod.POST) {
HttpPost postMethod = new HttpPost(uri);
if (request.getContent() != null) {
postMethod.setEntity(new RepeatableInputStreamEntity(request));
}
httpRequest = postMethod;
} else if (method == HttpMethod.PUT) {
HttpPut putMethod = new HttpPut(uri);
if (request.getContent() != null) {
if (request.isUseChunkEncoding()) {
putMethod.setEntity(buildChunkedInputStreamEntity(request));
} else {
putMethod.setEntity(new RepeatableInputStreamEntity(request));
}
}
httpRequest = putMethod;
} else if (method == HttpMethod.GET) {
httpRequest = new HttpGet(uri);
} else if (method == HttpMethod.DELETE) {
httpRequest = new HttpDelete(uri);
} else if (method == HttpMethod.HEAD) {
httpRequest = new HttpHead(uri);
} else if (method == HttpMethod.OPTIONS) {
httpRequest = new HttpOptions(uri);
} else {
throw new ClientException("Unknown HTTP method name: " + method.toString());
}
configureRequestHeaders(request, context, httpRequest);
return httpRequest;
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class ServiceClient method pause.
private void pause(int retries, RetryStrategy retryStrategy) throws ClientException {
long delay = retryStrategy.getPauseDelay(retries);
getLog().debug("An retriable error request will be retried after " + delay + "(ms) with attempt times: " + retries);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
throw new ClientException(e.getMessage(), e);
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class TimeoutServiceClient method sendRequestCore.
@Override
public ResponseMessage sendRequestCore(ServiceClient.Request request, ExecutionContext context) throws IOException {
HttpRequestBase httpRequest = httpRequestFactory.createHttpRequest(request, context);
HttpClientContext httpContext = HttpClientContext.create();
httpContext.setRequestConfig(this.requestConfig);
CloseableHttpResponse httpResponse = null;
HttpRequestTask httpRequestTask = new HttpRequestTask(httpRequest, httpContext);
Future<CloseableHttpResponse> future = executor.submit(httpRequestTask);
try {
httpResponse = future.get(this.config.getRequestTimeout(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
logException("[ExecutorService]The current thread was interrupted while waiting: ", e);
httpRequest.abort();
throw new ClientException(e.getMessage(), e);
} catch (ExecutionException e) {
RuntimeException ex;
httpRequest.abort();
if (e.getCause() instanceof IOException) {
ex = ExceptionFactory.createNetworkException((IOException) e.getCause());
} else {
ex = new OSSException(e.getMessage(), e);
}
logException("[ExecutorService]The computation threw an exception: ", ex);
throw ex;
} catch (TimeoutException e) {
logException("[ExecutorService]The wait " + this.config.getRequestTimeout() + " timed out: ", e);
httpRequest.abort();
throw new ClientException(e.getMessage(), OSSErrorCode.REQUEST_TIMEOUT, "Unknown", e);
}
return buildResponse(request, httpResponse);
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class OSSBucketOperation method getBucketMetadata.
public BucketMetadata getBucketMetadata(GenericRequest genericRequest) throws OSSException, ClientException {
assertParameterNotNull(genericRequest, "genericRequest");
String bucketName = genericRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.HEAD).setBucket(bucketName).setOriginalRequest(genericRequest).build();
List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
reponseHandlers.add(new ResponseHandler() {
@Override
public void handle(ResponseMessage response) throws ServiceException, ClientException {
if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
safeCloseResponse(response);
throw ExceptionFactory.createOSSException(response.getHeaders().get(OSSHeaders.OSS_HEADER_REQUEST_ID), OSSErrorCode.NO_SUCH_BUCKET, OSS_RESOURCE_MANAGER.getString("NoSuchBucket"));
}
}
});
return doOperation(request, ResponseParsers.getBucketMetadataResponseParser, bucketName, null, true, null, reponseHandlers);
}
Aggregations