Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.amazonaws.*;
- import com.amazonaws.auth.AWS4Signer;
- import com.amazonaws.auth.BasicAWSCredentials;
- import com.amazonaws.http.*;
- import com.amazonaws.util.IOUtils;
- import org.apache.commons.io.FileUtils;
- import org.apache.poi.ss.formula.functions.T;
- import org.primefaces.json.JSONObject;
- import java.io.IOException;
- import java.net.URI;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- public class Main2
- {
- public static void main(String[] args) throws IOException
- {
- String accessKey, secretKey, sessionId;
- Response<AmazonWebServiceResponse> authResponse = sendAmazonAuthRequest();
- System.out.println("Auth http Response status: " + authResponse.getHttpResponse().getStatusCode());
- Object jsonAuthResult = authResponse.getAwsResponse().getResult();
- System.out.println("Auth response content: " + jsonAuthResult);
- JSONObject obj = new JSONObject(jsonAuthResult.toString());
- accessKey = obj.getString("accessKey");
- secretKey = obj.getString("secretKey");
- sessionId = obj.getString("sessionId");
- String url = ""; // TODO
- Request<T> request = createAmazonRequest(url);
- request.addHeader("X-Amz-Security-Token", sessionId);
- AWS4Signer signer = new AWS4Signer();
- signer.setRegionName("eu-west-2");
- signer.setServiceName(request.getServiceName());
- signer.sign(request, new BasicAWSCredentials(accessKey,
- secretKey));
- System.out.println("Request endpoint: " + request.getEndpoint());
- System.out.println("Request parameters: " + request.getParameters());
- ClientConfiguration clientConfiguration = new ClientConfiguration();
- boolean useProxySettings = false; // If you need to use proxy settings - set useProxySettings=true, and set
- // host, port, username and password values
- if (useProxySettings)
- {
- clientConfiguration.setProxyHost("some host");
- clientConfiguration.setProxyPort(1234); // some port
- clientConfiguration.setProxyUsername("some username");
- clientConfiguration.setProxyPassword("some password");
- }
- Response<AmazonWebServiceResponse> response = new AmazonHttpClient(clientConfiguration)
- .requestExecutionBuilder()
- .executionContext(new ExecutionContext())
- .request(request)
- .errorResponseHandler(new AmazonErrorHttpResponseHandler())
- .execute(new AmazonHttpResponseHandler());
- System.out.println("HTTP response status: " + response.getHttpResponse().getStatusCode());
- System.out.println(response.getAwsResponse());
- }
- private static Response<AmazonWebServiceResponse> sendAmazonAuthRequest() throws IOException
- {
- String authUrl = ""; // TODO
- Response<AmazonWebServiceResponse> authResponse;
- Request<T> authRequest = createAmazonRequest(authUrl);
- String jwtTokenPath = ""; // TODO
- String integrationId = ""; // TODO
- Path pathToToken = Paths.get(jwtTokenPath);
- Files.createDirectories(pathToToken.getParent());
- String token = FileUtils.readFileToString(pathToToken.toFile(), StandardCharsets.UTF_8).trim();
- System.out.println("Auth request: " + authRequest.getEndpoint());
- authRequest.addHeader("Authorization", "Bearer " + token);
- authRequest.addHeader("x-cmw-integration-id", integrationId);
- ClientConfiguration clientConfiguration = new ClientConfiguration();
- boolean useProxySettings = false; // If you need to use proxy settings - set useProxySettings=true, and set
- // host, port, username and password values
- if (useProxySettings)
- {
- clientConfiguration.setProxyHost("some host");
- clientConfiguration.setProxyPort(1234); // some port
- clientConfiguration.setProxyUsername("some username");
- clientConfiguration.setProxyPassword("some password");
- }
- authResponse = new AmazonHttpClient(clientConfiguration)
- .requestExecutionBuilder()
- .executionContext(new ExecutionContext())
- .request(authRequest)
- .errorResponseHandler(new AmazonErrorHttpResponseHandler())
- .execute(new AmazonHttpResponseHandler());
- return authResponse;
- }
- private static Request<T> createAmazonRequest(String url)
- {
- Request<T> request = new DefaultRequest<T>("execute-api");
- request.setHttpMethod(HttpMethodName.GET);
- request.setEndpoint(URI.create(url));
- return request;
- }
- public static class AmazonErrorHttpResponseHandler implements HttpResponseHandler<AmazonClientException>
- {
- @Override
- public AmazonClientException handle(HttpResponse response) throws Exception
- {
- String error = String.format("Error: %s (%s): %s", response.getStatusCode(),
- response.getStatusText(), IOUtils.toString(response.getContent()));
- System.out.println(error);
- return new AmazonClientException(error);
- }
- @Override
- public boolean needsConnectionLeftOpen()
- {
- return false;
- }
- }
- public static class AmazonHttpResponseHandler
- implements HttpResponseHandler<AmazonWebServiceResponse>
- {
- @Override
- public AmazonWebServiceResponse handle(HttpResponse response) throws Exception
- {
- AmazonWebServiceResponse<String> amazonWebServiceResponse = new AmazonWebServiceResponse<>();
- String content = IOUtils.toString(response.getContent());
- amazonWebServiceResponse.setResult(content);
- return amazonWebServiceResponse;
- }
- @Override
- public boolean needsConnectionLeftOpen()
- {
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment