Search in sources :

Example 31 with ClientException

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;
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) ClientException(com.aliyun.oss.ClientException) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder)

Example 32 with ClientException

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) ClientException(com.aliyun.oss.ClientException) HttpMethod(com.aliyun.oss.HttpMethod) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead)

Example 33 with ClientException

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);
    }
}
Also used : ClientException(com.aliyun.oss.ClientException)

Example 34 with ClientException

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);
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) OSSException(com.aliyun.oss.OSSException) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ClientException(com.aliyun.oss.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 35 with ClientException

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);
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) ServiceException(com.aliyun.oss.ServiceException) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ArrayList(java.util.ArrayList) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) ClientException(com.aliyun.oss.ClientException)

Aggregations

ClientException (com.aliyun.oss.ClientException)48 OSSException (com.aliyun.oss.OSSException)27 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 OSS (com.aliyun.oss.OSS)16 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 Test (org.junit.Test)11 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)9 OSSObject (com.aliyun.oss.model.OSSObject)9 ArrayList (java.util.ArrayList)9 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)8 File (java.io.File)7 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)6 ObjectListing (com.aliyun.oss.model.ObjectListing)5 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)5 ClientConfiguration (com.aliyun.oss.ClientConfiguration)4 ServiceException (com.aliyun.oss.ServiceException)4 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)4 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)4