Skip to content

Commit e31ff64

Browse files
committed
Initial commit
0 parents  commit e31ff64

File tree

11 files changed

+1098
-0
lines changed

11 files changed

+1098
-0
lines changed

Diff for: .gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build/
2+
dist/
3+
target/
4+
nbproject/
5+
build.xml
6+
manifest.mf

Diff for: LICENSE.TXT

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2006-2023, Peter Borissow
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# JavaXT Azure
2+
The javaxt-azure library is used to connect to a Microsoft Azure instance via the
3+
graph API.
4+
5+
6+
## License
7+
All JavaXT libraries are free and open source released under a permissive MIT license.
8+
This software comes with no guarantees or warranties. You may use this software in any open source or commercial project.

Diff for: pom.xml

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<properties>
5+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6+
<maven.compiler.source>1.8</maven.compiler.source>
7+
<maven.compiler.target>1.8</maven.compiler.target>
8+
9+
<!-- Ant-style directory layout -->
10+
<src.dir>src</src.dir>
11+
12+
</properties>
13+
14+
<!-- =========================================================== -->
15+
<!-- Module Description -->
16+
<!-- =========================================================== -->
17+
<groupId>javaxt</groupId>
18+
<artifactId>javaxt-azure</artifactId>
19+
<version>dev</version>
20+
<packaging>jar</packaging>
21+
22+
23+
<!-- =========================================================== -->
24+
<!-- Dependency Management -->
25+
<!-- =========================================================== -->
26+
<repositories>
27+
<repository>
28+
<id>javaxt.com</id>
29+
<url>https://www.javaxt.com/maven</url>
30+
</repository>
31+
</repositories>
32+
<dependencies>
33+
<dependency>
34+
<groupId>javaxt</groupId>
35+
<artifactId>javaxt-core</artifactId>
36+
<version>2.1.2</version>
37+
</dependency>
38+
</dependencies>
39+
40+
41+
42+
<!-- =========================================================== -->
43+
<!-- Build Info -->
44+
<!-- =========================================================== -->
45+
<build>
46+
47+
<sourceDirectory>${src.dir}</sourceDirectory>
48+
49+
50+
<plugins>
51+
52+
<!-- Copy dependencies into the lib folder -->
53+
<plugin>
54+
<artifactId>maven-dependency-plugin</artifactId>
55+
<executions>
56+
<execution>
57+
<phase>install</phase>
58+
<goals>
59+
<goal>copy-dependencies</goal>
60+
</goals>
61+
<configuration>
62+
<outputDirectory>${project.build.directory}/lib</outputDirectory>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
68+
69+
<!-- Create main app -->
70+
<plugin>
71+
<artifactId>maven-jar-plugin</artifactId>
72+
<configuration>
73+
<archive>
74+
<manifest>
75+
<addClasspath>true</addClasspath>
76+
<classpathPrefix>lib/</classpathPrefix>
77+
<mainClass>javaxt.azure.graph.Main</mainClass>
78+
</manifest>
79+
</archive>
80+
</configuration>
81+
</plugin>
82+
83+
84+
<!-- copy jars to the dist directory -->
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-antrun-plugin</artifactId>
88+
<version>1.8</version>
89+
<executions>
90+
<execution>
91+
<phase>install</phase>
92+
<configuration>
93+
<target>
94+
<move file="${project.build.directory}/${project.artifactId}-${project.version}.jar" todir="${project.basedir}/dist" />
95+
<move todir="${project.basedir}/dist/lib" >
96+
<fileset dir="${project.build.directory}/lib" />
97+
</move>
98+
</target>
99+
</configuration>
100+
<goals>
101+
<goal>run</goal>
102+
</goals>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
107+
108+
109+
</plugins>
110+
</build>
111+
</project>

Diff for: src/javaxt/azure/graph/Calendar.java

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package javaxt.azure.graph;
2+
3+
import java.util.*;
4+
import javaxt.json.*;
5+
6+
7+
public class Calendar extends Node {
8+
9+
private final String userID;
10+
11+
//**************************************************************************
12+
//** Constructor
13+
//**************************************************************************
14+
public Calendar(JSONObject json, String userID, Connection conn){
15+
super(json, conn);
16+
this.userID = userID;
17+
}
18+
19+
//**************************************************************************
20+
//** isDefault
21+
//**************************************************************************
22+
public boolean isDefault(){
23+
return get("isDefaultCalendar").toBoolean();
24+
}
25+
26+
27+
//**************************************************************************
28+
//** getEvents
29+
//**************************************************************************
30+
public ArrayList<Event> getEvents(Integer limit) throws Exception {
31+
ArrayList<Event> events = new ArrayList<>();
32+
33+
String calendarID = getID();
34+
String url = "/users/" + userID + "/calendars/"+calendarID+"/events";
35+
HashSet<String> params = new HashSet<>();
36+
if (limit!=null) params.add("$top=" + limit);
37+
params.add("$count=true");
38+
39+
if (!params.isEmpty()){
40+
url += "?";
41+
for (String param : params){
42+
url += "&" + param;
43+
}
44+
}
45+
46+
47+
JSONObject json = conn.getResponse(url);
48+
for (JSONValue v : json.get("value").toJSONArray()){
49+
events.add(new Event(v.toJSONObject(), conn));
50+
}
51+
52+
return events;
53+
}
54+
55+
56+
//**************************************************************************
57+
//** Event Class
58+
//**************************************************************************
59+
public class Event extends Node {
60+
public Event(JSONObject json, Connection conn){
61+
super(json, conn);
62+
}
63+
64+
public javaxt.utils.Date getStartDate(){
65+
try{
66+
return new javaxt.utils.Date(get("start").get("dateTime").toString());
67+
}
68+
catch(Exception e){
69+
return null;
70+
}
71+
}
72+
}
73+
}

Diff for: src/javaxt/azure/graph/Connection.java

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package javaxt.azure.graph;
2+
3+
import javaxt.json.*;
4+
import static javaxt.utils.Console.console;
5+
6+
public class Connection {
7+
8+
//Azure end-points
9+
private String loginURL = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token";
10+
private String graphURL = "https://graph.microsoft.com/v1.0";
11+
12+
13+
//Static properties
14+
private String tenantID;
15+
private String clientID;
16+
private String clientSecret;
17+
18+
19+
//Transient properties
20+
private String tokenType;
21+
private String accessToken;
22+
private javaxt.utils.Date expirationDate;
23+
24+
25+
//**************************************************************************
26+
//** Constructor
27+
//**************************************************************************
28+
public Connection(String tenantID, String clientID, String clientSecret) throws Exception {
29+
this.tenantID = tenantID;
30+
this.clientID = clientID;
31+
this.clientSecret = clientSecret;
32+
connect();
33+
}
34+
35+
36+
//**************************************************************************
37+
//** connect
38+
//**************************************************************************
39+
private void connect() throws Exception {
40+
console.log("connecting...");
41+
42+
String loginURL = this.loginURL.replace("{tenant}", tenantID);
43+
javaxt.http.Request request = new javaxt.http.Request(loginURL);
44+
request.setNumRedirects(0);
45+
46+
String payload = "client_id=" +clientID +
47+
"&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default" +
48+
"&client_secret=" + clientSecret +
49+
"&grant_type=client_credentials";
50+
51+
//request.setHeader(); application/x-www-form-urlencoded
52+
request.write(payload);
53+
54+
javaxt.http.Response response = request.getResponse();
55+
JSONObject json = response.getJSONObject();
56+
57+
58+
if (response.getStatus()==200){
59+
tokenType = json.get("token_type").toString();
60+
accessToken = json.get("access_token").toString();
61+
Integer expiresIn = json.get("expires_in").toInteger();
62+
expirationDate = new javaxt.utils.Date().add(expiresIn, "seconds");
63+
64+
console.log("Connected! token expires " + expirationDate);
65+
}
66+
else{
67+
System.out.println(response.toString());
68+
System.out.println(json.toString(4));
69+
throw new Exception();
70+
}
71+
}
72+
73+
74+
//**************************************************************************
75+
//** getExpiration
76+
//**************************************************************************
77+
public javaxt.utils.Date getExpirationDate(){
78+
return expirationDate;
79+
}
80+
81+
82+
//**************************************************************************
83+
//** getResponse
84+
//**************************************************************************
85+
public JSONObject getResponse(String url) throws Exception {
86+
javaxt.http.Response response = getRequest(url).getResponse();
87+
JSONObject json = response.getJSONObject();
88+
int status = response.getStatus();
89+
if (status==200){
90+
return json;
91+
}
92+
else if (status==429){
93+
Thread.sleep(1500);
94+
return getResponse(url);
95+
}
96+
else{
97+
System.out.println(response.toString());
98+
System.out.println(json.toString(4));
99+
throw new Exception();
100+
}
101+
}
102+
103+
104+
//**************************************************************************
105+
//** get
106+
//**************************************************************************
107+
private synchronized javaxt.http.Request getRequest(String url) throws Exception {
108+
109+
//Update url as needed
110+
if (!url.startsWith(graphURL) && !url.startsWith("http")){
111+
if (!url.startsWith("/")) url = "/" + url;
112+
url = graphURL + url;
113+
}
114+
115+
116+
//Refresh tokens as needed
117+
var timeRemaining = expirationDate.compareTo(new javaxt.utils.Date(), "seconds");
118+
//console.log("timeRemaining: " + timeRemaining + " seconds");
119+
if (timeRemaining<60) connect();
120+
121+
122+
//Execute http request and return response
123+
javaxt.http.Request request = new javaxt.http.Request(url);
124+
request.setHeader("Authorization", tokenType + " " + accessToken);
125+
request.setNumRedirects(0);
126+
return request;
127+
}
128+
129+
}

Diff for: src/javaxt/azure/graph/Main.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package javaxt.azure.graph;
2+
3+
import java.util.*;
4+
import javaxt.json.*;
5+
import static javaxt.utils.Console.console;
6+
7+
8+
public class Main {
9+
10+
public static void main(String[] inputs) throws Exception {
11+
12+
13+
}
14+
}

0 commit comments

Comments
 (0)