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());
}
}
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());
}
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());
}
}
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());
}
}
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();
}
}
Aggregations