use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.
the class QueryParamBindingTest method testReservedCharInParamValue.
/**
* Make sure the marshaller is able to handle @UriLabel parameter values
* containing special characters.
*/
@Test
public void testReservedCharInParamValue() {
final String VALUE_WITH_SEMICOLON = ";foo";
final String VALUE_WITH_AMPERSAND = "&bar";
final String VALUE_WITH_QUESTION_MARK = "?charlie";
ListHealthChecksRequest listReq = ListHealthChecksRequest.builder().marker(VALUE_WITH_SEMICOLON).maxItems(VALUE_WITH_AMPERSAND).build();
SdkHttpFullRequest httpReq_List = new ListHealthChecksRequestMarshaller(PROTOCOL_FACTORY).marshall(listReq);
assertEquals("/2013-04-01/healthcheck", httpReq_List.encodedPath());
Map<String, List<String>> queryParams = httpReq_List.rawQueryParameters();
assertEquals(2, queryParams.size());
assertEquals(VALUE_WITH_SEMICOLON, queryParams.get("marker").get(0));
assertEquals(VALUE_WITH_AMPERSAND, queryParams.get("maxitems").get(0));
GetHealthCheckLastFailureReasonRequest getFailureReq = GetHealthCheckLastFailureReasonRequest.builder().healthCheckId(VALUE_WITH_QUESTION_MARK).build();
SdkHttpFullRequest httpReq_GetFailure = new GetHealthCheckLastFailureReasonRequestMarshaller(PROTOCOL_FACTORY).marshall(getFailureReq);
System.out.println(httpReq_GetFailure);
// parameter value should be URL encoded
assertEquals("/2013-04-01/healthcheck/%3Fcharlie/lastfailurereason", httpReq_GetFailure.encodedPath());
queryParams = httpReq_GetFailure.rawQueryParameters();
assertEquals(0, queryParams.size());
}
use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.
the class AwsS3V4SignerIntegrationTest method test_SignMethod_WithModeledParam_And_WithoutUsingSdkClient.
@Test
public void test_SignMethod_WithModeledParam_And_WithoutUsingSdkClient() throws Exception {
AwsS3V4Signer signer = AwsS3V4Signer.create();
SdkHttpFullRequest httpFullRequest = generateBasicGetRequest();
// sign the request
SdkHttpFullRequest signedRequest = signer.sign(httpFullRequest, constructSignerParams());
SdkHttpClient httpClient = ApacheHttpClient.builder().build();
HttpExecuteResponse response = httpClient.prepareRequest(HttpExecuteRequest.builder().request(signedRequest).build()).call();
assertEquals("Non success http status code", 200, response.httpResponse().statusCode());
String actualResult = IoUtils.toUtf8String(response.responseBody().get());
assertEquals(CONTENT, actualResult);
}
use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.
the class DefaultS3Presigner method initializePresignedRequest.
/**
* Initialize the provided presigned request.
*/
private void initializePresignedRequest(PresignedRequest.Builder presignedRequest, ExecutionContext execCtx, SdkHttpFullRequest signedHttpRequest) {
SdkBytes signedPayload = signedHttpRequest.contentStreamProvider().map(p -> SdkBytes.fromInputStream(p.newStream())).orElse(null);
List<String> signedHeadersQueryParam = signedHttpRequest.rawQueryParameters().get("X-Amz-SignedHeaders");
Validate.validState(signedHeadersQueryParam != null, "Only SigV4 presigners are supported at this time, but the configured " + "presigner (%s) did not seem to generate a SigV4 signature.", execCtx.signer());
Map<String, List<String>> signedHeaders = signedHeadersQueryParam.stream().flatMap(h -> Stream.of(h.split(";"))).collect(toMap(h -> h, h -> signedHttpRequest.firstMatchingHeader(h).map(Collections::singletonList).orElseGet(ArrayList::new)));
boolean isBrowserExecutable = signedHttpRequest.method() == SdkHttpMethod.GET && signedPayload == null && (signedHeaders.isEmpty() || (signedHeaders.size() == 1 && signedHeaders.containsKey("host")));
presignedRequest.expiration(execCtx.executionAttributes().getAttribute(PRESIGNER_EXPIRATION)).isBrowserExecutable(isBrowserExecutable).httpRequest(signedHttpRequest).signedHeaders(signedHeaders).signedPayload(signedPayload);
}
use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.
the class ProfileFileConfigurationTest method profileIsHonoredForCredentialsAndRegion.
@Test
public void profileIsHonoredForCredentialsAndRegion() {
EnvironmentVariableHelper.run(env -> {
env.remove(SdkSystemSetting.AWS_REGION);
env.remove(SdkSystemSetting.AWS_ACCESS_KEY_ID);
env.remove(SdkSystemSetting.AWS_SECRET_ACCESS_KEY);
String profileContent = "[profile foo]\n" + "region = us-banana-46\n" + "aws_access_key_id = profileIsHonoredForCredentials_akid\n" + "aws_secret_access_key = profileIsHonoredForCredentials_skid";
String profileName = "foo";
Signer signer = mock(NoOpSigner.class);
ProtocolRestJsonClient client = ProtocolRestJsonClient.builder().overrideConfiguration(overrideConfig(profileContent, profileName, signer)).build();
Mockito.when(signer.sign(any(), any())).thenCallRealMethod();
try {
client.allTypes();
} catch (SdkClientException e) {
// expected
}
ArgumentCaptor<SdkHttpFullRequest> httpRequest = ArgumentCaptor.forClass(SdkHttpFullRequest.class);
ArgumentCaptor<ExecutionAttributes> attributes = ArgumentCaptor.forClass(ExecutionAttributes.class);
Mockito.verify(signer).sign(httpRequest.capture(), attributes.capture());
AwsCredentials credentials = attributes.getValue().getAttribute(AwsSignerExecutionAttribute.AWS_CREDENTIALS);
assertThat(credentials.accessKeyId()).isEqualTo("profileIsHonoredForCredentials_akid");
assertThat(credentials.secretAccessKey()).isEqualTo("profileIsHonoredForCredentials_skid");
Region region = attributes.getValue().getAttribute(AwsExecutionAttribute.AWS_REGION);
assertThat(region.id()).isEqualTo("us-banana-46");
assertThat(httpRequest.getValue().getUri().getHost()).contains("us-banana-46");
});
}
use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.
the class V2DynamoDbAttributeValue method toUtf8ByteArray.
private static byte[] toUtf8ByteArray(Map<String, AttributeValue> item) {
SdkHttpFullRequest marshalled = putItemRequestMarshaller().marshall(PutItemRequest.builder().item(item).build());
InputStream content = marshalled.contentStreamProvider().get().newStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[8192];
int read;
try {
while ((read = content.read(buff)) != -1) {
baos.write(buff, 0, read);
}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
return baos.toByteArray();
}
Aggregations