Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.security.SecureRandom;
- import java.security.cert.Certificate;
- import java.security.cert.X509Certificate;
- import javax.net.ssl.HostnameVerifier;
- import javax.net.ssl.HttpsURLConnection;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSession;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import org.apache.commons.codec.binary.Base64;
- import static java.lang.System.out;
- import static com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile.printUsage;
- public class ociAuth {
- private static String server;
- private static String user;
- private static String password;
- private static String port = "8443";
- private static String response_format = "json";
- private static String server_url;
- public static void main(String[] args) {
- if(args.length < 3 || args.length > 4) {
- printUsage();
- System.exit(1);
- }
- setUserArguments(args);
- server_url = "https://" + server + ":" + port + "/rest/v1/assets/storages";
- try {
- HttpsURLConnection connection =
- getAllTrustingHttpsUrlConnection();
- if(connection == null) {
- System.err.println("FATAL: Failed to create HTTPS connection to URL: " + server_url);
- System.exit(1);
- }
- System.out.println("Invoking API: " + server_url);
- connection.setRequestMethod("GET");
- connection.setRequestProperty("Accept", "application/" + response_format);
- String authString = getAuthorizationString();
- connection.setRequestProperty("Authorization", "Basic " +
- authString);
- if (connection.getResponseCode() != 200) {
- System.err.println("API Invocation Failed : HTTP error code : "
- + connection.getResponseCode());
- System.exit(1);
- }
- BufferedReader br = new BufferedReader(new InputStreamReader(
- (connection.getInputStream())));
- String response;
- System.out.println("Response:");
- while ((response = br.readLine()) != null) {
- System.out.println(response);
- }
- connection.disconnect();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //THESE PRINTS HERE
- System.out.print("nUsage:ntHelloApiServices <api-server host[:port]> <user> <password> [json|xml]n");
- System.out.print("nExamples:ntHelloApiServices localhost admin mypassword");
- System.out.print("tHelloApiServices 10.22.12.34:8320 admin password");
- System.out.print("tHelloApiServices 10.22.12.34 admin password xml");
- System.out.print("tHelloApiServices 10.22.12.34:8212 admin password xmln");
- System.out.print("nNote:nt(1) When port number is not provided, 8443 is chosen by default.");
- System.out.print("t(2) When response format (json or xml) is not provided, json is chosen by default. n");
- }
- private static void setUserArguments(String[] args) {
- server = args[0];
- user = args[1];
- password = args[2];
- if(args.length == 4) {
- response_format = args[3];
- if(!response_format.equals("json") && ! response_format.equals("xml")) {
- printUsage();
- System.exit(1);
- }
- }
- if(server.contains(":")) {
- String[] parts = server.split(":");
- server = parts[0];
- port = parts[1];
- }
- }
- private static HttpsURLConnection getAllTrustingHttpsUrlConnection() {
- HttpsURLConnection conn = null;
- try {
- TrustManager[] trustAllCertificatesManager = new
- TrustManager[]{new X509TrustManager() {
- public X509Certificate[] getAcceptedIssuers() {
- return
- null;
- }
- public void checkClientTrusted(X509Certificate[]
- certs, String authType) {
- }
- public void checkServerTrusted(X509Certificate[]
- certs, String authType) {
- }
- }};
- SSLContext sslContext = SSLContext.getInstance("TLS");
- sslContext.init(null, trustAllCertificatesManager, new
- SecureRandom());
- HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
- URL url = new URL(server_url);
- conn = (HttpsURLConnection) url.openConnection();
- conn.setHostnameVerifier(new HostnameVerifier() {
- public boolean verify(String host, SSLSession
- session) {
- return true;
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- return conn;
- }
- private static String getAuthorizationString() {
- String userPassword = user + ":" + password;
- byte[] authEncodedBytes =
- Base64.encodeBase64(userPassword.getBytes());
- String ajdeovako = new String(authEncodedBytes);
- return ajdeovako;
- }
- }
Add Comment
Please, Sign In to add comment