Example usage for com.amazonaws.handlers RequestHandler2 RequestHandler2

List of usage examples for com.amazonaws.handlers RequestHandler2 RequestHandler2

Introduction

In this page you can find the example usage for com.amazonaws.handlers RequestHandler2 RequestHandler2.

Prototype

RequestHandler2

Source Link

Usage

From source file:com.eucalyptus.simpleworkflow.common.client.Config.java

License:Open Source License

public static AmazonSimpleWorkflow buildClient(final Supplier<User> user, final String text)
        throws AuthException {
    final AWSCredentialsProvider credentialsProvider = new SecurityTokenAWSCredentialsProvider(user);
    final AmazonSimpleWorkflowClient client = new AmazonSimpleWorkflowClient(credentialsProvider,
            buildConfiguration(text));//from   w w  w.j  ava  2 s.c om
    client.setEndpoint(ServiceUris.remote(Topology.lookup(SimpleWorkflow.class)).toString());
    client.addRequestHandler(new RequestHandler2() {
        @Override
        public void beforeRequest(final Request<?> request) {
            // Add nonce to ensure unique request signature
            request.addHeader("Euca-Nonce", UUID.randomUUID().toString());
        }

        @Override
        public void afterResponse(final Request<?> request, final Response<?> response) {
        }

        @Override
        public void afterError(final Request<?> request, final Response<?> response, final Exception e) {
            final String errorMessage = Strings.nullToEmpty(e.getMessage());
            boolean resetEndpoint = false;
            if (Exceptions.isCausedBy(e, JSONException.class) && (errorMessage.contains("Response Code: 404")
                    || errorMessage.contains("Response Code: 503"))) {
                resetEndpoint = true;
            } else if (Exceptions.isCausedBy(e, ConnectException.class)) {
                resetEndpoint = true;
            } else if (Exceptions.isCausedBy(e, NoHttpResponseException.class)) {
                resetEndpoint = true;
            }
            if (resetEndpoint) {
                try {
                    client.setEndpoint(ServiceUris.remote(Topology.lookup(SimpleWorkflow.class)).toString());
                } catch (final Exception e2) {
                    // retry on next failure
                }
            }

            if (e instanceof AmazonServiceException) {
                final int status = ((AmazonServiceException) e).getStatusCode();
                if (status == 403) {
                    credentialsProvider.refresh();
                }
            }
        }
    });
    return client;
}