use of com.aliyun.oss.common.comm.ExecutionContext in project aliyun-oss-java-sdk by aliyun.
the class OSSOperation method doOperation.
protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, String bucketName, String key, boolean keepResponseOpen, List<RequestHandler> requestHandlers, List<ResponseHandler> reponseHandlers) throws OSSException, ClientException {
final WebServiceRequest originalRequest = request.getOriginalRequest();
request.getHeaders().putAll(client.getClientConfiguration().getDefaultHeaders());
request.getHeaders().putAll(originalRequest.getHeaders());
request.getParameters().putAll(originalRequest.getParameters());
ExecutionContext context = createDefaultContext(request.getMethod(), bucketName, key);
if (context.getCredentials().useSecurityToken() && !request.isUseUrlSignature()) {
request.addHeader(OSSHeaders.OSS_SECURITY_TOKEN, context.getCredentials().getSecurityToken());
}
context.addRequestHandler(new RequestProgressHanlder());
if (requestHandlers != null) {
for (RequestHandler handler : requestHandlers) context.addRequestHandler(handler);
}
if (client.getClientConfiguration().isCrcCheckEnabled()) {
context.addRequestHandler(new RequestChecksumHanlder());
}
context.addResponseHandler(new ResponseProgressHandler(originalRequest));
if (reponseHandlers != null) {
for (ResponseHandler handler : reponseHandlers) context.addResponseHandler(handler);
}
if (client.getClientConfiguration().isCrcCheckEnabled()) {
context.addResponseHandler(new ResponseChecksumHandler());
}
List<RequestSigner> signerHandlers = this.client.getClientConfiguration().getSignerHandlers();
if (signerHandlers != null) {
for (RequestSigner signer : signerHandlers) {
context.addSignerHandler(signer);
}
}
ResponseMessage response = send(request, context, keepResponseOpen);
try {
return parser.parse(response);
} catch (ResponseParseException rpe) {
OSSException oe = ExceptionFactory.createInvalidResponseException(response.getRequestId(), rpe.getMessage(), rpe);
logException("Unable to parse response error: ", rpe);
throw oe;
}
}
use of com.aliyun.oss.common.comm.ExecutionContext in project aliyun-oss-java-sdk by aliyun.
the class OSSOperation method createDefaultContext.
protected ExecutionContext createDefaultContext(HttpMethod method, String bucketName, String key) {
ExecutionContext context = new ExecutionContext();
context.setCharset(DEFAULT_CHARSET_NAME);
context.setSigner(createSigner(method, bucketName, key, credsProvider.getCredentials()));
context.addResponseHandler(errorResponseHandler);
if (method == HttpMethod.POST) {
context.setRetryStrategy(noRetryStrategy);
}
context.setCredentials(credsProvider.getCredentials());
return context;
}
use of com.aliyun.oss.common.comm.ExecutionContext 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.common.comm.ExecutionContext 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.common.comm.ExecutionContext 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());
}
}
Aggregations