Sample Java Code that Invokes the API

The following code is a generic example that demonstrates how to invoke the API to get information about a project with a project ID of 109. This example uses basic authentication.

Sample Program to Invoke GET REST API

	// 
Declare Packages 
      	package com.oracle.pgbu.api.rest.sample; 
      	import java.io.BufferedReader; 
      	import java.io.InputStreamReader; 
      	import java.net.HttpURLConnection; 
      	import java.net.URL; 
      	 
       
      	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 projectId = "109"; 
       
      		private static String getProjectUrl = "http://" + hostName + ":" + portNumber + "/primeapi/restapi/v16.2/project/" + projectId; 
       
      		public static void main(String s[]) throws Exception { 
       
      			// Invoke the API to get information about a project with a project ID of 109 
       
      			String projectJson = callRestURL(getProjectUrl, "GET"); 
       
      			System.out.println("Project:- " + projectJson); 
       
      	} 
       
      	// 
Supporting Methods 
       
      	private static String callRestURL(String restUrl, String method) throws Exception { 
       
      		HttpURLConnection conn = null; 
       
      		try { 
       
      			URL url = new URL(restUrl); 
       
      			conn = (HttpURLConnection) url.openConnection(); 
       
      			conn.setRequestMethod(method); 
       
      			conn.setRequestProperty("Accept", "application/json"); 
       
      			String userCredentials = userName + ":" + password; 
       
      			String base64Credentials = javax.xml.bind.DatatypeConverter.printBase64Binary(userCredentials.getBytes()); 
       
      			String basicAuth = "Basic " + base64Credentials; 
       
      			conn.setRequestProperty("Authorization", basicAuth); 
       
      			if (conn.getResponseCode() != 200) { 
       
      				throw new RuntimeException("Failed : HTTP error code : " 
       
      					+ conn.getResponseCode()); 
       
      			} 
       
      			try (BufferedReader br = new BufferedReader(new InputStreamReader( 
       
      				(conn.getInputStream())))) { 
       
      				String output; 
       
      				StringBuilder buff = new StringBuilder(); 
       
      				while ((output = br.readLine()) != null) { 
       
      					buff.append(output); 
       
      				} 
       
      				return buff.toString(); 
       
      				} 
       
      			} finally { 
       
      				if (conn != null) { 
      					conn.disconnect(); 
      				} 
       
      			} 
       
      		} 
       
      	}

Sample Program to Invoke POST REST API

// 
Declare Packages 
      package com.helloweb.resource; 
      import java.io.BufferedReader 
      import java.io.InputStreamReader; 
      import java.io.OutputStream; 
      import java.net.HttpURLConnection; 
      import java.net.URL; 
       
      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 getProjectUrl = "http://" + hostName + ":" + portNumber + "/primeapi/restapi/v16.2/project"; 
       
          public static void main(String s[]) throws Exception { 
       
      // Invoke the API to get information about a project with a project ID of 109 
       
              String projectJson = callRestURL(getProjectUrl, "POST"); 
       
              System.out.println("Project:- " + projectJson); 
       
          } 
       
      // 
Supporting Methods 
       
          private static String callRestURL(String restUrl, String method) throws Exception { 
       
              HttpURLConnection conn = null; 
       
              try { 
       
                  URL url = new URL(restUrl); 
       
                  conn = (HttpURLConnection) url.openConnection(); 
       
                  conn.setDoOutput(true); 
                  conn.setDoInput(true); 
       
                  conn.setRequestMethod(method); 
       
                  conn.setRequestProperty("Accept", "application/json"); 
       
                  conn.setRequestProperty("Content-Type", "application/json"); 
       
                  String userCredentials = userName + ":" + password; 
       
                  String base64Credentials = javax.xml.bind.DatatypeConverter.printBase64Binary(userCredentials.getBytes()); 
       
                  String basicAuth = "Basic " + base64Credentials; 
       
                  conn.setRequestProperty("Authorization", basicAuth); 
       
                  String input = "{\"projectCode\":\"P51\",\"projectName\":\"P5\",\"workspaceId\":1}"; 
       
                  OutputStream os = conn.getOutputStream(); 
                  os.write(input.getBytes("UTF-8")); 
                  os.flush(); 
       
                  if (conn.getResponseCode() != 200) { 
       
                      throw new RuntimeException("Failed : HTTP error code : " 
       
                              + conn.getResponseCode() + " Error: " + conn.getErrorStream()); 
       
                  } 
       
                  try (BufferedReader br = new BufferedReader(new InputStreamReader( 
       
                          (conn.getInputStream())))) { 
       
                      String output; 
       
                      StringBuilder buff = new StringBuilder(); 
       
                      while ((output = br.readLine()) != null) { 
       
                          buff.append(output); 
       
                      } 
       
                      return buff.toString(); 
       
                  } 
       
              } finally { 
       
                  if (conn != null) { 
                      conn.disconnect(); 
                  } 
       
              } 
       
          } 
       
      }