Sample Java Code that Invokes the API for On-Premises

The following code is a generic example that demonstrates how to establish a session and use the returned session ID to retrieve business flows.This example uses basic authentication.

private static String userName = "GatewayAdmin"; 
      private static String password = "Primavera1"; 
      private static String hostName = "localhost"; 
      private static String portNumber = "7001"; 
      private static String getSessionUrl = "http://"+hostName+":"+portNumber+"/gatewayapi/restapisession/usersession"; 
      private static String getAppsUrl = "http://"+hostName+":"+portNumber+"/gatewayapi/restapi/v1/flows/list"; 
      private static String jsessionId; 
       
           public static void main(String s[]) throws Exception{ 
               try { 
                   //Call session api 
                   String sessionJson = callRestURL(getSessionUrl,"GET"); 
       
                   //Convert the JSON to java object 
                   ObjectMapper mapper = new ObjectMapper(); 
                   Session session = mapper.readValue(sessionJson, Session.class); 
                   //Store session id 
                   jsessionId = session.getSessionId();<< 
       
                   // Get a list of Flow objects 
                   String appsJson=callRestURL(getFlowsUrl,"GET"); 
       
                   //End the session object once all the operations are done 
                   String deleteJson = 
      callRestURL(getSessionUrl+";JSESSIONID="+session.getSessionId(),"DELETE"); 
       
               } catch (Exception e) { 
                   e.printStackTrace(); 
                   throw e; 
               } 
           } 
       
           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"); 
                   if(jsessionId != null){ 
                       conn.setRequestProperty("Cookie", "JSESSIONID=" 
                               + jsessionId); 
                   } 
       
                   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());} 
       
                   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(); 
               } 
           }