public ExecutionContext build() { return new ExecutionContext(this); }
public ExecutionContext build() { return new ExecutionContext(this); }
/** * @deprecated by {@link #createExecutionContext(AmazonWebServiceRequest)}. * This method exists only for backward compatiblity reason, so * that clients compiled against the older version of this class * won't get {@link NoSuchMethodError} at runtime. However, * calling this methods would effectively ignore and disable the * request metric collector, if any, specified at the request * level. Request metric collector specified at the service * client or AWS SDK level will still be honored. */ @Deprecated protected final ExecutionContext createExecutionContext() { final boolean isMetricsEnabled = isRMCEnabledAtClientOrSdkLevel() || isProfilingEnabled(); return new ExecutionContext(requestHandler2s, isMetricsEnabled, this); }
protected ExecutionContext createExecutionContext(final AmazonWebServiceRequest req) { final boolean isMetricsEnabled = isRequestMetricsEnabled(req) || isProfilingEnabled(); return new ExecutionContext(requestHandler2s, isMetricsEnabled, this); }
/** * Build a {@link HttpRequest} object for the given method. * * @param method method that annotated with {@link Operation} * @param args arguments of the method * @return a {@link HttpRequest} object */ HttpRequest createHttpRequest(Method method, Object[] args) { final Request<?> request = buildRequest(method, args); final ExecutionContext context = new ExecutionContext(); String userAgent = apiName; if (request.getHeaders().containsKey("User-Agent")) { // append it to execution context userAgent += " " + request.getHeaders().get("User-Agent"); } context.setContextUserAgent(userAgent); return requestFactory.createHttpRequest(request, clientConfiguration, context); }
@Before public void setup() { factory = new HttpRequestFactory(); request = new DefaultRequest<Object>("dummy"); request.setEndpoint(URI.create("https://s3.amazonaws.com")); clientConfiguration = new ClientConfiguration(); context = new ExecutionContext(); }
HttpRequest invokeExecuteMethod(Object[] args) { final ExecutionContext context = new ExecutionContext(); final Request<?> request = ((ApiRequest) args[0]).getRequest(); if (request.getEndpoint() == null) { request.setEndpoint(URI.create(endpoint)); } String userAgent = apiName; if (request.getHeaders().containsKey("User-Agent")) { // append it to execution context userAgent += " " + request.getHeaders().get("User-Agent"); } context.setContextUserAgent(userAgent); // add the api key if (apiKey != null) { request.addHeader("x-api-key", apiKey); } // sign the request if (provider != null && signer != null) { signer.sign(request, provider.getCredentials()); } return requestFactory.createHttpRequest(request, clientConfiguration, context); }
ExecutionContext ec = new ExecutionContext(handlers, false, null); client.requestHandler2s(testRequest, ec); assertEquals(calls.size(), 2);
/** * Verifies the request is actually retried for the expected times. */ private static void testActualRetries(int expectedRetryAttempts) { // The ExecutionContext should collect the expected RequestCount ExecutionContext context = new ExecutionContext(true); try { testedClient.execute(getSampleRequestWithRepeatableContent(originalRequest), null, errorResponseHandler, context); Assert.fail("AmazonServiceException is expected."); } catch (AmazonServiceException ase) { } // Check the RequestCount metric equals the expected value. Assert.assertEquals( expectedRetryAttempts + 1, // request count = retries + 1 context.getAwsRequestMetrics() .getTimingInfo() .getCounter(AWSRequestMetrics.Field.RequestCount.toString()).intValue()); } }
@Test public void testRetryIOExceptionFromExecute() throws IOException { IOException exception = new IOException("BOOM"); EasyMock .expect(httpClient.execute(EasyMock.<HttpRequest> anyObject())) .andThrow(exception) .times(4); EasyMock.replay(httpClient); ExecutionContext context = new ExecutionContext(); Request<?> request = new DefaultRequest<Object>("testsvc"); request.setEndpoint(java.net.URI.create( "http://testsvc.region.amazonaws.com")); request.addHeader(HttpHeader.CONTENT_LENGTH, "0"); request.setContent(new ByteArrayInputStream(new byte[0])); try { client.execute(request, null, null, context); Assert.fail("No exception when request repeatedly fails!"); } catch (AmazonClientException e) { Assert.assertSame(exception, e.getCause()); } // Verify that we called execute 4 times. EasyMock.verify(httpClient); }
ExecutionContext context = new ExecutionContext();
ExecutionContext context = new ExecutionContext(true);
ExecutionContext context = new ExecutionContext(true);
ExecutionContext context = new ExecutionContext(true);
ExecutionContext context = new ExecutionContext(true);
@Test(expected = RuntimeException.class) public void testHandleResponseWithNullResult() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); return null; } @Override public boolean needsConnectionLeftOpen() { return false; } }; client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext()); }
@Test(expected = Exception.class) public void testHandleResponseThrowsGenericException() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new Exception("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext()); }
@Test(expected = CRC32MismatchException.class) public void testHandleResponseThrowsCRC32MisMatch() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new CRC32MismatchException("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext()); }
@Test(expected = IOException.class) public void testHandleResponseThrowsIOException() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new IOException("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext()); }
@Test public void testHandleResponse() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); AmazonWebServiceResponse<String> awsResponse = new AmazonWebServiceResponse<String>(); awsResponse.setResult("Result"); return awsResponse; } @Override public boolean needsConnectionLeftOpen() { return false; } }; assertEquals("Result", client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext())); }