Skip to content

Commit 86405dc

Browse files
authored
feat: Make a Java SDK for Casvisor (#2)
1 parent 812436f commit 86405dc

File tree

15 files changed

+974
-0
lines changed

15 files changed

+974
-0
lines changed

.github/workflows/maven-ci.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Build casdoor-java-sdk
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
with:
13+
fetch-depth: '0'
14+
15+
- name: Set up JDK 1.8
16+
uses: actions/setup-java@v1
17+
with:
18+
java-version: 1.8
19+
server-username: OSSRH_JIRA_USERNAME
20+
server-password: OSSRH_JIRA_PASSWORD
21+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
22+
gpg-passphrase: GPG_PASSPHRASE
23+
24+
- name: Build with Maven
25+
run: mvn package -Dmaven.test.skip=true cobertura:cobertura
26+
27+
- name: Codecov
28+
uses: codecov/codecov-action@v1
29+
with:
30+
token: ${{ secrets.CODECOV_TOKEN }}
31+
32+
- name: Set up Node.js
33+
uses: actions/setup-node@v2
34+
with:
35+
node-version: 20
36+
37+
- name: Semantic Release
38+
run: |
39+
npm install -g @conveyal/maven-semantic-release semantic-release
40+
semantic-release --prepare @conveyal/maven-semantic-release --publish @semantic-release/github,@conveyal/maven-semantic-release --verify-conditions @semantic-release/github,@conveyal/maven-semantic-release --verify-release @conveyal/maven-semantic-release
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
GPG_KEY_NAME: ${{ secrets.GPG_KEY_NAME }}
44+
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
45+
OSSRH_JIRA_USERNAME: ${{ secrets.OSSRH_JIRA_USERNAME }}
46+
OSSRH_JIRA_PASSWORD: ${{ secrets.OSSRH_JIRA_PASSWORD }}

maven-settings.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<settings>
2+
<servers>
3+
<server>
4+
<id>ossrh</id>
5+
<username>${OSSRH_JIRA_USERNAME}</username>
6+
<password>${OSSRH_JIRA_PASSWORD}</password>
7+
</server>
8+
</servers>
9+
<profiles>
10+
<profile>
11+
<id>ossrh</id>
12+
<activation>
13+
<activeByDefault>true</activeByDefault>
14+
</activation>
15+
<properties>
16+
<gpg.executable>gpg</gpg.executable>
17+
<gpg.keyname>${GPG_KEY_NAME}</gpg.keyname>
18+
<gpg.passphrase>${GPG_PASSPHRASE}</gpg.passphrase>
19+
</properties>
20+
</profile>
21+
</profiles>
22+
</settings>

pom.xml

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.casbin</groupId>
8+
<artifactId>casvisor-java-sdk</artifactId>
9+
<version>1.0</version>
10+
11+
<name>Casvisor Java SDK</name>
12+
<description>Java client SDK for Casvisor.</description>
13+
<url>https://github.com/casvisor/casvisor-java-sdk</url>
14+
<licenses>
15+
<license>
16+
<name>The Apache Software License, Version 2.0</name>
17+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
18+
<distribution>repo</distribution>
19+
</license>
20+
</licenses>
21+
<developers>
22+
<developer>
23+
<name>Yang Luo</name>
24+
<email>hsluoyz@qq.com</email>
25+
<url>https://github.com/hsluoyz</url>
26+
</developer>
27+
</developers>
28+
<scm>
29+
<connection>scm:git:https://github.com/casvisor/casvisor-java-sdk.git</connection>
30+
<developerConnection>scm:git:https://github.com/casvisor/casvisor-java-sdk.git</developerConnection>
31+
<url>https://github.com/casvisor/casvisor-java-sdk</url>
32+
</scm>
33+
34+
<distributionManagement>
35+
<snapshotRepository>
36+
<id>ossrh</id>
37+
<url>https://central.sonatype.com</url>
38+
</snapshotRepository>
39+
</distributionManagement>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<!-- Automatically close and deploy from OSSRH -->
45+
<groupId>org.sonatype.central</groupId>
46+
<artifactId>central-publishing-maven-plugin</artifactId>
47+
<version>0.5.0</version>
48+
<extensions>true</extensions>
49+
<configuration>
50+
<publishingServerId>ossrh</publishingServerId>
51+
<tokenAuth>true</tokenAuth>
52+
<!-- Release versions will be synced to Maven Central automatically. -->
53+
<autoPublish>true</autoPublish>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-source-plugin</artifactId>
59+
<executions>
60+
<execution>
61+
<id>attach-sources</id>
62+
<goals>
63+
<goal>jar-no-fork</goal>
64+
</goals>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-javadoc-plugin</artifactId>
71+
<version>2.10.4</version>
72+
<executions>
73+
<execution>
74+
<id>attach-javadocs</id>
75+
<goals>
76+
<goal>jar</goal>
77+
</goals>
78+
</execution>
79+
</executions>
80+
</plugin>
81+
<plugin>
82+
<groupId>org.apache.maven.plugins</groupId>
83+
<artifactId>maven-gpg-plugin</artifactId>
84+
<version>1.5</version>
85+
<executions>
86+
<execution>
87+
<id>sign-artifacts</id>
88+
<phase>verify</phase>
89+
<goals>
90+
<goal>sign</goal>
91+
</goals>
92+
</execution>
93+
</executions>
94+
<configuration>
95+
<!-- Prevent gpg from using pinentry programs -->
96+
<gpgArguments>
97+
<arg>--pinentry-mode</arg>
98+
<arg>loopback</arg>
99+
</gpgArguments>
100+
</configuration>
101+
</plugin>
102+
<plugin>
103+
<groupId>org.codehaus.mojo</groupId>
104+
<artifactId>cobertura-maven-plugin</artifactId>
105+
<version>2.7</version>
106+
<configuration>
107+
<formats>
108+
<format>html</format>
109+
<format>xml</format>
110+
</formats>
111+
<check />
112+
</configuration>
113+
</plugin>
114+
</plugins>
115+
</build>
116+
117+
<dependencies>
118+
<dependency>
119+
<groupId>commons-beanutils</groupId>
120+
<artifactId>commons-beanutils</artifactId>
121+
<version>1.9.4</version>
122+
</dependency>
123+
<dependency>
124+
<groupId>com.squareup.okhttp3</groupId>
125+
<artifactId>okhttp</artifactId>
126+
<version>4.9.3</version>
127+
</dependency>
128+
<dependency>
129+
<groupId>org.apache.oltu.oauth2</groupId>
130+
<artifactId>org.apache.oltu.oauth2.client</artifactId>
131+
<version>1.0.2</version>
132+
</dependency>
133+
<!-- https://mvnrepository.com/artifact/com.nimbusds/nimbus-jose-jwt -->
134+
<dependency>
135+
<groupId>com.nimbusds</groupId>
136+
<artifactId>nimbus-jose-jwt</artifactId>
137+
<version>9.10</version>
138+
</dependency>
139+
140+
<dependency>
141+
<groupId>com.fasterxml.jackson.core</groupId>
142+
<artifactId>jackson-core</artifactId>
143+
<version>2.13.3</version>
144+
</dependency>
145+
<dependency>
146+
<groupId>com.fasterxml.jackson.core</groupId>
147+
<artifactId>jackson-databind</artifactId>
148+
<version>2.13.3</version>
149+
</dependency>
150+
<dependency>
151+
<groupId>junit</groupId>
152+
<artifactId>junit</artifactId>
153+
<version>4.13.2</version>
154+
<scope>test</scope>
155+
</dependency>
156+
<dependency>
157+
<groupId>org.junit.jupiter</groupId>
158+
<artifactId>junit-jupiter-api</artifactId>
159+
<version>5.8.2</version>
160+
<scope>test</scope>
161+
</dependency>
162+
<dependency>
163+
<groupId>org.junit.jupiter</groupId>
164+
<artifactId>junit-jupiter-engine</artifactId>
165+
<version>5.8.2</version>
166+
<scope>test</scope>
167+
</dependency>
168+
</dependencies>
169+
170+
171+
<properties>
172+
<maven.compiler.source>8</maven.compiler.source>
173+
<maven.compiler.target>8</maven.compiler.target>
174+
</properties>
175+
176+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.casbin.casvisor.config;
2+
3+
public class Config {
4+
public String endpoint;
5+
public String clientId;
6+
public String clientSecret;
7+
public String organizationName;
8+
public String applicationName;
9+
10+
public Config(String endpoint, String clientId, String clientSecret, String organizationName, String applicationName) {
11+
this.endpoint = endpoint;
12+
this.clientId = clientId;
13+
this.clientSecret = clientSecret;
14+
this.organizationName = organizationName;
15+
this.applicationName = applicationName;
16+
}
17+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.casbin.casvisor.entity;
2+
3+
public class Record {
4+
5+
public int id;
6+
public String owner;
7+
public String name;
8+
public String createdTime;
9+
public String organization;
10+
public String clientIp;
11+
public String user;
12+
public String method;
13+
public String requestUri;
14+
public String action;
15+
public String language;
16+
public String object;
17+
public String response;
18+
public String provider;
19+
public String block;
20+
public boolean isTriggered;
21+
22+
public Record(){}
23+
24+
public Record(String owner, String name, String createdTime, String organization, String clientIp,
25+
String user, String method, String requestUri, String action, String object, String language,
26+
String response, boolean isTriggered){
27+
this.owner = owner;
28+
this.name = name;
29+
this.createdTime = createdTime;
30+
this.organization = organization;
31+
this.clientIp = clientIp;
32+
this.user = user;
33+
this.method = method;
34+
this.requestUri = requestUri;
35+
this.action = action;
36+
this.object = object;
37+
this.language = language;
38+
this.response = response;
39+
this.isTriggered = isTriggered;
40+
}
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.casbin.casvisor.exception;
2+
3+
public class Exception extends RuntimeException {
4+
public Exception() {
5+
super();
6+
}
7+
8+
public Exception(String message, Throwable cause) {
9+
super(message, cause);
10+
}
11+
12+
public Exception(String message) {
13+
super(message);
14+
}
15+
16+
public Exception(Throwable cause) {
17+
super(cause);
18+
}
19+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.casbin.casvisor.service;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import org.casbin.casvisor.config.Config;
6+
import org.casbin.casvisor.entity.Record;
7+
import org.casbin.casvisor.util.Map;
8+
import org.casbin.casvisor.util.RecordOperations;
9+
import org.casbin.casvisor.util.http.CasvisorResponse;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.List;
13+
14+
import java.io.IOException;
15+
16+
public class RecordService extends Service{
17+
18+
public RecordService(Config config) {
19+
super(config);
20+
}
21+
22+
public Record getRecord(String name) throws IOException {
23+
CasvisorResponse<Record, Object> response = doGet("get-record",
24+
Map.of("id", config.organizationName + "/" + name), new TypeReference<CasvisorResponse<Record, Object>>() {
25+
});
26+
return response.getData();
27+
}
28+
29+
public List<Record> getRecords() throws IOException {
30+
CasvisorResponse<List<Record>, Object> response = doGet("get-records",
31+
Map.of("owner", config.organizationName), new TypeReference<CasvisorResponse<List<Record>, Object>>() {
32+
});
33+
return response.getData();
34+
}
35+
36+
public java.util.Map<String, Object> getPaginationRecords(int p, int pageSize, @Nullable java.util.Map<String, String> queryMap) throws IOException {
37+
CasvisorResponse<Record[], Object> response = doGet("get-records",
38+
Map.mergeMap(Map.of("owner", config.organizationName,
39+
"p", Integer.toString(p),
40+
"pageSize", Integer.toString(pageSize)), queryMap), new TypeReference<CasvisorResponse<Record[], Object>>() {
41+
});
42+
return Map.of("casvisorRecords", response.getData(), "data2", response.getData2());
43+
}
44+
45+
public CasvisorResponse<String, Object> addRecord(Record record) throws IOException {
46+
return modifyRecord(RecordOperations.ADD_Record,record,null);
47+
}
48+
49+
public CasvisorResponse<String, Object> deleteRecord(Record record) throws IOException {
50+
return modifyRecord(RecordOperations.DELETE_Record,record,null);
51+
}
52+
53+
public CasvisorResponse<String, Object> updateRecord(Record record) throws IOException {
54+
return modifyRecord(RecordOperations.UPDATE_Record,record,null);
55+
}
56+
57+
private <T1, T2> CasvisorResponse modifyRecord(RecordOperations method, Record record, java.util.Map<String, String> queryMap) throws IOException {
58+
String id = record.owner + "/" + record.name;
59+
record.owner = config.organizationName;
60+
String payload = objectMapper.writeValueAsString(record);
61+
62+
return doPost(method.getOperation(), Map.mergeMap(Map.of("id", id), queryMap), payload,
63+
new TypeReference<CasvisorResponse<T1, T2>>() {
64+
});
65+
}
66+
}

0 commit comments

Comments
 (0)