Java Code Sample
Prerequisites:
-
Install Apache HTTP Client (v4.5.6) and GSON (v2.8.2) libraries and their dependencies in your class path before attempting to build and run the provided sample code .
-
Set the
SERVER_IP_ADDRESS
constant to the value of your Oracle ILOM SP IP address before compiling the sample code.
Note:
This Java sample code was tested on Java v1.8.
Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
/
package com.oracle.ssm.ilomrestapi.client;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
public class IlomRestApiClient {
public static final String SERVER_IP_ADDRESS = <IP_address>;
public static final String SERVER_PORT = "443";
public static final String ILOM_ROOT_PATH = "/rest/v1";
public static final String ILOM_ROOT_URI = "https://" + SERVER_IP_ADDRESS + ":" + SERVER_PORT + ILOM_ROOT_PATH;
public static final String URI_PATH_LOGIN = "/login";
public static final String ILOM_URI_LOGIN = ILOM_ROOT_URI + URI_PATH_LOGIN;
public static final String URI_PATH_LOGOUT = "/logout";
public static final String ILOM_URI_LOGOUT = ILOM_ROOT_URI + URI_PATH_LOGOUT;
public static final String URI_PATH_SYSTEM = "/System";
public static final String ILOM_URI_SYSTEM = ILOM_ROOT_URI + URI_PATH_SYSTEM;
public static final String URI_PATH_SP_USERS = "/SP/users";
public static final String ILOM_URI_SP_USERS = ILOM_ROOT_URI + URI_PATH_SP_USERS;
public static final String URI_PATH_ELWOOD_USER = "/Elwood";
public static final String ILOM_URI_ELWOOD_USER = ILOM_URI_SP_USERS + URI_PATH_ELWOOD_USER;
public static final String ACCEPT_HEADER = "Accept";
public static final String CONTENT_TYPE_HEADER = "Content-Type";
public static final String JSON_MEDIA_TYPE = "application/json";
public static final String TOKEN_HEADER = "X-Rest-Token";
private String token;
private CloseableHttpClient getHttpClient(boolean useCreds) throws Exception {
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(new TrustSelfSignedStrategy())
.build();
HostnameVerifier hostNameVerifier = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
SSLConnectionSocketFactory socketFactory =
new SSLConnectionSocketFactory(sslContext,
new String[] { "TLSv1.2" }, null,
hostNameVerifier);
if (useCreds) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(SERVER_IP_ADDRESS, Integer.parseInt(SERVER_PORT)),
new UsernamePasswordCredentials("root", "changeme"));
return HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setSSLSocketFactory(socketFactory)
.build();
} else {
return HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.build();
}
}