Search in sources :

Example 1 with HTTP_TARGET_HOST

use of org.apache.http.protocol.HttpCoreContext.HTTP_TARGET_HOST in project presto by prestodb.

the class AwsRequestSigner method process.

@Override
public void process(final HttpRequest request, final HttpContext context) throws IOException {
    String method = request.getRequestLine().getMethod();
    URI uri = URI.create(request.getRequestLine().getUri());
    URIBuilder uriBuilder = new URIBuilder(uri);
    Map<String, List<String>> parameters = new TreeMap<>(CASE_INSENSITIVE_ORDER);
    for (NameValuePair parameter : uriBuilder.getQueryParams()) {
        parameters.computeIfAbsent(parameter.getName(), key -> new ArrayList<>()).add(parameter.getValue());
    }
    Map<String, String> headers = Arrays.stream(request.getAllHeaders()).collect(toImmutableMap(Header::getName, Header::getValue));
    InputStream content = null;
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
        if (enclosingRequest.getEntity() != null) {
            content = enclosingRequest.getEntity().getContent();
        }
    }
    DefaultRequest<?> awsRequest = new DefaultRequest<>(SERVICE_NAME);
    HttpHost host = (HttpHost) context.getAttribute(HTTP_TARGET_HOST);
    if (host != null) {
        awsRequest.setEndpoint(URI.create(host.toURI()));
    }
    awsRequest.setHttpMethod(HttpMethodName.fromValue(method));
    awsRequest.setResourcePath(uri.getRawPath());
    awsRequest.setContent(content);
    awsRequest.setParameters(parameters);
    awsRequest.setHeaders(headers);
    signer.sign(awsRequest, credentialsProvider.getCredentials());
    Header[] newHeaders = awsRequest.getHeaders().entrySet().stream().map(entry -> new BasicHeader(entry.getKey(), entry.getValue())).toArray(Header[]::new);
    request.setHeaders(newHeaders);
    InputStream newContent = awsRequest.getContent();
    checkState(newContent == null || request instanceof HttpEntityEnclosingRequest);
    if (newContent != null) {
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(newContent);
        ((HttpEntityEnclosingRequest) request).setEntity(entity);
    }
}
Also used : Arrays(java.util.Arrays) DefaultRequest(com.amazonaws.DefaultRequest) CASE_INSENSITIVE_ORDER(java.lang.String.CASE_INSENSITIVE_ORDER) HTTP_TARGET_HOST(org.apache.http.protocol.HttpCoreContext.HTTP_TARGET_HOST) Header(org.apache.http.Header) ArrayList(java.util.ArrayList) Map(java.util.Map) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) AWS4Signer(com.amazonaws.auth.AWS4Signer) URI(java.net.URI) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) URIBuilder(org.apache.http.client.utils.URIBuilder) IOException(java.io.IOException) HttpMethodName(com.amazonaws.http.HttpMethodName) HttpRequest(org.apache.http.HttpRequest) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) TreeMap(java.util.TreeMap) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpContext(org.apache.http.protocol.HttpContext) BasicHeader(org.apache.http.message.BasicHeader) NameValuePair(org.apache.http.NameValuePair) HttpHost(org.apache.http.HttpHost) InputStream(java.io.InputStream) NameValuePair(org.apache.http.NameValuePair) DefaultRequest(com.amazonaws.DefaultRequest) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) TreeMap(java.util.TreeMap) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) ArrayList(java.util.ArrayList) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

DefaultRequest (com.amazonaws.DefaultRequest)1 AWS4Signer (com.amazonaws.auth.AWS4Signer)1 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)1 HttpMethodName (com.amazonaws.http.HttpMethodName)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CASE_INSENSITIVE_ORDER (java.lang.String.CASE_INSENSITIVE_ORDER)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Header (org.apache.http.Header)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 HttpHost (org.apache.http.HttpHost)1 HttpRequest (org.apache.http.HttpRequest)1 HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)1