-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.java
More file actions
178 lines (132 loc) · 5.72 KB
/
Copy pathConnection.java
File metadata and controls
178 lines (132 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package javaxt.azure.graph;
import java.util.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import static java.nio.charset.StandardCharsets.*;
import javaxt.json.*;
import static javaxt.utils.Console.console;
public class Connection {
//Azure end-points
private String loginURL = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token";
private String graphURL = "https://graph.microsoft.com/v1.0";
//Static properties
private String tenantID;
private String clientID;
private String clientSecret;
//Transient properties
private String tokenType;
private String accessToken;
private javaxt.utils.Date expirationDate;
//**************************************************************************
//** Constructor
//**************************************************************************
public Connection(String tenantID, String clientID, String clientSecret) throws Exception {
this.tenantID = tenantID;
this.clientID = clientID;
this.clientSecret = clientSecret;
connect();
}
//**************************************************************************
//** connect
//**************************************************************************
private void connect() throws Exception {
console.log("connecting...");
String loginURL = this.loginURL.replace("{tenant}", tenantID);
javaxt.http.Request request = new javaxt.http.Request(loginURL);
request.setNumRedirects(0);
String payload = "client_id=" +clientID +
"&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default" +
"&client_secret=" + clientSecret +
"&grant_type=client_credentials";
//request.setHeader(); application/x-www-form-urlencoded
request.write(payload);
javaxt.http.Response response = request.getResponse();
JSONObject json = response.getJSONObject();
if (response.getStatus()==200){
tokenType = json.get("token_type").toString();
accessToken = json.get("access_token").toString();
Integer expiresIn = json.get("expires_in").toInteger();
expirationDate = new javaxt.utils.Date().add(expiresIn, "seconds");
console.log("Connected! token expires " + expirationDate);
}
else{
System.out.println(response.toString());
System.out.println(json.toString(4));
throw new Exception();
}
}
//**************************************************************************
//** getExpiration
//**************************************************************************
public javaxt.utils.Date getExpirationDate(){
return expirationDate;
}
//**************************************************************************
//** getResponse
//**************************************************************************
public JSONObject getResponse(String url) throws Exception {
return getResponse(url, null);
}
//**************************************************************************
//** getResponse
//**************************************************************************
public JSONObject getResponse(String url, JSONObject payload) throws Exception {
return getResponse(url, payload, null);
}
//**************************************************************************
//** getResponse
//**************************************************************************
public JSONObject getResponse(String url, JSONObject payload, String method) throws Exception {
//Get request builder
HttpRequest.Builder request = getRequest(url);
//Set payload and method as needed
if (payload!=null){
if (method==null) method = "POST";
else method = method.toUpperCase();
request.header("Content-Type", "application/json;charset=UTF-8");
request.method(method, HttpRequest.BodyPublishers.ofByteArray(
payload.toString().getBytes(UTF_8))
);
}
//Get response
HttpResponse<String> response = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build()
.send(request.build(), HttpResponse.BodyHandlers.ofString(UTF_8));
//Parse and return response
JSONObject json = new JSONObject(response.body());
int status = response.statusCode();
if (status==200){
return json;
}
else if (status==429){
Thread.sleep(1500);
return getResponse(url);
}
else{
//console.log(response.headers().map());
//System.out.println(response.toString());
System.out.println(json.toString(4));
throw new Exception();
}
}
//**************************************************************************
//** getRequest
//**************************************************************************
private synchronized HttpRequest.Builder getRequest(String url) throws Exception {
//Update url as needed
if (!url.startsWith(graphURL) && !url.startsWith("http")){
if (!url.startsWith("/")) url = "/" + url;
url = graphURL + url;
}
//Refresh tokens as needed
var timeRemaining = expirationDate.compareTo(new javaxt.utils.Date(), "seconds");
//console.log("timeRemaining: " + timeRemaining + " seconds");
if (timeRemaining<60) connect();
//Execute http request and return response
return HttpRequest.newBuilder()
.uri(java.net.URI.create(url))
.header("Authorization", tokenType + " " + accessToken);
}
}