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