use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.
the class AmazonHttpClientTest method testTemporaryRedirect.
@Test
public void testTemporaryRedirect() throws IOException, URISyntaxException {
HttpResponse redirectResponse = HttpResponse.builder().statusCode(307).header("Location", "https://www.redirect.com").build();
HttpResponse successfulResponse = HttpResponse.builder().statusCode(200).content(null).build();
final Request<?> request = new DefaultRequest<String>(new AmazonWebServiceRequest() {
}, "TestService");
request.setHttpMethod(HttpMethodName.GET);
request.setEndpoint(new URI("https://www.test.com"));
request.setResourcePath("/test/table");
HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() {
@Override
public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception {
AmazonWebServiceResponse<String> awsResponse = new AmazonWebServiceResponse<String>();
awsResponse.setResult("Result");
return awsResponse;
}
@Override
public boolean needsConnectionLeftOpen() {
return false;
}
};
ExecutionContext ec = EasyMock.createMock(ExecutionContext.class);
EasyMock.reset(httpClient, ec);
final List<Boolean> signerCalled = new ArrayList<Boolean>();
EasyMock.expect(ec.getAwsRequestMetrics()).andReturn(new AWSRequestMetrics()).anyTimes();
EasyMock.expect(ec.getContextUserAgent()).andReturn("TestUserAgent").anyTimes();
EasyMock.expect(ec.getCredentials()).andReturn(new AnonymousAWSCredentials());
EasyMock.expect(ec.getSignerByURI(EasyMock.anyObject(URI.class))).andReturn(new Signer() {
@Override
public void sign(Request<?> requestToSign, AWSCredentials credentials) {
assertSame(request, requestToSign);
assertTrue(credentials instanceof AnonymousAWSCredentials);
signerCalled.add(true);
}
});
EasyMock.expect(httpClient.execute(EasyMock.<HttpRequest>anyObject())).andReturn(redirectResponse);
Capture<HttpRequest> capture = new Capture<HttpRequest>();
EasyMock.expect(httpClient.execute(EasyMock.capture(capture))).andReturn(successfulResponse);
EasyMock.replay(httpClient, ec);
Response<String> response = client.executeHelper(request, responseHandler, null, ec);
assertEquals(response.getAwsResponse(), "Result");
assertEquals(signerCalled.size(), 2);
assertTrue(signerCalled.get(0));
assertTrue(signerCalled.get(1));
assertEquals(capture.getValue().getUri().toString(), "https://www.redirect.com/");
EasyMock.verify(httpClient, ec);
}
use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.
the class AmazonPollyPresigningClient method getPresignedSynthesizeSpeechUrl.
/**
* <p>
* Returns a pre-signed URL for accessing an Amazon Polly resource.
* </p>
* <p>
* Pre-signed URLs allow clients to form a URL for an Amazon Polly
* resource, and then sign it with the current AWS security credentials.
* The pre-signed URL can be shared to other users, allowing access to
* the resource without providing an account's AWS security credentials.
* </p>
* <p>
* Pre-signed URLs are useful in many situations where AWS security
* credentials aren't available from the client that needs to make the
* actual request to Amazon Polly.
* </p>
* <p>
* For example, a pre-signed URL to GET the synthesized speech audio stream
* using the owner's AWS account can be generated and passed to a system
* media player.
* </p>
*
* @param synthesizeSpeechPresignRequest Object providing pre-signed synthesize speech
* request parameters.
* @return Pre-signed URL of audio stream containing synthesized speech
* matching parameters provided in synthesizeSpeechPresignRequest
* that is possible to obtain using HTTP GET method.
*/
public URL getPresignedSynthesizeSpeechUrl(SynthesizeSpeechPresignRequest synthesizeSpeechPresignRequest) {
Request<PresigningRequest> request = new SynthesizeSpeechPresignRequestMarshaller().marshall(synthesizeSpeechPresignRequest);
request.setEndpoint(endpoint);
request.setTimeOffset(timeOffset);
if (synthesizeSpeechPresignRequest.getExpiration() == null) {
synthesizeSpeechPresignRequest.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * DEFAULT_GET_REQUEST_EXPIRATION_MINUTES));
}
Signer signer = getSignerByURI(endpoint);
if (!(signer instanceof Presigner)) {
throw new AmazonClientException("Unsupported signer");
}
Presigner presigner = (Presigner) signer;
Date expirationDate = synthesizeSpeechPresignRequest.getExpiration();
AWSCredentials credentials = synthesizeSpeechPresignRequest.getRequestCredentials();
if (credentials == null) {
credentials = awsCredentialsProvider.getCredentials();
}
presigner.presignRequest(request, credentials, expirationDate);
// Remove the leading slash (if any) in the resource-path
return ServiceUtils.convertRequestToUrl(request, true);
}
use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.
the class AmazonWebServiceClient method setSignerRegionOverride.
/**
* An internal method used to explicitly override the internal signer region
* computed by the default implementation. This method is not expected to be
* normally called except for AWS internal development purposes.
* @param signerRegionOverride the signer region override.
*/
@SuppressWarnings("checkstyle:hiddenfield")
public final void setSignerRegionOverride(final String signerRegionOverride) {
final Signer signer = computeSignerByURI(endpoint, signerRegionOverride, true);
synchronized (this) {
this.signer = signer;
this.signerRegionOverride = signerRegionOverride;
}
}
use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.
the class Amazons3ClientTest method testCreateSigner.
@Test
public void testCreateSigner() {
s3.setS3ClientOptions(accelerateOption);
final Regions region = Regions.US_WEST_2;
s3.setRegion(Region.getRegion(region));
final String bucketName = "bucket";
final String key = "key";
final HttpMethodName method = HttpMethodName.GET;
final GetObjectRequest originalRequest = new GetObjectRequest(bucketName, key);
final Request<?> request = s3.createRequest(bucketName, key, originalRequest, method);
final Signer signer = s3.createSigner(request, bucketName, key);
assertTrue(signer instanceof AWSS3V4Signer);
signer.sign(request, creds);
final String authorization = request.getHeaders().get("Authorization");
assertNotNull(authorization);
final String regionName = authorization.split("/")[2];
assertEquals(region.getName(), regionName);
}
use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.
the class Amazons3ClientTest method testCreateSignerWithSpecialCharacterKeys.
@Test
public void testCreateSignerWithSpecialCharacterKeys() {
s3.setS3ClientOptions(accelerateOption);
final Regions region = Regions.US_WEST_2;
s3.setRegion(Region.getRegion(region));
final String bucketName = "bucket";
final String key = "key%^!@#*()";
final HttpMethodName method = HttpMethodName.GET;
final GetObjectRequest originalRequest = new GetObjectRequest(bucketName, key);
final Request<?> request = s3.createRequest(bucketName, key, originalRequest, method);
final Signer signer = s3.createSigner(request, bucketName, key);
assertTrue(signer instanceof AWSS3V4Signer);
signer.sign(request, creds);
final String authorization = request.getHeaders().get("Authorization");
assertNotNull(authorization);
final String regionName = authorization.split("/")[2];
assertEquals(region.getName(), regionName);
assertTrue(request.getResourcePath().contains(key));
}
Aggregations