software.amazon.awssdk.http.SdkHttpResponse Java Examples

The following examples show how to use software.amazon.awssdk.http.SdkHttpResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void afterUnmarshalling_putObjectRequest_shouldValidateChecksum() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();

    PutObjectResponse response = PutObjectResponse.builder()
                                                  .eTag(VALID_CHECKSUM)
                                                  .build();

    PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                                                        .build();

    SdkHttpRequest sdkHttpRequest = SdkHttpFullRequest.builder()
                                                      .uri(URI.create("http://localhost:8080"))
                                                      .method(SdkHttpMethod.PUT)
                                                      .build();

    Context.AfterUnmarshalling afterUnmarshallingContext =
        InterceptorTestUtils.afterUnmarshallingContext(putObjectRequest, sdkHttpRequest, response, sdkHttpResponse);

    interceptor.afterUnmarshalling(afterUnmarshallingContext, getExecutionAttributesWithChecksum());
}
 
Example #2
Source File: ExecutionInterceptorChain.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
public InterceptorContext modifyHttpResponse(InterceptorContext context,
                                             ExecutionAttributes executionAttributes) {
    InterceptorContext result = context;

    for (int i = interceptors.size() - 1; i >= 0; i--) {
        SdkHttpResponse interceptorResult =
            interceptors.get(i).modifyHttpResponse(result, executionAttributes);
        validateInterceptorResult(result.httpResponse(), interceptorResult, interceptors.get(i), "modifyHttpResponse");

        InputStream response = interceptors.get(i).modifyHttpResponseContent(result, executionAttributes).orElse(null);

        result = result.toBuilder().httpResponse(interceptorResult).responseBody(response).build();
    }

    return result;
}
 
Example #3
Source File: SyncClientHandlerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void successfulExecutionCallsResponseHandler() throws Exception {

    SdkResponse expected = VoidSdkResponse.builder().build();
    Map<String, List<String>> headers = new HashMap<>();
    headers.put("foo", Arrays.asList("bar"));

    // Given
    expectRetrievalFromMocks();
    when(httpClientCall.call()).thenReturn(HttpExecuteResponse.builder()
                                                              .response(SdkHttpResponse.builder()
                                                                                   .statusCode(200)
                                                                                   .headers(headers)
                                                                                   .build())
                                                              .build()); // Successful HTTP call
    when(responseHandler.handle(any(), any())).thenReturn(expected); // Response handler call

    // When
    SdkResponse actual = syncClientHandler.execute(clientExecutionParams());

    // Then
    verifyNoMoreInteractions(errorResponseHandler); // No error handler calls
    assertThat(actual.sdkHttpResponse().statusCode()).isEqualTo(200);
    assertThat(actual.sdkHttpResponse().headers()).isEqualTo(headers);
}
 
Example #4
Source File: TracingInterceptor.java    From aws-xray-sdk-java with Apache License 2.0 6 votes vote down vote up
private HashMap<String, Object> extractHttpResponseParameters(SdkHttpResponse httpResponse) {
    HashMap<String, Object> parameters = new HashMap<>();
    HashMap<String, Object> responseData = new HashMap<>();

    responseData.put(EntityDataKeys.HTTP.STATUS_CODE_KEY, httpResponse.statusCode());

    try {
        if (httpResponse.headers().containsKey(EntityHeaderKeys.HTTP.CONTENT_LENGTH_HEADER)) {
            responseData.put(EntityDataKeys.HTTP.CONTENT_LENGTH_KEY, Long.parseLong(
                    httpResponse.headers().get(EntityHeaderKeys.HTTP.CONTENT_LENGTH_HEADER).get(0))
            );
        }
    } catch (NumberFormatException e) {
        logger.warn("Unable to parse Content-Length header.", e);
    }

    parameters.put(EntityDataKeys.HTTP.RESPONSE_KEY, responseData);
    return parameters;
}
 
Example #5
Source File: AwsServiceException.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isClockSkewException() {
    if (super.isClockSkewException()) {
        return true;
    }

    if (awsErrorDetails == null) {
        return false;
    }

    if (AwsErrorCode.isDefiniteClockSkewErrorCode(awsErrorDetails.errorCode())) {
        return true;
    }

    SdkHttpResponse sdkHttpResponse = awsErrorDetails.sdkHttpResponse();

    if (clockSkew == null || sdkHttpResponse == null) {
        return false;
    }

    boolean isPossibleClockSkewError = AwsErrorCode.isPossibleClockSkewErrorCode(awsErrorDetails.errorCode()) ||
                                       AwsStatusCode.isPossibleClockSkewStatusCode(statusCode());

    return isPossibleClockSkewError && ClockSkew.isClockSkewed(Instant.now().minus(clockSkew),
                                                               ClockSkew.getServerTime(sdkHttpResponse).orElse(null));
}
 
Example #6
Source File: SpectatorExecutionInterceptorTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
private void parseRetryHeaderTest(String expected, String header) {
  SdkHttpRequest request = SdkHttpRequest.builder()
      .method(SdkHttpMethod.POST)
      .uri(URI.create("https://ec2.us-east-1.amazonaws.com"))
      .appendHeader("amz-sdk-retry", header)
      .build();
  SdkHttpResponse response = SdkHttpResponse.builder()
      .statusCode(200)
      .build();
  TestContext context = new TestContext(request, response);
  execute(context, createAttributes("EC2", "DescribeInstances"), millis(30));
  Assertions.assertEquals(1, registry.timers().count());

  Timer t = registry.timers().findFirst().orElse(null);
  Assertions.assertNotNull(t);
  Assertions.assertEquals(1, t.count());
  Assertions.assertEquals(millis(30), t.totalTime());
  Assertions.assertEquals(expected, get(t.id(), "ipc.attempt"));
}
 
Example #7
Source File: GetObjectInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * S3 currently returns content-range in two possible headers: Content-Range or x-amz-content-range based on the x-amz-te
 * in the request. This will check the x-amz-content-range if the modeled header (Content-Range) wasn't populated.
 */
private SdkResponse fixContentRange(SdkResponse sdkResponse, SdkHttpResponse httpResponse) {
    // Use the modeled content range header, if the service returned it.
    GetObjectResponse getObjectResponse = (GetObjectResponse) sdkResponse;
    if (getObjectResponse.contentRange() != null) {
        return getObjectResponse;
    }

    // If the service didn't use the modeled content range header, check the x-amz-content-range header.
    Optional<String> xAmzContentRange = httpResponse.firstMatchingHeader("x-amz-content-range");
    if (!xAmzContentRange.isPresent()) {
        return getObjectResponse;
    }

    return getObjectResponse.copy(r -> r.contentRange(xAmzContentRange.get()));
}
 
Example #8
Source File: S3MockUtils.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * @return A mocked result for the ListObjects operation.
 */
public static HttpExecuteResponse mockListObjectsResponse() throws UnsupportedEncodingException {
    return HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(200).build()).responseBody(
        AbortableInputStream.create(new StringInputStream(
            "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\n" +
            "  <Name>example-bucket</Name>\n" +
            "  <Prefix>photos/2006/</Prefix>\n" +
            "  <Marker></Marker>\n" +
            "  <MaxKeys>1000</MaxKeys>\n" +
            "  <Delimiter>/</Delimiter>\n" +
            "  <IsTruncated>false</IsTruncated>\n" +
            "\n" +
            "  <CommonPrefixes>\n" +
            "    <Prefix>photos/2006/February/</Prefix>\n" +
            "  </CommonPrefixes>\n" +
            "  <CommonPrefixes>\n" +
            "    <Prefix>photos/2006/January/</Prefix>\n" +
            "  </CommonPrefixes>\n" +
            "</ListBucketResult>")))
                              .build();
}
 
Example #9
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void afterUnmarshalling_putObjectRequest_with_SSE_shouldNotValidateChecksum() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();

    PutObjectResponse response = PutObjectResponse.builder()
                                                  .eTag(INVALID_CHECKSUM)
                                                  .build();

    PutObjectRequest putObjectRequest = PutObjectRequest.builder().build();

    SdkHttpRequest sdkHttpRequest = SdkHttpFullRequest.builder()
                                                      .putHeader(SERVER_SIDE_ENCRYPTION_HEADER, AWS_KMS.toString())
                                                      .putHeader("x-amz-server-side-encryption-aws-kms-key-id", ENABLE_MD5_CHECKSUM_HEADER_VALUE)
                                                      .uri(URI.create("http://localhost:8080"))
                                                      .method(SdkHttpMethod.PUT)
                                                      .build();

    Context.AfterUnmarshalling afterUnmarshallingContext =
        InterceptorTestUtils.afterUnmarshallingContext(putObjectRequest, sdkHttpRequest, response, sdkHttpResponse);

    interceptor.afterUnmarshalling(afterUnmarshallingContext, getExecutionAttributesWithChecksum());
}
 
Example #10
Source File: AsyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void afterUnmarshalling_putObjectRequest_with_SSE_shouldNotValidateChecksum() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();

    PutObjectResponse response = PutObjectResponse.builder()
                                                  .eTag(INVALID_CHECKSUM)
                                                  .build();

    PutObjectRequest putObjectRequest = PutObjectRequest.builder().build();

    SdkHttpRequest sdkHttpRequest = SdkHttpFullRequest.builder()
                                                      .putHeader(SERVER_SIDE_ENCRYPTION_HEADER, AWS_KMS.toString())
                                                      .putHeader("x-amz-server-side-encryption-aws-kms-key-id", ENABLE_MD5_CHECKSUM_HEADER_VALUE)
                                                      .uri(URI.create("http://localhost:8080"))
                                                      .method(SdkHttpMethod.PUT)
                                                      .build();

    Context.AfterUnmarshalling afterUnmarshallingContext =
        InterceptorTestUtils.afterUnmarshallingContext(putObjectRequest, sdkHttpRequest, response, sdkHttpResponse);

    interceptor.afterUnmarshalling(afterUnmarshallingContext, getExecutionAttributesWithChecksum());
}
 
Example #11
Source File: AwsServiceExceptionTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private AwsServiceException exception(int clientSideTimeOffset, String errorCode, int statusCode, String serverDate) {
    SdkHttpResponse httpResponse =
            SdkHttpFullResponse.builder()
                               .statusCode(statusCode)
                               .applyMutation(r -> {
                                   if (serverDate != null) {
                                       r.putHeader("Date", serverDate);
                                   }
                               })
                               .build();
    AwsErrorDetails errorDetails =
            AwsErrorDetails.builder()
                           .errorCode(errorCode)
                           .sdkHttpResponse(httpResponse)
                           .build();

    return AwsServiceException.builder()
                              .clockSkew(Duration.ofSeconds(clientSideTimeOffset))
                              .awsErrorDetails(errorDetails)
                              .statusCode(statusCode)
                              .build();
}
 
Example #12
Source File: SpectatorExecutionInterceptorTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void awsFailure() {
  SdkHttpRequest request = SdkHttpRequest.builder()
      .method(SdkHttpMethod.POST)
      .uri(URI.create("https://ec2.us-east-1.amazonaws.com"))
      .build();
  SdkHttpResponse response = SdkHttpResponse.builder()
      .statusCode(403)
      .build();
  Throwable error = AwsServiceException.builder()
      .awsErrorDetails(AwsErrorDetails.builder()
          .errorCode("AccessDenied")
          .errorMessage("credentials have expired")
          .build())
      .build();
  TestContext context = new TestContext(request, response, error);
  execute(context, createAttributes("EC2", "DescribeInstances"), millis(30));
  Assertions.assertEquals(1, registry.timers().count());

  Timer t = registry.timers().findFirst().orElse(null);
  Assertions.assertNotNull(t);
  Assertions.assertEquals(1, t.count());
  Assertions.assertEquals(millis(30), t.totalTime());
  Assertions.assertEquals("403", get(t.id(), "http.status"));
  Assertions.assertEquals("AccessDenied", get(t.id(), "ipc.status.detail"));
}
 
Example #13
Source File: SnsClientMockErrors.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public GetTopicAttributesResponse getTopicAttributes(
    GetTopicAttributesRequest topicAttributesRequest) {
  GetTopicAttributesResponse response = Mockito.mock(GetTopicAttributesResponse.class);
  SdkHttpResponse metadata = Mockito.mock(SdkHttpResponse.class);

  Mockito.when(metadata.statusCode()).thenReturn(200);
  Mockito.when(response.sdkHttpResponse()).thenReturn(metadata);

  return response;
}
 
Example #14
Source File: AsyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void afterUnmarshalling_putObjectRequest_shouldValidateChecksum_throwExceptionIfInvalid() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();

    PutObjectResponse response = PutObjectResponse.builder()
                                                  .eTag(INVALID_CHECKSUM)
                                                  .build();

    PutObjectRequest putObjectRequest = PutObjectRequest.builder().build();

    SdkHttpRequest sdkHttpRequest = SdkHttpFullRequest.builder()
                                                      .uri(URI.create("http://localhost:8080"))
                                                      .method(SdkHttpMethod.PUT)
                                                      .build();

    Context.AfterUnmarshalling afterUnmarshallingContext =
        InterceptorContext.builder()
                          .request(putObjectRequest)
                          .httpRequest(sdkHttpRequest)
                          .response(response)
                          .httpResponse(sdkHttpResponse)
                          .asyncRequestBody(AsyncRequestBody.fromString("Test"))
                          .build();

    ExecutionAttributes attributes = getExecutionAttributesWithChecksum();
    interceptor.modifyAsyncHttpContent(afterUnmarshallingContext, attributes);
    assertThatThrownBy(() -> interceptor.afterUnmarshalling(afterUnmarshallingContext, attributes))
        .hasMessageContaining("Data read has a different checksum than expected.");
}
 
Example #15
Source File: GetBucketPolicyInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getBucketPolicy_shouldModifyResponseContent() {
    GetBucketPolicyRequest request = GetBucketPolicyRequest.builder().build();
    Context.ModifyHttpResponse context = modifyHttpResponseContent(request, SdkHttpResponse.builder()
                                                                                           .statusCode(200)
                                                                                           .build());
    Optional<InputStream> inputStream = interceptor.modifyHttpResponseContent(context, new ExecutionAttributes());
    assertThat(inputStream).isNotEqualTo(context.responseBody());
}
 
Example #16
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyHttpResponseContent_getObjectRequest_checksumEnabled_shouldWrapChecksumValidatingPublisher() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(GetObjectRequest.builder().build(), sdkHttpResponse);
    Optional<InputStream> publisher = interceptor.modifyHttpResponseContent(modifyHttpResponse,
                                                                            getExecutionAttributes());
    assertThat(publisher.get()).isExactlyInstanceOf(ChecksumValidatingInputStream.class);
}
 
Example #17
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyHttpResponseContent_getObjectRequest_responseDoesNotContainChecksum_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributesWithChecksumDisabled();
    SdkHttpResponse sdkHttpResponse = SdkHttpResponse.builder()
                                                     .putHeader(CONTENT_LENGTH_HEADER, "100")
                                                     .build();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(GetObjectRequest.builder().build(), sdkHttpResponse);
    Optional<InputStream> publisher = interceptor.modifyHttpResponseContent(modifyHttpResponse,
                                                                            executionAttributes);
    assertThat(publisher).isEqualTo(modifyHttpResponse.responseBody());
}
 
Example #18
Source File: ChecksumsEnabledValidatorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void responseChecksumIsValid_serverSideEncryption_false() {
    SdkHttpResponse response = SdkHttpResponse.builder()
                                              .putHeader(SERVER_SIDE_ENCRYPTION_HEADER, AWS_KMS.toString())
                                              .build();

    assertThat(responseChecksumIsValid(response)).isFalse();
}
 
Example #19
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyHttpResponseContent_nonGetObjectRequest_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributes();
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
    sdkHttpResponse.toBuilder().clearHeaders();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(PutObjectRequest.builder().build(), sdkHttpResponse);
    Optional<InputStream> publisher = interceptor.modifyHttpResponseContent(modifyHttpResponse,
                                                                            executionAttributes);
    assertThat(publisher).isEqualTo(modifyHttpResponse.responseBody());

}
 
Example #20
Source File: AmazonWebServicesClientProxyTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void badRequest() {
    final AmazonWebServicesClientProxy proxy = new AmazonWebServicesClientProxy(mock(LoggerProxy.class), MOCK,
                                                                                () -> Duration.ofMinutes(2).toMillis() // just
                                                                                                                       // keep
                                                                                                                       // going

    );
    final Model model = new Model();
    model.setRepoName("NewRepo");
    final StdCallbackContext context = new StdCallbackContext();
    final SdkHttpResponse sdkHttpResponse = mock(SdkHttpResponse.class);
    when(sdkHttpResponse.statusCode()).thenReturn(400);
    final ProgressEvent<Model,
        StdCallbackContext> result = proxy
            .initiate("client:createRespository", proxy.newProxy(() -> mock(ServiceClient.class)), model, context)
            .translateToServiceRequest(m -> new CreateRequest.Builder().repoName(m.getRepoName()).build())
            .makeServiceCall((r, c) -> {
                throw new BadRequestException(mock(AwsServiceException.Builder.class)) {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public AwsErrorDetails awsErrorDetails() {
                        return AwsErrorDetails.builder().errorCode("BadRequest").errorMessage("Bad Parameter in request")
                            .sdkHttpResponse(sdkHttpResponse).build();
                    }
                };
            }).done(o -> ProgressEvent.success(model, context));
    assertThat(result.getStatus()).isEqualTo(OperationStatus.FAILED);
    assertThat(result.getMessage()).contains("BadRequest");
}
 
Example #21
Source File: GetBucketPolicyInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void errorResponseShouldNotModifyResponse() {
    GetBucketPolicyRequest request = GetBucketPolicyRequest.builder().build();
    Context.ModifyHttpResponse context = modifyHttpResponseContent(request, SdkHttpResponse.builder().statusCode(404).build());
    Optional<InputStream> inputStream = interceptor.modifyHttpResponseContent(context, new ExecutionAttributes());
    assertThat(inputStream).isEqualTo(context.responseBody());
}
 
Example #22
Source File: AsyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyAsyncHttpResponseContent_nonGetObjectRequest_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributes();
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();
    sdkHttpResponse.toBuilder().clearHeaders();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(PutObjectRequest.builder().build(), sdkHttpResponse);
    Optional<Publisher<ByteBuffer>> publisher = interceptor.modifyAsyncHttpResponseContent(modifyHttpResponse,
                                                                                           executionAttributes);
    assertThat(publisher).isEqualTo(modifyHttpResponse.responsePublisher());

}
 
Example #23
Source File: AsyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyAsyncHttpResponseContent_getObjectRequest_responseDoesNotContainChecksum_shouldNotModify() {
    ExecutionAttributes executionAttributes = getExecutionAttributesWithChecksumDisabled();
    SdkHttpResponse sdkHttpResponse = SdkHttpResponse.builder()
                                                     .putHeader(CONTENT_LENGTH_HEADER, "100")
                                                     .build();
    Context.ModifyHttpResponse modifyHttpResponse =
        InterceptorTestUtils.modifyHttpResponse(GetObjectRequest.builder().build(), sdkHttpResponse);
    Optional<Publisher<ByteBuffer>> publisher = interceptor.modifyAsyncHttpResponseContent(modifyHttpResponse,
                                                                                           executionAttributes);
    assertThat(publisher).isEqualTo(modifyHttpResponse.responsePublisher());
}
 
Example #24
Source File: SyncChecksumValidationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void afterUnmarshalling_putObjectRequest_shouldValidateChecksum_throwExceptionIfInvalid() {
    SdkHttpResponse sdkHttpResponse = getSdkHttpResponseWithChecksumHeader();

    PutObjectResponse response = PutObjectResponse.builder()
                                                  .eTag(INVALID_CHECKSUM)
                                                  .build();

    PutObjectRequest putObjectRequest = PutObjectRequest.builder().build();

    SdkHttpRequest sdkHttpRequest = SdkHttpFullRequest.builder()
                                                      .uri(URI.create("http://localhost:8080"))
                                                      .method(SdkHttpMethod.PUT)
                                                      .contentStreamProvider(() -> new StringInputStream("Test"))
                                                      .build();

    Context.AfterUnmarshalling afterUnmarshallingContext =
        InterceptorContext.builder()
                          .request(putObjectRequest)
                          .httpRequest(sdkHttpRequest)
                          .response(response)
                          .httpResponse(sdkHttpResponse)
                          .requestBody(RequestBody.fromString("Test"))
                          .build();

    ExecutionAttributes attributes = getExecutionAttributesWithChecksum();
    interceptor.modifyHttpContent(afterUnmarshallingContext, attributes);
    assertThatThrownBy(() -> interceptor.afterUnmarshalling(afterUnmarshallingContext, attributes))
        .hasMessageContaining("Data read has a different checksum than expected.");
}
 
Example #25
Source File: ClockSkewAdjuster.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the recommended clock adjustment that should be used for future requests (in seconds). The result has the same
 * semantics of {@link HttpClientDependencies#timeOffset()}. Positive values imply the client clock is "fast" and negative
 * values imply the client clock is "slow".
 */
public Integer getAdjustmentInSeconds(SdkHttpResponse response) {
    Instant now = Instant.now();
    Instant serverTime = ClockSkew.getServerTime(response).orElse(null);
    Duration skew = ClockSkew.getClockSkew(now, serverTime);
    try {
        return Math.toIntExact(skew.getSeconds());
    } catch (ArithmeticException e) {
        log.warn(() -> "The clock skew between the client and server was too large to be compensated for (" + now +
                       " versus " + serverTime + ").");
        return 0;
    }
}
 
Example #26
Source File: SyncClientHandlerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void mockSuccessfulApiCall() throws Exception {
    expectRetrievalFromMocks();
    when(httpClientCall.call()).thenReturn(HttpExecuteResponse.builder()
                                                              .responseBody(AbortableInputStream.create(new ByteArrayInputStream("TEST".getBytes())))
                                                              .response(SdkHttpResponse.builder().statusCode(200).build())
                                                              .build());
    when(responseHandler.handle(any(), any())).thenReturn(VoidSdkResponse.builder().build());
}
 
Example #27
Source File: SpectatorExecutionInterceptor.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override
public void afterTransmission(Context.AfterTransmission context, ExecutionAttributes attrs) {
  SdkHttpResponse response = context.httpResponse();
  IpcLogEntry logEntry = attrs.getAttribute(LOG_ENTRY)
      .markEnd()
      .withHttpStatus(response.statusCode());
  attrs.putAttribute(STATUS_IS_SET, true);

  response.headers().forEach((k, vs) -> vs.forEach(v -> logEntry.addResponseHeader(k, v)));
}
 
Example #28
Source File: MockHttpClient.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private HttpExecuteResponse successResponse() {

        AbortableInputStream inputStream =
            AbortableInputStream.create(new ByteArrayInputStream(successResponseContent.getBytes()));

        return HttpExecuteResponse.builder()
                                  .response(SdkHttpResponse.builder()
                                                           .statusCode(200)
                                                           .build())
                                  .responseBody(inputStream)
                                  .build();
    }
 
Example #29
Source File: AbortedExceptionClientExecutionTimerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
    when(sdkHttpClient.prepareRequest(any())).thenReturn(abortableCallable);
    httpClient = HttpTestUtils.testClientBuilder().httpClient(sdkHttpClient)
                              .apiCallTimeout(Duration.ofMillis(1000))
                              .build();
    when(abortableCallable.call()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder()
                                                                                                    .statusCode(200)
                                                                                                    .build())
                                                                 .build());
}
 
Example #30
Source File: DownloadResource.java    From tutorials with MIT License 5 votes vote down vote up
private static void checkResult(GetObjectResponse response) {
    SdkHttpResponse sdkResponse = response.sdkHttpResponse();
    if ( sdkResponse != null && sdkResponse.isSuccessful()) {
        return;
    }
    
    throw new DownloadFailedException(response);
}