Example usage for com.amazonaws DefaultRequest DefaultRequest

List of usage examples for com.amazonaws DefaultRequest DefaultRequest

Introduction

In this page you can find the example usage for com.amazonaws DefaultRequest DefaultRequest.

Prototype

public DefaultRequest(String serviceName) 

Source Link

Document

Constructs a new DefaultRequest with the specified service name and no specified original, user facing request object.

Usage

From source file:com.erudika.para.rest.Signer.java

License:Apache License

private Request<?> buildAWSRequest(String httpMethod, String endpoint, String resourcePath,
        Map<String, String> headers, Map<String, String> params, InputStream entity) {
    Request<?> r = new DefaultRequest<AmazonWebServiceRequest>(Config.PARA);

    if (!StringUtils.isBlank(httpMethod)) {
        r.setHttpMethod(HttpMethodName.valueOf(httpMethod));
    }/*from w  w w . j ava  2 s .c  o m*/
    if (!StringUtils.isBlank(endpoint)) {
        if (!endpoint.startsWith("http")) {
            endpoint = "https://" + endpoint;
        }
        r.setEndpoint(URI.create(endpoint));
    }
    if (!StringUtils.isBlank(resourcePath)) {
        r.setResourcePath(SdkHttpUtils.urlEncode(resourcePath, true));
    }
    if (headers != null) {
        if (headers.containsKey("x-amz-date")) {
            overriddenDate = parseAWSDate(headers.get("x-amz-date"));
        }
        // we don't need these here, added by default
        headers.remove("host");
        headers.remove("x-amz-date");
        r.setHeaders(headers);
    }
    if (params != null) {
        for (Map.Entry<String, String> param : params.entrySet()) {
            r.addParameter(param.getKey(), param.getValue());
        }
    }
    if (entity != null) {
        r.setContent(entity);
    }
    return r;
}

From source file:com.ghedeon.AwsInterceptor.java

License:Apache License

@SuppressWarnings("unchecked")
@Nonnull/*from   w w  w .j  a  v a 2 s . c  o  m*/
private Request sign(@Nonnull Request request) throws IOException {
    Request.Builder builder = request.newBuilder();
    DefaultRequest awsDummyRequest = new DefaultRequest(serviceName);

    HttpUrl url = setEndpoint(builder, awsDummyRequest, request.url());

    setQueryParams(awsDummyRequest, url);

    setHttpMethod(awsDummyRequest, request.method());

    setBody(awsDummyRequest, request.body());

    signer.sign(awsDummyRequest, credentialsProvider.getCredentials());

    applyAwsHeaders(builder, awsDummyRequest.getHeaders());

    return builder.build();
}

From source file:com.streamsets.pipeline.lib.aws.AwsRequestSigningApacheInterceptor.java

License:Apache License

/**
 * {@inheritDoc}/*  w  w w . ja  v a  2  s .c  om*/
 */
@Override
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 DefaultRequest
    DefaultRequest<?> signableRequest = new DefaultRequest<>(service);

    HttpHost host = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
    if (host != null) {
        signableRequest.setEndpoint(URI.create(host.toURI()));
    }
    final HttpMethodName httpMethod = HttpMethodName.fromValue(request.getRequestLine().getMethod());
    signableRequest.setHttpMethod(httpMethod);
    try {
        signableRequest.setResourcePath(uriBuilder.build().getRawPath());
    } catch (URISyntaxException e) {
        throw new IOException("Invalid URI", e);
    }

    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) request;
        if (httpEntityEnclosingRequest.getEntity() != null) {
            signableRequest.setContent(httpEntityEnclosingRequest.getEntity().getContent());
        }
    }
    signableRequest.setParameters(nvpToMapParams(uriBuilder.getQueryParams()));
    signableRequest.setHeaders(headerArrayToMap(request.getAllHeaders()));

    // Sign it
    signer.sign(signableRequest, awsCredentialsProvider.getCredentials());

    // Now copy everything back
    request.setHeaders(mapToHeaderArray(signableRequest.getHeaders()));
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) request;
        if (httpEntityEnclosingRequest.getEntity() != null) {
            BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
            basicHttpEntity.setContent(signableRequest.getContent());
            httpEntityEnclosingRequest.setEntity(basicHttpEntity);
        }
    }
}

From source file:org.springframework.vault.authentication.AwsIamAuthentication.java

License:Apache License

private static String getSignedHeaders(AwsIamAuthenticationOptions options) {

    Map<String, String> headers = createIamRequestHeaders(options);

    AWS4Signer signer = new AWS4Signer();

    DefaultRequest<String> request = new DefaultRequest<>("sts");

    request.setContent(new ByteArrayInputStream(REQUEST_BODY.getBytes()));
    request.setHeaders(headers);/*w w  w .jav a2  s  .  c  om*/
    request.setHttpMethod(HttpMethodName.POST);
    request.setEndpoint(options.getEndpointUri());

    signer.setServiceName(request.getServiceName());
    signer.sign(request, options.getCredentialsProvider().getCredentials());

    Map<String, Object> map = new LinkedHashMap<>();

    for (Entry<String, String> entry : request.getHeaders().entrySet()) {
        map.put(entry.getKey(), Collections.singletonList(entry.getValue()));
    }

    try {
        return OBJECT_MAPPER.writeValueAsString(map);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Cannot serialize headers to JSON", e);
    }
}