Search in sources :

Example 1 with ExecutionContext

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;
    }
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) RequestChecksumHanlder(com.aliyun.oss.common.comm.RequestChecksumHanlder) OSSException(com.aliyun.oss.OSSException) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) RequestProgressHanlder(com.aliyun.oss.common.comm.RequestProgressHanlder) ResponseProgressHandler(com.aliyun.oss.common.comm.ResponseProgressHandler) ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) WebServiceRequest(com.aliyun.oss.model.WebServiceRequest) RequestHandler(com.aliyun.oss.common.comm.RequestHandler) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ResponseChecksumHandler(com.aliyun.oss.common.comm.ResponseChecksumHandler) RequestSigner(com.aliyun.oss.common.auth.RequestSigner)

Example 2 with ExecutionContext

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

Example 3 with ExecutionContext

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());
    }
}
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 4 with ExecutionContext

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());
}
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 5 with ExecutionContext

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

Aggregations

ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)6 ClientConfiguration (com.aliyun.oss.ClientConfiguration)4 ClientException (com.aliyun.oss.ClientException)4 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 URI (java.net.URI)4 Test (org.junit.Test)3 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)2 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)2 OSSException (com.aliyun.oss.OSSException)1 ServiceException (com.aliyun.oss.ServiceException)1 RequestSigner (com.aliyun.oss.common.auth.RequestSigner)1 RequestChecksumHanlder (com.aliyun.oss.common.comm.RequestChecksumHanlder)1 RequestHandler (com.aliyun.oss.common.comm.RequestHandler)1 RequestProgressHanlder (com.aliyun.oss.common.comm.RequestProgressHanlder)1 ResponseChecksumHandler (com.aliyun.oss.common.comm.ResponseChecksumHandler)1 ResponseProgressHandler (com.aliyun.oss.common.comm.ResponseProgressHandler)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 WebServiceRequest (com.aliyun.oss.model.WebServiceRequest)1 File (java.io.File)1