Search in sources :

Example 96 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class RestJsonEventStreamProtocolTest method headersOnly.

@Test
public void headersOnly() {
    HeadersOnlyEventMarshaller marshaller = new HeadersOnlyEventMarshaller(protocolFactory());
    HeadersOnlyEvent event = InputEventStream.headersOnlyEventBuilder().headerMember("hello rest-json").build();
    SdkHttpFullRequest marshalledEvent = marshaller.marshall(event);
    assertThat(marshalledEvent.headers().keySet()).doesNotContain(EVENT_CONTENT_TYPE_HEADER);
    String content = contentAsString(marshalledEvent);
    assertThat(content).isEqualTo("");
}
Also used : HeadersOnlyEvent(software.amazon.awssdk.services.protocolrestjsoncontenttype.model.HeadersOnlyEvent) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) HeadersOnlyEventMarshaller(software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.HeadersOnlyEventMarshaller) Test(org.junit.jupiter.api.Test)

Example 97 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class RestJsonEventStreamProtocolTest method implicitPayloadAndHeaders_payloadMemberNotPresent.

@Test
public void implicitPayloadAndHeaders_payloadMemberNotPresent() {
    ImplicitPayloadAndHeadersEventMarshaller marshaller = new ImplicitPayloadAndHeadersEventMarshaller(protocolFactory());
    ImplicitPayloadAndHeadersEvent event = InputEventStream.implicitPayloadAndHeadersEventBuilder().headerMember("hello rest-json").build();
    SdkHttpFullRequest marshalledEvent = marshaller.marshall(event);
    assertThat(marshalledEvent.headers().get(EVENT_CONTENT_TYPE_HEADER)).containsExactly("application/json");
    String content = contentAsString(marshalledEvent);
    assertThat(content).isEqualTo("{}");
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ImplicitPayloadAndHeadersEvent(software.amazon.awssdk.services.protocolrestjsoncontenttype.model.ImplicitPayloadAndHeadersEvent) ImplicitPayloadAndHeadersEventMarshaller(software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.ImplicitPayloadAndHeadersEventMarshaller) Test(org.junit.jupiter.api.Test)

Example 98 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project httpx by httpx-sh.

the class AwsExecutor method execute.

public List<byte[]> execute(HttpRequest httpRequest) {
    try {
        String method = httpRequest.getMethod().getName();
        final byte[] bodyBytes = httpRequest.getBodyBytes();
        if (Objects.equals(method, "AWS")) {
            if (bodyBytes == null || bodyBytes.length == 0) {
                method = "GET";
            } else {
                method = "POST";
            }
        }
        final URI requestUri = httpRequest.getRequestTarget().getUri();
        String host = requestUri.getHost();
        String serviceName = host.substring(0, host.indexOf('.'));
        // resolve region id from X-Region-Id header or host name
        String regionId = httpRequest.getHeader("X-Region-Id");
        if (regionId == null) {
            // resolve region id from host
            String tempRegionId = host.replace(".amazonaws.com", "");
            if (tempRegionId.contains(".")) {
                tempRegionId = tempRegionId.substring(tempRegionId.indexOf(".") + 1);
            } else if (tempRegionId.contains("-")) {
                tempRegionId = tempRegionId.substring(tempRegionId.indexOf("-") + 1);
            }
            if (Aliyun.regions().contains(tempRegionId)) {
                regionId = tempRegionId;
            }
        }
        if (regionId == null) {
            regionId = "us-east-1";
        }
        @Nullable String[] credential = AWS.readAwsAccessToken(httpRequest);
        if (credential == null || credential.length < 2) {
            log.error("HTX-301-401");
            return Collections.emptyList();
        }
        for (String item : credential) {
            if (item != null) {
                // compatible VS Code humao.rest-client
                if (item.startsWith("region:")) {
                    regionId = item.replace("region:", "");
                } else if (item.startsWith("service:")) {
                    serviceName = item.replace("service:", "");
                }
            }
        }
        final SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder().uri(requestUri).method(SdkHttpMethod.valueOf(method));
        final Map<String, String> headers = httpRequest.getHeadersMap();
        headers.forEach((name, value) -> {
            if (!name.equalsIgnoreCase("Authorization")) {
                requestBuilder.putHeader(name, value);
            }
        });
        if (!headers.containsKey("Host")) {
            requestBuilder.putHeader("Host", host);
        }
        // set default format: json
        if (!headers.containsKey("Accept")) {
            requestBuilder.putHeader("Accept", "application/json");
        }
        // set body
        if (bodyBytes != null && bodyBytes.length > 0) {
            requestBuilder.contentStreamProvider(() -> new ByteArrayInputStream(bodyBytes));
        }
        final Aws4SignerParams aws4SignerParams = Aws4SignerParams.builder().awsCredentials(AwsBasicCredentials.create(credential[0], credential[1])).signingRegion(Region.of(regionId)).signingName(serviceName).build();
        final SdkHttpFullRequest awsRequest = requestBuilder.build();
        final SdkHttpFullRequest signedRequest = Aws4Signer.create().sign(awsRequest, aws4SignerParams);
        List<HttpHeader> newHeaders = new ArrayList<>();
        signedRequest.headers().forEach((name, values) -> {
            newHeaders.add(new HttpHeader(name, values.get(0)));
        });
        httpRequest.setHeaders(newHeaders);
        super.execute(httpRequest);
    } catch (Exception e) {
        log.error("HTX-101-500", e);
    }
    return Collections.emptyList();
}
Also used : Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) URI(java.net.URI) HttpHeader(org.mvnsearch.http.model.HttpHeader) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) Nullable(org.jetbrains.annotations.Nullable)

Example 99 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project httpx by servicex-sh.

the class AwsExecutor method execute.

public List<byte[]> execute(HttpRequest httpRequest) {
    try {
        String method = httpRequest.getMethod().getName();
        final byte[] bodyBytes = httpRequest.getBodyBytes();
        if (HttpMethod.AWS_METHODS.contains(method)) {
            if (Objects.equals(method, "AWS")) {
                method = "GET";
            } else {
                method = method.substring(3);
            }
        }
        final URI requestUri = httpRequest.getRequestTarget().getUri();
        String host = requestUri.getHost();
        String serviceName = host.substring(0, host.indexOf('.'));
        // resolve region id from X-Region-Id header or host name
        String regionId = AWS.readRegionId(httpRequest);
        @Nullable String[] credential = AWS.readAwsAccessToken(httpRequest);
        if (credential == null || credential.length < 2) {
            log.error("HTX-301-401");
            return Collections.emptyList();
        }
        for (String item : credential) {
            if (item != null) {
                // compatible VS Code humao.rest-client
                if (item.startsWith("region:")) {
                    regionId = item.replace("region:", "");
                } else if (item.startsWith("service:")) {
                    serviceName = item.replace("service:", "");
                }
            }
        }
        final SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder().uri(requestUri).method(SdkHttpMethod.valueOf(method));
        final Map<String, String> headers = httpRequest.getHeadersMap();
        headers.forEach((name, value) -> {
            if (!name.equalsIgnoreCase("Authorization")) {
                requestBuilder.putHeader(name, value);
            }
        });
        if (!headers.containsKey("Host")) {
            requestBuilder.putHeader("Host", host);
        }
        // set default format: json
        if (!headers.containsKey("Accept")) {
            requestBuilder.putHeader("Accept", "application/json");
        }
        // set body
        if (bodyBytes != null && bodyBytes.length > 0) {
            requestBuilder.contentStreamProvider(() -> new ByteArrayInputStream(bodyBytes));
        }
        final Aws4SignerParams aws4SignerParams = Aws4SignerParams.builder().awsCredentials(AwsBasicCredentials.create(credential[0], credential[1])).signingRegion(Region.of(regionId)).signingName(serviceName).build();
        final SdkHttpFullRequest awsRequest = requestBuilder.build();
        final SdkHttpFullRequest signedRequest = Aws4Signer.create().sign(awsRequest, aws4SignerParams);
        List<HttpHeader> newHeaders = new ArrayList<>();
        signedRequest.headers().forEach((name, values) -> {
            newHeaders.add(new HttpHeader(name, values.get(0)));
        });
        httpRequest.setHeaders(newHeaders);
        super.execute(httpRequest);
    } catch (Exception e) {
        log.error("HTX-101-500", e);
    }
    return Collections.emptyList();
}
Also used : Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) URI(java.net.URI) HttpHeader(org.mvnsearch.http.model.HttpHeader) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) Nullable(org.jetbrains.annotations.Nullable)

Example 100 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project formkiq-core by formkiq.

the class StsService method createSignedRequest.

/**
 * Create a Signed Request.
 *
 * @param requestbuilder {@link Builder}
 * @param params {@link Aws4SignerParams}
 *
 * @return {@link SdkHttpFullRequest}
 * @throws URISyntaxException URISyntaxException
 */
public SdkHttpFullRequest createSignedRequest(final SdkHttpFullRequest.Builder requestbuilder, final Aws4SignerParams params) throws URISyntaxException {
    Aws4Signer signer = Aws4Signer.create();
    SdkHttpFullRequest req = signer.sign(requestbuilder.build(), params);
    return req;
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) Aws4Signer(software.amazon.awssdk.auth.signer.Aws4Signer)

Aggregations

SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)178 Test (org.junit.Test)70 Test (org.junit.jupiter.api.Test)50 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)43 URI (java.net.URI)25 SigningTestCase (software.amazon.awssdk.authcrt.signer.SigningTestCase)23 ByteArrayInputStream (java.io.ByteArrayInputStream)18 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)16 List (java.util.List)13 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)13 RequestExecutionContext (software.amazon.awssdk.core.internal.http.RequestExecutionContext)12 ByteBuffer (java.nio.ByteBuffer)11 AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)11 Map (java.util.Map)10 ContentStreamProvider (software.amazon.awssdk.http.ContentStreamProvider)10 Instant (java.time.Instant)9 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)9 Aws4SignerParams (software.amazon.awssdk.auth.signer.params.Aws4SignerParams)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8