in data-prepper-plugins/opensearch/src/main/java/com/amazon/dataprepper/plugins/sink/opensearch/AwsRequestSigningApacheInterceptor.java [122:168]
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
URIBuilder uriBuilder;
try {
uriBuilder = new URIBuilder(request.getRequestLine().getUri());
} catch (URISyntaxException e) {
throw new IOException("Invalid URI" , e);
}
// Copy Apache HttpRequest to AWS Request
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder()
.method(SdkHttpMethod.fromValue(request.getRequestLine().getMethod()))
.uri(buildUri(context, uriBuilder));
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest httpEntityEnclosingRequest =
(HttpEntityEnclosingRequest) request;
if (httpEntityEnclosingRequest.getEntity() != null) {
InputStream content = httpEntityEnclosingRequest.getEntity().getContent();
requestBuilder.contentStreamProvider(() -> content);
}
}
requestBuilder.rawQueryParameters(nvpToMapParams(uriBuilder.getQueryParams()));
requestBuilder.headers(headerArrayToMap(request.getAllHeaders()));
ExecutionAttributes attributes = new ExecutionAttributes();
attributes.putAttribute(AwsSignerExecutionAttribute.AWS_CREDENTIALS, awsCredentialsProvider.resolveCredentials());
attributes.putAttribute(AwsSignerExecutionAttribute.SERVICE_SIGNING_NAME, service);
attributes.putAttribute(AwsSignerExecutionAttribute.SIGNING_REGION, region);
// Sign it
SdkHttpFullRequest signedRequest = signer.sign(requestBuilder.build(), attributes);
// Now copy everything back
request.setHeaders(mapToHeaderArray(signedRequest.headers()));
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest httpEntityEnclosingRequest =
(HttpEntityEnclosingRequest) request;
if (httpEntityEnclosingRequest.getEntity() != null) {
BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
basicHttpEntity.setContent(signedRequest.contentStreamProvider()
.orElseThrow(() -> new IllegalStateException("There must be content"))
.newStream());
httpEntityEnclosingRequest.setEntity(basicHttpEntity);
}
}
}