ibragimova_mariam

Send request with auth

Jul 17th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. import com.amazonaws.*;
  2. import com.amazonaws.auth.AWS4Signer;
  3. import com.amazonaws.auth.BasicAWSCredentials;
  4. import com.amazonaws.http.*;
  5. import com.amazonaws.util.IOUtils;
  6. import org.apache.commons.io.FileUtils;
  7. import org.apache.poi.ss.formula.functions.T;
  8. import org.primefaces.json.JSONObject;
  9.  
  10. import java.io.IOException;
  11. import java.net.URI;
  12. import java.nio.charset.StandardCharsets;
  13. import java.nio.file.Files;
  14. import java.nio.file.Path;
  15. import java.nio.file.Paths;
  16.  
  17.  
  18. public class Main2
  19. {
  20. public static void main(String[] args) throws IOException
  21. {
  22. String accessKey, secretKey, sessionId;
  23. Response<AmazonWebServiceResponse> authResponse = sendAmazonAuthRequest();
  24. System.out.println("Auth http Response status: " + authResponse.getHttpResponse().getStatusCode());
  25. Object jsonAuthResult = authResponse.getAwsResponse().getResult();
  26. System.out.println("Auth response content: " + jsonAuthResult);
  27. JSONObject obj = new JSONObject(jsonAuthResult.toString());
  28. accessKey = obj.getString("accessKey");
  29. secretKey = obj.getString("secretKey");
  30. sessionId = obj.getString("sessionId");
  31.  
  32.  
  33. String url = ""; // TODO
  34. Request<T> request = createAmazonRequest(url);
  35. request.addHeader("X-Amz-Security-Token", sessionId);
  36.  
  37. AWS4Signer signer = new AWS4Signer();
  38. signer.setRegionName("eu-west-2");
  39. signer.setServiceName(request.getServiceName());
  40. signer.sign(request, new BasicAWSCredentials(accessKey,
  41. secretKey));
  42.  
  43. System.out.println("Request endpoint: " + request.getEndpoint());
  44. System.out.println("Request parameters: " + request.getParameters());
  45.  
  46.  
  47. ClientConfiguration clientConfiguration = new ClientConfiguration();
  48. boolean useProxySettings = false; // If you need to use proxy settings - set useProxySettings=true, and set
  49. // host, port, username and password values
  50. if (useProxySettings)
  51. {
  52. clientConfiguration.setProxyHost("some host");
  53. clientConfiguration.setProxyPort(1234); // some port
  54. clientConfiguration.setProxyUsername("some username");
  55. clientConfiguration.setProxyPassword("some password");
  56. }
  57.  
  58. Response<AmazonWebServiceResponse> response = new AmazonHttpClient(clientConfiguration)
  59. .requestExecutionBuilder()
  60. .executionContext(new ExecutionContext())
  61. .request(request)
  62. .errorResponseHandler(new AmazonErrorHttpResponseHandler())
  63. .execute(new AmazonHttpResponseHandler());
  64.  
  65. System.out.println("HTTP response status: " + response.getHttpResponse().getStatusCode());
  66. System.out.println(response.getAwsResponse());
  67. }
  68.  
  69. private static Response<AmazonWebServiceResponse> sendAmazonAuthRequest() throws IOException
  70. {
  71. String authUrl = ""; // TODO
  72. Response<AmazonWebServiceResponse> authResponse;
  73. Request<T> authRequest = createAmazonRequest(authUrl);
  74. String jwtTokenPath = ""; // TODO
  75. String integrationId = ""; // TODO
  76.  
  77. Path pathToToken = Paths.get(jwtTokenPath);
  78. Files.createDirectories(pathToToken.getParent());
  79. String token = FileUtils.readFileToString(pathToToken.toFile(), StandardCharsets.UTF_8).trim();
  80.  
  81. System.out.println("Auth request: " + authRequest.getEndpoint());
  82.  
  83. authRequest.addHeader("Authorization", "Bearer " + token);
  84. authRequest.addHeader("x-cmw-integration-id", integrationId);
  85.  
  86. ClientConfiguration clientConfiguration = new ClientConfiguration();
  87. boolean useProxySettings = false; // If you need to use proxy settings - set useProxySettings=true, and set
  88. // host, port, username and password values
  89. if (useProxySettings)
  90. {
  91. clientConfiguration.setProxyHost("some host");
  92. clientConfiguration.setProxyPort(1234); // some port
  93. clientConfiguration.setProxyUsername("some username");
  94. clientConfiguration.setProxyPassword("some password");
  95. }
  96.  
  97. authResponse = new AmazonHttpClient(clientConfiguration)
  98. .requestExecutionBuilder()
  99. .executionContext(new ExecutionContext())
  100. .request(authRequest)
  101. .errorResponseHandler(new AmazonErrorHttpResponseHandler())
  102. .execute(new AmazonHttpResponseHandler());
  103. return authResponse;
  104. }
  105.  
  106. private static Request<T> createAmazonRequest(String url)
  107. {
  108. Request<T> request = new DefaultRequest<T>("execute-api");
  109. request.setHttpMethod(HttpMethodName.GET);
  110. request.setEndpoint(URI.create(url));
  111. return request;
  112. }
  113.  
  114. public static class AmazonErrorHttpResponseHandler implements HttpResponseHandler<AmazonClientException>
  115. {
  116. @Override
  117. public AmazonClientException handle(HttpResponse response) throws Exception
  118. {
  119. String error = String.format("Error: %s (%s): %s", response.getStatusCode(),
  120. response.getStatusText(), IOUtils.toString(response.getContent()));
  121. System.out.println(error);
  122. return new AmazonClientException(error);
  123. }
  124.  
  125. @Override
  126. public boolean needsConnectionLeftOpen()
  127. {
  128. return false;
  129. }
  130. }
  131.  
  132. public static class AmazonHttpResponseHandler
  133. implements HttpResponseHandler<AmazonWebServiceResponse>
  134. {
  135.  
  136. @Override
  137. public AmazonWebServiceResponse handle(HttpResponse response) throws Exception
  138. {
  139. AmazonWebServiceResponse<String> amazonWebServiceResponse = new AmazonWebServiceResponse<>();
  140. String content = IOUtils.toString(response.getContent());
  141. amazonWebServiceResponse.setResult(content);
  142. return amazonWebServiceResponse;
  143. }
  144.  
  145. @Override
  146. public boolean needsConnectionLeftOpen()
  147. {
  148. return false;
  149. }
  150. }
  151. }
Add Comment
Please, Sign In to add comment