Search in sources :

Example 36 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class ServiceClientTest method testRetryWithServiceException.

@Test
public void testRetryWithServiceException() throws Exception {
    // This request will fail after 1 retries
    ClientConfiguration config = new ClientConfiguration();
    config.setMaxErrorRetry(1);
    // It should be always successful.
    int maxFailures = 0;
    String content = "Let's retry!";
    byte[] contentBytes = content.getBytes(OSSConstants.DEFAULT_CHARSET_NAME);
    ByteArrayInputStream contentStream = new ByteArrayInputStream(contentBytes);
    RequestMessage request = new RequestMessage(null, null);
    request.setEndpoint(new URI("http://localhost"));
    request.setMethod(HttpMethod.GET);
    request.setContent(contentStream);
    request.setContentLength(contentBytes.length);
    ExecutionContext context = new ExecutionContext();
    context.getResponseHandlers().add(new ResponseHandler() {

        @Override
        public void handle(ResponseMessage responseData) throws ServiceException, ClientException {
            throw new ServiceException();
        }
    });
    // This request will succeed after 2 retries
    ServiceClientImpl client = new ServiceClientImpl(config, maxFailures, null, 500, content);
    // Fix the max error retry count to 3
    try {
        client.sendRequest(request, context);
        fail("ServiceException has not been thrown.");
    } catch (ServiceException e) {
        assertEquals(2, client.getRequestAttempts());
    }
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) URI(java.net.URI) ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) ServiceException(com.aliyun.oss.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) ClientConfiguration(com.aliyun.oss.ClientConfiguration) Test(org.junit.Test)

Example 37 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class ServiceClientTest method testRetryWillSucceed.

@Test
public void testRetryWillSucceed() throws Exception {
    // Fix the max error retry count to 3
    final int MAX_RETRIES = 3;
    ClientConfiguration config = new ClientConfiguration();
    config.setMaxErrorRetry(MAX_RETRIES);
    int maxFailures = 3;
    final long skipBeforeSend = 3;
    String content = "Let's retry!";
    byte[] contentBytes = content.getBytes(OSSConstants.DEFAULT_CHARSET_NAME);
    ByteArrayInputStream contentStream = new ByteArrayInputStream(contentBytes);
    contentStream.skip(skipBeforeSend);
    RequestMessage request = new RequestMessage(null, null);
    request.setEndpoint(new URI("http://localhost"));
    request.setMethod(HttpMethod.GET);
    request.setContent(contentStream);
    request.setContentLength(contentBytes.length - skipBeforeSend);
    ExecutionContext context = new ExecutionContext();
    ClientException exceptionToThrow = createRetryableException();
    // This request will succeed after 2 retries
    ServiceClientImpl client = new ServiceClientImpl(config, maxFailures, exceptionToThrow, 200, content.substring((int) skipBeforeSend));
    client.sendRequest(request, context);
    assertEquals(MAX_RETRIES + 1, client.getRequestAttempts());
}
Also used : ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) URI(java.net.URI) ClientConfiguration(com.aliyun.oss.ClientConfiguration) Test(org.junit.Test)

Example 38 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class ServiceClientTest method retryButFail.

private void retryButFail(final int maxRetries) throws UnsupportedEncodingException, URISyntaxException, ServiceException {
    // This request will fail after 3 retries
    ClientConfiguration config = new ClientConfiguration();
    config.setMaxErrorRetry(maxRetries);
    int maxFailures = 4;
    String content = "Let's retry!";
    byte[] contentBytes = content.getBytes(OSSConstants.DEFAULT_CHARSET_NAME);
    ByteArrayInputStream contentStream = new ByteArrayInputStream(contentBytes);
    RequestMessage request = new RequestMessage(null, null);
    request.setEndpoint(new URI("http://localhost"));
    request.setMethod(HttpMethod.GET);
    request.setContent(contentStream);
    request.setContentLength(contentBytes.length);
    ExecutionContext context = new ExecutionContext();
    ClientException exceptionToThrown = createRetryableException();
    ServiceClientImpl client = new ServiceClientImpl(config, maxFailures, exceptionToThrown, 200, content);
    try {
        client.sendRequest(request, context);
        fail("ClientException has not been thrown.");
    } catch (ClientException e) {
        assertEquals(exceptionToThrown, e);
        assertEquals(maxRetries + 1, client.getRequestAttempts());
    }
}
Also used : ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) URI(java.net.URI) ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Example 39 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class ServiceClientTest method testRetryWithNoneMarkSupportedStream.

@Test
public void testRetryWithNoneMarkSupportedStream() throws Exception {
    String filename = ResourceUtils.getTestFilename("oss/listBucket.xml");
    File file = new File(filename);
    InputStream contentStream = new FileInputStream(file);
    RequestMessage request = new RequestMessage(null, null);
    request.setEndpoint(new URI("http://localhost"));
    request.setMethod(HttpMethod.GET);
    request.setContent(contentStream);
    request.setContentLength(file.length());
    ExecutionContext context = new ExecutionContext();
    ClientException exceptionToThrown = createRetryableException();
    String content = "";
    InputStream contentStream2 = new FileInputStream(file);
    try {
        content = StreamUtils.readContent(contentStream2, "utf-8");
    } finally {
        contentStream2.close();
    }
    // This request will succeed after 2 retries
    ServiceClientImpl client = new ServiceClientImpl(new ClientConfiguration(), 3, exceptionToThrown, 400, content);
    // Fix the max error retry count to 3
    try {
        client.sendRequest(request, context);
        fail("ClientException has not been thrown.");
    } catch (ClientException e) {
        assertEquals(exceptionToThrown, e);
        assertEquals(1, client.getRequestAttempts());
    }
}
Also used : ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) File(java.io.File) URI(java.net.URI) FileInputStream(java.io.FileInputStream) ClientConfiguration(com.aliyun.oss.ClientConfiguration) Test(org.junit.Test)

Example 40 with ClientException

use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.

the class ImageSample method main.

public static void main(String[] args) throws IOException {
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        // resize
        String style = "image/resize,m_fixed,w_100,h_100";
        GetObjectRequest request = new GetObjectRequest(bucketName, key);
        request.setProcess(style);
        ossClient.getObject(request, new File("example-resize.jpg"));
        // crop
        style = "image/crop,w_100,h_100,x_100,y_100,r_1";
        request = new GetObjectRequest(bucketName, key);
        request.setProcess(style);
        ossClient.getObject(request, new File("example-crop.jpg"));
        // rotate
        style = "image/rotate,90";
        request = new GetObjectRequest(bucketName, key);
        request.setProcess(style);
        ossClient.getObject(request, new File("example-rotate.jpg"));
        // sharpen
        style = "image/sharpen,100";
        request = new GetObjectRequest(bucketName, key);
        request.setProcess(style);
        ossClient.getObject(request, new File("example-sharpen.jpg"));
        // add watermark into the image
        style = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ";
        request = new GetObjectRequest(bucketName, key);
        request.setProcess(style);
        ossClient.getObject(request, new File("example-watermark.jpg"));
        // convert format
        style = "image/format,png";
        request = new GetObjectRequest(bucketName, key);
        request.setProcess(style);
        ossClient.getObject(request, new File("example-format.png"));
        // image information
        style = "image/info";
        request = new GetObjectRequest(bucketName, key);
        request.setProcess(style);
        ossClient.getObject(request, new File("example-info.txt"));
    } catch (OSSException oe) {
        System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
        System.out.println("Error Message: " + oe.getErrorCode());
        System.out.println("Error Code:       " + oe.getErrorCode());
        System.out.println("Request ID:      " + oe.getRequestId());
        System.out.println("Host ID:           " + oe.getHostId());
    } catch (ClientException ce) {
        System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
        System.out.println("Error Message: " + ce.getMessage());
    } catch (Throwable e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) File(java.io.File) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

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