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("");
}
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("{}");
}
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();
}
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();
}
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;
}
Aggregations