Java Code Examples for software.amazon.awssdk.http.SdkHttpResponse#firstMatchingHeader()

The following examples show how to use software.amazon.awssdk.http.SdkHttpResponse#firstMatchingHeader() . 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: 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 2
Source File: ClockSkew.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Get the server time from the service response, or empty if the time could not be determined.
 */
public static Optional<Instant> getServerTime(SdkHttpResponse serviceResponse) {
    Optional<String> responseDateHeader = serviceResponse.firstMatchingHeader("Date");

    if (responseDateHeader.isPresent()) {
        String serverDate = responseDateHeader.get();
        log.debug(() -> "Reported service date: " + serverDate);

        try {
            return Optional.of(DateUtils.parseRfc1123Date(serverDate));
        } catch (RuntimeException e) {
            log.warn(() -> "Unable to parse clock skew offset from response: " + serverDate, e);
            return Optional.empty();
        }
    }

    log.debug(() -> "Service did not return a Date header, so clock skew adjustments will not be applied.");
    return Optional.empty();
}