Sample Java Code
The following examples demonstrate how to access the Oracle Prime Projects API using Java. The first example sends an HTTP GET request to the /workspace/ endpoint to retrieve information about a workspace with a given workspace ID. The second example sends an HTTP POST request to the /project endpoint to create a project. Both examples use basic authentication.
Sample Program to Invoke GET REST API
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
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.httpclient.HttpStatus;
public class SampleProgram {
private static String userName = "<username>";
private static String password = "<password>";
private static String hostName = "<hostname>";
private static String portNumber = "<portnumber>";
private static String workspaceId = "<workspaceId>";
private static String version = "2";
/*
* Note: if hostName does not contain portNumber, remove below '+ ":" + portNumber' from getWorkspaceUrl
*/
private static String getWorkspaceUrl = "https://" + hostName + ":" + portNumber + "/primeapi/restapi/workspace/" + workspaceId;
public static void main(String s[]) throws Exception {
/*
* Invoke the API to get information about a wokrspace matching the workspaceId
*/
String workspaceJson = callRestURL(getWorkspaceUrl, "GET", version);
System.out.println("Workspace:- " + workspaceJson);
}
/*
* Supporting Methods
*/
private static String callRestURL(String restUrl, String method, String version) throws Exception {
HttpURLConnection conn = null;
try {
trustAllCert();
URL url = new URL(restUrl);
/*
* Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("<proxyhost>", <port>));
* conn = (HttpURLConnection) url.openConnection(proxy);
*/
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(method);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Version", version);
String userCredentials = userName + ":" + password;
String base64Credentials = javax.xml.bind.DatatypeConverter.printBase64Binary(userCredentials.getBytes());
String basicAuth = "Basic " + base64Credentials;
conn.setRequestProperty("Authorization", basicAuth);
if (conn.getResponseCode() != HttpStatus.SC_OK) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode() + "Error: " + readStreamData(conn.getErrorStream()));
}
return readStreamData(conn.getInputStream());
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private static String readStreamData(InputStream is) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String output;
StringBuilder buff = new StringBuilder();
while ((output = br.readLine()) != null) {
buff.append(output);
}
return buff.toString();
}
}
private static void trustAllCert() throws NoSuchAlgorithmException, KeyManagementException {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}
}
Sample Program to Invoke POST REST API
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
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;
public class SamplePostProgram {
private static String userName = "<username>";
private static String password = "<password>";
private static String hostName = "<hostname>";
private static String portNumber = "<portnumber>";
private static String version = "2";
/*
* Note: if hostName does not contain portNumber, remove below '+ ":" + portNumber' from projectUrl
*/
private static String projectUrl = "https://" + hostName + ":" + portNumber + "/primeapi/restapi/project";
public static void main(String s[]) throws Exception {
/*
* Invoke the API to create a project using the provided JSON data
*/
String projectJson = callRestURL(projectUrl, "POST", version);
System.out.println("Project:- " + projectJson);
}
/*
* Supporting Methods
*/
private static String callRestURL(String restUrl, String method, String version) throws Exception {
HttpURLConnection conn = null;
try {
trustAllCert();
URL url = new URL(restUrl);
/*
* Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("<proxyhost>", <port>));
* conn = (HttpURLConnection) url.openConnection(proxy);
*/
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(method);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Version", version);
String userCredentials = userName + ":" + password;
String base64Credentials = javax.xml.bind.DatatypeConverter.printBase64Binary(userCredentials.getBytes());
String basicAuth = "Basic " + base64Credentials;
conn.setRequestProperty("Authorization", basicAuth);
String input = "{\"projectCode\":\"P62111\", \"calendarId\":0, \"status\":\"ACTIVE\", \"projectName\":\"P15\",\"workspaceId\":2001, \"pegRate\":12}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes("UTF-8"));
os.flush();
if (conn.getResponseCode() != 200 && conn.getResponseCode() != 201 && conn.getResponseCode() != 204 && conn.getResponseCode() != 202
&& conn.getResponseCode() != 203) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode() + " Error: " + readStreamData(conn.getErrorStream()));
}
return readStreamData(conn.getInputStream());
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private static String readStreamData(InputStream is) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String output;
StringBuilder buff = new StringBuilder();
while ((output = br.readLine()) != null) {
buff.append(output);
}
return buff.toString();
}
}
private static void trustAllCert() throws NoSuchAlgorithmException, KeyManagementException {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}
}