Search in sources :

Example 1 with RequestHandler2

use of com.amazonaws.handlers.RequestHandler2 in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBDynamicFaultInjection method init.

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.PropertiesCredentials
 * @see com.amazonaws.ClientConfiguration
 */
private static void init() throws Exception {
    dynamoDBClient = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain());
    // pass in the client for access to the cached metadata.
    RequestHandler2 requestHandler = new FaultInjectionRequestHandler(dynamoDBClient);
    dynamoDBClient.addRequestHandler(requestHandler);
}
Also used : DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) RequestHandler2(com.amazonaws.handlers.RequestHandler2) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient)

Example 2 with RequestHandler2

use of com.amazonaws.handlers.RequestHandler2 in project aws-sdk-android by aws-amplify.

the class AmazonHttpClientTest method testRequestHander2s.

@Test
public void testRequestHander2s() {
    final Request<?> testRequest = new DefaultRequest<String>("test");
    final List<Object> calls = new ArrayList<Object>();
    List<RequestHandler2> handlers = new ArrayList<RequestHandler2>();
    RequestHandler2 defaultHandler = new RequestHandler2() {

        @Override
        public void beforeRequest(Request<?> request) {
            assertSame(testRequest, request);
            calls.add(this);
        }

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

        @Override
        public void afterError(Request<?> request, Response<?> response, Exception e) {
        }
    };
    RequestHandler2 credentialHandler = new CredentialsRequestHandler() {

        @Override
        public void beforeRequest(Request<?> request) {
            assertSame(testRequest, request);
            calls.add(this);
        }

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

        @Override
        public void afterError(Request<?> request, Response<?> response, Exception e) {
        }
    };
    handlers.add(defaultHandler);
    handlers.add(credentialHandler);
    ExecutionContext ec = new ExecutionContext(handlers, false, null);
    client.requestHandler2s(testRequest, ec);
    assertEquals(calls.size(), 2);
    assertTrue(calls.contains(defaultHandler));
    assertTrue(calls.contains(credentialHandler));
}
Also used : AmazonWebServiceResponse(com.amazonaws.AmazonWebServiceResponse) Response(com.amazonaws.Response) DefaultRequest(com.amazonaws.DefaultRequest) RequestHandler2(com.amazonaws.handlers.RequestHandler2) ArrayList(java.util.ArrayList) Request(com.amazonaws.Request) DefaultRequest(com.amazonaws.DefaultRequest) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) URISyntaxException(java.net.URISyntaxException) CRC32MismatchException(com.amazonaws.internal.CRC32MismatchException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException) CredentialsRequestHandler(com.amazonaws.handlers.CredentialsRequestHandler) Test(org.junit.Test)

Example 3 with RequestHandler2

use of com.amazonaws.handlers.RequestHandler2 in project aws-sdk-android by aws-amplify.

the class AmazonHttpClientTest method testAfterError.

@Test
public void testAfterError() {
    final Request<?> testRequest = new DefaultRequest<String>("test");
    final Response<?> testResponse = new Response<String>("test", new HttpResponse.Builder().build());
    final List<Object> calls = new ArrayList<Object>();
    List<RequestHandler2> handlers = new ArrayList<RequestHandler2>();
    final AmazonClientException ace = new AmazonClientException("Test exception");
    RequestHandler2 handler = new RequestHandler2() {

        @Override
        public void beforeRequest(Request<?> request) {
        }

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

        @Override
        public void afterError(Request<?> request, Response<?> response, Exception e) {
            assertSame(testRequest, request);
            assertSame(testResponse, response);
            assertSame(e, ace);
            calls.add(this);
        }
    };
    handlers.add(handler);
    client.afterError(testRequest, testResponse, handlers, ace);
    assertEquals(calls.size(), 1);
    assertTrue(calls.contains(handler));
}
Also used : DefaultRequest(com.amazonaws.DefaultRequest) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Request(com.amazonaws.Request) DefaultRequest(com.amazonaws.DefaultRequest) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) URISyntaxException(java.net.URISyntaxException) CRC32MismatchException(com.amazonaws.internal.CRC32MismatchException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonWebServiceResponse(com.amazonaws.AmazonWebServiceResponse) Response(com.amazonaws.Response) RequestHandler2(com.amazonaws.handlers.RequestHandler2) Test(org.junit.Test)

Example 4 with RequestHandler2

use of com.amazonaws.handlers.RequestHandler2 in project aws-sdk-android by aws-amplify.

the class AmazonHttpClientTest method testAfterResponse.

@Test
public void testAfterResponse() {
    final Request<?> testRequest = new DefaultRequest<String>("test");
    final Response<?> testResponse = new Response<String>("test", new HttpResponse.Builder().build());
    final List<Object> calls = new ArrayList<Object>();
    List<RequestHandler2> handlers = new ArrayList<RequestHandler2>();
    RequestHandler2 handler = new RequestHandler2() {

        @Override
        public void beforeRequest(Request<?> request) {
        }

        @Override
        public void afterResponse(Request<?> request, Response<?> response) {
            assertSame(testRequest, request);
            assertSame(testResponse, response);
            calls.add(this);
        }

        @Override
        public void afterError(Request<?> request, Response<?> response, Exception e) {
        }
    };
    handlers.add(handler);
    client.afterResponse(testRequest, handlers, testResponse, null);
    assertEquals(calls.size(), 1);
    assertTrue(calls.contains(handler));
}
Also used : AmazonWebServiceResponse(com.amazonaws.AmazonWebServiceResponse) Response(com.amazonaws.Response) DefaultRequest(com.amazonaws.DefaultRequest) RequestHandler2(com.amazonaws.handlers.RequestHandler2) ArrayList(java.util.ArrayList) Request(com.amazonaws.Request) DefaultRequest(com.amazonaws.DefaultRequest) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) URISyntaxException(java.net.URISyntaxException) CRC32MismatchException(com.amazonaws.internal.CRC32MismatchException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException) Test(org.junit.Test)

Example 5 with RequestHandler2

use of com.amazonaws.handlers.RequestHandler2 in project aws-sdk-android by aws-amplify.

the class AmazonHttpClient method execute.

/**
 * Executes the request and returns the result.
 *
 * @param request The AmazonWebServices request to send to the remote server
 * @param responseHandler A response handler to accept a successful response
 *            from the remote server
 * @param errorResponseHandler A response handler to accept an unsuccessful
 *            response from the remote server
 * @param executionContext Additional information about the context of this
 *            web service call
 * @param <T> the response type.
 * @return the result of the request.
 */
public <T> Response<T> execute(Request<?> request, HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler, HttpResponseHandler<AmazonServiceException> errorResponseHandler, ExecutionContext executionContext) {
    // Prepend host prefix if specified in the request.
    if (request.getHostPrefix() != null) {
        try {
            URI endpoint = request.getEndpoint();
            String host = request.getHostPrefix() + endpoint.getHost();
            request.setEndpoint(URIBuilder.builder(endpoint).host(host).build());
        } catch (URISyntaxException error) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to prepend host prefix: " + error.getMessage(), error);
            }
        }
    }
    if (executionContext == null) {
        throw new AmazonClientException("Internal SDK Error: No execution context parameter specified.");
    }
    final List<RequestHandler2> requestHandler2s = requestHandler2s(request, executionContext);
    final AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
    Response<T> response = null;
    try {
        response = executeHelper(request, responseHandler, errorResponseHandler, executionContext);
        final TimingInfo timingInfo = awsRequestMetrics.getTimingInfo().endTiming();
        afterResponse(request, requestHandler2s, response, timingInfo);
        return response;
    } catch (final AmazonClientException e) {
        afterError(request, response, requestHandler2s, e);
        throw e;
    }
}
Also used : RequestHandler2(com.amazonaws.handlers.RequestHandler2) TimingInfo(com.amazonaws.util.TimingInfo) AWSRequestMetrics(com.amazonaws.util.AWSRequestMetrics) AmazonClientException(com.amazonaws.AmazonClientException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

RequestHandler2 (com.amazonaws.handlers.RequestHandler2)6 URISyntaxException (java.net.URISyntaxException)5 AmazonClientException (com.amazonaws.AmazonClientException)4 Test (org.junit.Test)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)3 AmazonWebServiceResponse (com.amazonaws.AmazonWebServiceResponse)3 DefaultRequest (com.amazonaws.DefaultRequest)3 Request (com.amazonaws.Request)3 Response (com.amazonaws.Response)3 CRC32MismatchException (com.amazonaws.internal.CRC32MismatchException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)1 CredentialsRequestHandler (com.amazonaws.handlers.CredentialsRequestHandler)1 AmazonDynamoDBClient (com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient)1 AWSRequestMetrics (com.amazonaws.util.AWSRequestMetrics)1 TimingInfo (com.amazonaws.util.TimingInfo)1 URI (java.net.URI)1