Guest User

Untitled

a guest
Nov 21st, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. package com;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.net.URL;
  6. import java.security.SecureRandom;
  7. import java.security.cert.Certificate;
  8. import java.security.cert.X509Certificate;
  9. import javax.net.ssl.HostnameVerifier;
  10. import javax.net.ssl.HttpsURLConnection;
  11. import javax.net.ssl.SSLContext;
  12. import javax.net.ssl.SSLSession;
  13. import javax.net.ssl.TrustManager;
  14. import javax.net.ssl.X509TrustManager;
  15. import org.apache.commons.codec.binary.Base64;
  16. import static java.lang.System.out;
  17.  
  18. import static com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile.printUsage;
  19.  
  20.  
  21. public class ociAuth {
  22. private static String server;
  23. private static String user;
  24. private static String password;
  25. private static String port = "8443";
  26. private static String response_format = "json";
  27. private static String server_url;
  28.  
  29.  
  30. public static void main(String[] args) {
  31. if(args.length < 3 || args.length > 4) {
  32. printUsage();
  33. System.exit(1);
  34. }
  35.  
  36. setUserArguments(args);
  37. server_url = "https://" + server + ":" + port + "/rest/v1/assets/storages";
  38. try {
  39. HttpsURLConnection connection =
  40. getAllTrustingHttpsUrlConnection();
  41. if(connection == null) {
  42. System.err.println("FATAL: Failed to create HTTPS connection to URL: " + server_url);
  43. System.exit(1);
  44. }
  45. System.out.println("Invoking API: " + server_url);
  46. connection.setRequestMethod("GET");
  47. connection.setRequestProperty("Accept", "application/" + response_format);
  48. String authString = getAuthorizationString();
  49. connection.setRequestProperty("Authorization", "Basic " +
  50. authString);
  51. if (connection.getResponseCode() != 200) {
  52. System.err.println("API Invocation Failed : HTTP error code : "
  53. + connection.getResponseCode());
  54. System.exit(1);
  55. }
  56. BufferedReader br = new BufferedReader(new InputStreamReader(
  57. (connection.getInputStream())));
  58. String response;
  59. System.out.println("Response:");
  60. while ((response = br.readLine()) != null) {
  61. System.out.println(response);
  62. }
  63. connection.disconnect();
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68.  
  69. //THESE PRINTS HERE
  70.  
  71. System.out.print("nUsage:ntHelloApiServices <api-server host[:port]> <user> <password> [json|xml]n");
  72. System.out.print("nExamples:ntHelloApiServices localhost admin mypassword");
  73. System.out.print("tHelloApiServices 10.22.12.34:8320 admin password");
  74. System.out.print("tHelloApiServices 10.22.12.34 admin password xml");
  75. System.out.print("tHelloApiServices 10.22.12.34:8212 admin password xmln");
  76. System.out.print("nNote:nt(1) When port number is not provided, 8443 is chosen by default.");
  77. System.out.print("t(2) When response format (json or xml) is not provided, json is chosen by default. n");
  78. }
  79. private static void setUserArguments(String[] args) {
  80. server = args[0];
  81. user = args[1];
  82. password = args[2];
  83. if(args.length == 4) {
  84. response_format = args[3];
  85. if(!response_format.equals("json") && ! response_format.equals("xml")) {
  86. printUsage();
  87. System.exit(1);
  88. }
  89. }
  90. if(server.contains(":")) {
  91. String[] parts = server.split(":");
  92. server = parts[0];
  93. port = parts[1];
  94. }
  95. }
  96.  
  97. private static HttpsURLConnection getAllTrustingHttpsUrlConnection() {
  98. HttpsURLConnection conn = null;
  99. try {
  100. TrustManager[] trustAllCertificatesManager = new
  101. TrustManager[]{new X509TrustManager() {
  102. public X509Certificate[] getAcceptedIssuers() {
  103. return
  104. null;
  105. }
  106.  
  107. public void checkClientTrusted(X509Certificate[]
  108. certs, String authType) {
  109. }
  110.  
  111. public void checkServerTrusted(X509Certificate[]
  112. certs, String authType) {
  113. }
  114. }};
  115.  
  116. SSLContext sslContext = SSLContext.getInstance("TLS");
  117. sslContext.init(null, trustAllCertificatesManager, new
  118. SecureRandom());
  119.  
  120. HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
  121. URL url = new URL(server_url);
  122. conn = (HttpsURLConnection) url.openConnection();
  123. conn.setHostnameVerifier(new HostnameVerifier() {
  124. public boolean verify(String host, SSLSession
  125. session) {
  126. return true;
  127. }
  128. });
  129. } catch (Exception e) {
  130. e.printStackTrace();
  131. }
  132. return conn;
  133. }
  134. private static String getAuthorizationString() {
  135. String userPassword = user + ":" + password;
  136. byte[] authEncodedBytes =
  137. Base64.encodeBase64(userPassword.getBytes());
  138. String ajdeovako = new String(authEncodedBytes);
  139. return ajdeovako;
  140. }
  141. }
Add Comment
Please, Sign In to add comment