private void setContentTypeHeaderIfNeeded(String contentType) { if (contentType != null && !request.headers().containsKey(CONTENT_TYPE)) { request.putHeader(CONTENT_TYPE, contentType); } }
private void setContentTypeHeaderIfNeeded(String contentType) { if (contentType != null && !request.headers().containsKey(CONTENT_TYPE)) { request.putHeader(CONTENT_TYPE, contentType); } }
private SdkHttpFullRequest finishMarshalling() { // Content may already be set if the payload is binary data. if (request.contentStreamProvider() == null) { // End the implicit request object if needed. if (!hasExplicitPayloadMember) { jsonGenerator.writeEndObject(); } byte[] content = jsonGenerator.getBytes(); request.contentStreamProvider(() -> new ByteArrayInputStream(content)); if (content.length > 0) { request.putHeader(CONTENT_LENGTH, Integer.toString(content.length)); } } // We skip setting the default content type if the request is streaming as // content-type is determined based on the body of the stream if (!request.headers().containsKey(CONTENT_TYPE) && contentType != null && !hasStreamingInput) { request.putHeader(CONTENT_TYPE, contentType); } return request.build(); }
/** * Step 1 of the AWS Signature version 4 calculation. Refer to * http://docs.aws * .amazon.com/general/latest/gr/sigv4-create-canonical-request.html to * generate the canonical request. */ private String createCanonicalRequest(SdkHttpFullRequest.Builder request, String contentSha256, boolean doubleUrlEncode) { String canonicalRequest = request.method().toString() + SignerConstant.LINE_SEPARATOR + // This would optionally double url-encode the resource path getCanonicalizedResourcePath(request.encodedPath(), doubleUrlEncode) + SignerConstant.LINE_SEPARATOR + getCanonicalizedQueryString(request.rawQueryParameters()) + SignerConstant.LINE_SEPARATOR + getCanonicalizedHeaderString(request.headers()) + SignerConstant.LINE_SEPARATOR + getSignedHeadersString(request.headers()) + SignerConstant.LINE_SEPARATOR + contentSha256; LOG.trace(() -> "AWS4 Canonical Request: " + canonicalRequest); return canonicalRequest; }
/** * Step 1 of the AWS Signature version 4 calculation. Refer to * http://docs.aws * .amazon.com/general/latest/gr/sigv4-create-canonical-request.html to * generate the canonical request. */ private String createCanonicalRequest(SdkHttpFullRequest.Builder request, String contentSha256, boolean doubleUrlEncode) { String canonicalRequest = request.method().toString() + SignerConstant.LINE_SEPARATOR + // This would optionally double url-encode the resource path getCanonicalizedResourcePath(request.encodedPath(), doubleUrlEncode) + SignerConstant.LINE_SEPARATOR + getCanonicalizedQueryString(request.rawQueryParameters()) + SignerConstant.LINE_SEPARATOR + getCanonicalizedHeaderString(request.headers()) + SignerConstant.LINE_SEPARATOR + getSignedHeadersString(request.headers()) + SignerConstant.LINE_SEPARATOR + contentSha256; LOG.trace(() -> "AWS4 Canonical Request: " + canonicalRequest); return canonicalRequest; }
private SdkHttpFullRequest finishMarshalling() { // Content may already be set if the payload is binary data. if (request.contentStreamProvider() == null) { // End the implicit request object if needed. if (!hasExplicitPayloadMember) { jsonGenerator.writeEndObject(); } byte[] content = jsonGenerator.getBytes(); request.contentStreamProvider(() -> new ByteArrayInputStream(content)); if (content.length > 0) { request.putHeader(CONTENT_LENGTH, Integer.toString(content.length)); } } // We skip setting the default content type if the request is streaming as // content-type is determined based on the body of the stream if (!request.headers().containsKey(CONTENT_TYPE) && contentType != null && !hasStreamingInput) { request.putHeader(CONTENT_TYPE, contentType); } return request.build(); }
/** * Perform a case-insensitive search for a particular header in this request, returning the first matching header, if one * is found. * * <p>This is useful for headers like 'Content-Type' or 'Content-Length' of which there is expected to be only one value * present.</p> * * <p>This is equivalent to invoking {@link SdkHttpUtils#firstMatchingHeader(Map, String)}</p>. * * @param header The header to search for (case insensitively). * @return The first header that matched the requested one, or empty if one was not found. */ @Override default Optional<String> firstMatchingHeader(String header) { return SdkHttpUtils.firstMatchingHeader(headers(), header); }
/** * Includes all the signing headers as request parameters for pre-signing. */ private void addPreSignInformationToRequest(SdkHttpFullRequest.Builder mutableRequest, AwsCredentials sanitizedCredentials, Aws4SignerRequestParams signerParams, String timeStamp, long expirationInSeconds) { String signingCredentials = sanitizedCredentials.accessKeyId() + "/" + signerParams.getScope(); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_ALGORITHM, SignerConstant.AWS4_SIGNING_ALGORITHM); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_DATE, timeStamp); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_SIGNED_HEADER, getSignedHeadersString(mutableRequest.headers())); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_EXPIRES, Long.toString(expirationInSeconds)); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_CREDENTIAL, signingCredentials); }
/** * Perform a case-insensitive search for a particular header in this request, returning the first matching header, if one * is found. * * <p>This is useful for headers like 'Content-Type' or 'Content-Length' of which there is expected to be only one value * present.</p> * * <p>This is equivalent to invoking {@link SdkHttpUtils#firstMatchingHeader(Map, String)}</p>. * * @param header The header to search for (case insensitively). * @return The first header that matched the requested one, or empty if one was not found. */ @Override default Optional<String> firstMatchingHeader(String header) { return SdkHttpUtils.firstMatchingHeader(headers(), header); }
/** * Creates the authorization header to be included in the request. */ private String buildAuthorizationHeader(byte[] signature, AwsCredentials credentials, Aws4SignerRequestParams signerParams, SdkHttpFullRequest.Builder mutableRequest) { String signingCredentials = credentials.accessKeyId() + "/" + signerParams.getScope(); String credential = "Credential=" + signingCredentials; String signerHeaders = "SignedHeaders=" + getSignedHeadersString(mutableRequest.headers()); String signatureHeader = "Signature=" + BinaryUtils.toHex(signature); return SignerConstant.AWS4_SIGNING_ALGORITHM + " " + credential + ", " + signerHeaders + ", " + signatureHeader; }
/** * Includes all the signing headers as request parameters for pre-signing. */ private void addPreSignInformationToRequest(SdkHttpFullRequest.Builder mutableRequest, AwsCredentials sanitizedCredentials, Aws4SignerRequestParams signerParams, String timeStamp, long expirationInSeconds) { String signingCredentials = sanitizedCredentials.accessKeyId() + "/" + signerParams.getScope(); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_ALGORITHM, SignerConstant.AWS4_SIGNING_ALGORITHM); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_DATE, timeStamp); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_SIGNED_HEADER, getSignedHeadersString(mutableRequest.headers())); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_EXPIRES, Long.toString(expirationInSeconds)); mutableRequest.putRawQueryParameter(SignerConstant.X_AMZ_CREDENTIAL, signingCredentials); }
/** * Creates the authorization header to be included in the request. */ private String buildAuthorizationHeader(byte[] signature, AwsCredentials credentials, Aws4SignerRequestParams signerParams, SdkHttpFullRequest.Builder mutableRequest) { String signingCredentials = credentials.accessKeyId() + "/" + signerParams.getScope(); String credential = "Credential=" + signingCredentials; String signerHeaders = "SignedHeaders=" + getSignedHeadersString(mutableRequest.headers()); String signatureHeader = "Signature=" + BinaryUtils.toHex(signature); return SignerConstant.AWS4_SIGNING_ALGORITHM + " " + credential + ", " + signerHeaders + ", " + signatureHeader; }
@Override public SdkHttpFullRequest.Builder toBuilder() { return new Builder() .contentStreamProvider(contentStreamProvider) .protocol(protocol) .host(host) .port(port) .encodedPath(path) .rawQueryParameters(queryParameters) .method(httpMethod) .headers(headers) ; }
@Override public SdkHttpFullRequest.Builder execute(SdkHttpFullRequest.Builder request, RequestExecutionContext context) throws Exception { return request.headers(mergeHeaders(request.headers(), config.option(SdkClientOption.ADDITIONAL_HTTP_HEADERS), adaptHeaders(context.requestConfig().headers()))); }
@Override public SdkHttpFullRequest.Builder toBuilder() { return new Builder() .contentStreamProvider(contentStreamProvider) .protocol(protocol) .host(host) .port(port) .encodedPath(path) .rawQueryParameters(queryParameters) .method(httpMethod) .headers(headers) ; }