11package edu .ucdavis .coeitss ;
22
3+ import java .net .URI ;
4+ import java .net .http .HttpClient ;
5+ import java .net .http .HttpRequest ;
6+ import java .net .http .HttpRequest .BodyPublishers ;
7+ import java .net .http .HttpResponse ;
8+
9+ import com .fasterxml .jackson .databind .JsonNode ;
10+ import com .fasterxml .jackson .databind .ObjectMapper ;
11+
12+ import io .github .cdimascio .dotenv .Dotenv ;
13+
14+
315public class Main {
4- public static void main (String [] args ) {
5- System .out .println ("Hello world!" );
16+ public static void main (String [] args ) throws Exception {
17+
18+ //Load the .env File
19+ Dotenv dotenv = Dotenv .load ();
20+
21+ //Initiate Instance Rosetta API Info and Load Required Values
22+ RosettaAPIInfo rosettaAPIInfo = new RosettaAPIInfo ();
23+ rosettaAPIInfo .base_url = dotenv .get ("ROSETTA_BASE_URL" );
24+ rosettaAPIInfo .client_id = dotenv .get ("ROSETTA_CLIENT_ID" );
25+ rosettaAPIInfo .client_secret = dotenv .get ("ROSETTA_CLIENT_SECRET" );
26+ rosettaAPIInfo .token_url = dotenv .get ("ROSETTA_OAUTH_URL" );
27+ rosettaAPIInfo .test_id = dotenv .get ("ROSETTA_TEST_ID" );
28+
29+ //Check for Required Client ID and Secret Before Making API Calls
30+ if (rosettaAPIInfo .client_id .isEmpty () == false && rosettaAPIInfo .client_secret .isEmpty () == false )
31+ {
32+
33+ //##########################################
34+ //Retreiving OAuth Token
35+ //##########################################
36+
37+ //HttpClient for API Call to Rosetta API
38+ HttpClient raHttpClient = HttpClient .newHttpClient ();
39+
40+ //Initiate Object Mapper to Parse Returned Json
41+ ObjectMapper joMapper = new ObjectMapper ();
42+
43+ //Build Request with Custom Header for OAuth Call
44+ HttpRequest raHttpRequest = HttpRequest .newBuilder ()
45+ .uri (URI .create (rosettaAPIInfo .token_url ))
46+ .header ("client_id" , rosettaAPIInfo .client_id )
47+ .header ("client_secret" , rosettaAPIInfo .client_secret )
48+ .header ("grant_type" ,"CLIENT_CREDENTIALS" )
49+ .POST (BodyPublishers .noBody ())
50+ .build ();
51+
52+ //Send Request via HTTP Client
53+ HttpResponse <String > raHttpResponse = raHttpClient .send (raHttpRequest , HttpResponse .BodyHandlers .ofString ());
54+
55+ //Create Json Object of Returned Json
56+ JsonNode jnOAuthToken = joMapper .readTree (raHttpResponse .body ());
57+
58+ //Load OAuth Access Token
59+ rosettaAPIInfo .oauth_token = jnOAuthToken .get ("access_token" ).asText ();
60+
61+ //Check on Returned OAuth Access Token
62+ if (rosettaAPIInfo .oauth_token .isEmpty () == false )
63+ {
64+
65+ //########################################
66+ // Viewing Accounts Endpoint Information
67+ //########################################
68+
69+ //Var for Accounts URL
70+ String accountsURL = rosettaAPIInfo .base_url + "accounts?iamid=" + rosettaAPIInfo .test_id ;
71+
72+ //Build Request for Accounts Lookup
73+ HttpRequest accntsHttpRequest = HttpRequest .newBuilder ()
74+ .uri (URI .create (accountsURL ))
75+ .header ("Authorization" ,"Bearer " + rosettaAPIInfo .oauth_token )
76+ .GET ()
77+ .build ();
78+
79+ //Send Accounts Request
80+ HttpResponse <String > accntsHttpResponse = raHttpClient .send (accntsHttpRequest , HttpResponse .BodyHandlers .ofString ());
81+
82+ //Create Json Object of Accounts Json Data
83+ JsonNode jnAccountsData = joMapper .readTree (accntsHttpResponse .body ());
84+
85+ //Loop Through Accounts Information
86+ for (JsonNode jnAccount : jnAccountsData )
87+ {
88+
89+ if (jnAccount .get ("AccountName" ).asText ().equalsIgnoreCase ("UCPath Position Entitlement" ) == true )
90+ {
91+ String prettyJson = joMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (jnAccount );
92+ System .out .println (prettyJson );
93+ }
94+
95+ }//End of jnAccountData For
96+
97+
98+
99+
100+ }//End of OAuth Access Token Empty Check
101+
102+ }//End of Empty Checks on Client ID and Secret
103+
6104 }
7105}
0 commit comments