Skip to content

Commit 954afcb

Browse files
committed
add more examples
1 parent d138b51 commit 954afcb

File tree

11 files changed

+251
-121
lines changed

11 files changed

+251
-121
lines changed

.github/workflows/ci-java.yaml

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,54 @@ jobs:
9494
with:
9595
arguments: publishToLocalMaven
9696
gradle-version: "8.6"
97-
build-root-directory: java
97+
build-root-directory: java
9898

99-
- name: run examples for file
99+
- name: run forSha256 example
100+
uses: gradle/gradle-build-action@v3
101+
with:
102+
arguments: sha256Scan
103+
gradle-version: "8.6"
104+
build-root-directory: java/examples/VaasExample
105+
106+
- name: run forFile example
100107
env:
101-
SCAN_PATH: "src/main/java/de/gdata/vaasexample/Main.java"
108+
SCAN_PATH: "build.gradle"
102109
uses: gradle/gradle-build-action@v3
103110
with:
104111
arguments: fileScan
105112
gradle-version: "8.6"
106113
build-root-directory: java/examples/VaasExample
107114

108-
- name: run examples for url
115+
- name: run forStream example
116+
env:
117+
SCAN_PATH: "build.gradle"
109118
uses: gradle/gradle-build-action@v3
110119
with:
111-
arguments: urlScan
120+
arguments: streamScan
112121
gradle-version: "8.6"
113122
build-root-directory: java/examples/VaasExample
114123

115-
- name: run authentication examples
124+
- name: run forUrl example
125+
uses: gradle/gradle-build-action@v3
126+
with:
127+
arguments: urlScan
128+
gradle-version: "8.6"
129+
build-root-directory: java/examples/VaasExample
130+
131+
- name: run authentication example
116132
uses: gradle/gradle-build-action@v3
117133
with:
118134
arguments: authentication
119135
gradle-version: "8.6"
136+
build-root-directory: java/examples/VaasExample
137+
138+
- name: run config example
139+
env:
140+
SCAN_PATH: "build.gradle"
141+
uses: gradle/gradle-build-action@v3
142+
with:
143+
arguments: config
144+
gradle-version: "8.6"
120145
build-root-directory: java/examples/VaasExample
121146

122147
- name: extract version

java/examples/VaasExample/build.gradle

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,35 @@ repositories {
1313
dependencies {
1414
implementation 'org.slf4j:slf4j-nop:2.0.16'
1515
implementation 'de.gdata:vaas:0.0.0'
16+
implementation 'org.projectlombok:lombok:1.18.36'
1617
}
1718

18-
task fileScan(type: JavaExec) {
19+
tasks.register('sha256Scan', JavaExec) {
1920
classpath = sourceSets.main.runtimeClasspath
20-
mainClass = 'de.gdata.vaasexample.Main'
21+
mainClass = 'de.gdata.vaasexample.ForSha256Scan'
2122
}
2223

23-
task urlScan(type: JavaExec) {
24+
tasks.register('fileScan', JavaExec) {
2425
classpath = sourceSets.main.runtimeClasspath
25-
mainClass = 'de.gdata.vaasexample.UrlScan'
26+
mainClass = 'de.gdata.vaasexample.ForFileScan'
2627
}
2728

28-
task authentication(type: JavaExec) {
29+
tasks.register('streamScan', JavaExec) {
30+
classpath = sourceSets.main.runtimeClasspath
31+
mainClass = 'de.gdata.vaasexample.ForStreamScan'
32+
}
33+
34+
tasks.register('urlScan', JavaExec) {
35+
classpath = sourceSets.main.runtimeClasspath
36+
mainClass = 'de.gdata.vaasexample.ForUrlScan'
37+
}
38+
39+
tasks.register('authentication', JavaExec) {
2940
classpath = sourceSets.main.runtimeClasspath
3041
mainClass = 'de.gdata.vaasexample.Authentication'
3142
}
43+
44+
tasks.register('config', JavaExec) {
45+
classpath = sourceSets.main.runtimeClasspath
46+
mainClass = 'de.gdata.vaasexample.Config'
47+
}

java/examples/VaasExample/build.local.gradle

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package de.gdata.vaasexample;
2+
3+
import de.gdata.vaas.*;
4+
import de.gdata.vaas.authentication.*;
5+
6+
import java.net.URI;
7+
import java.nio.file.Path;
8+
9+
public class Config {
10+
public static void main(String[] args) throws Exception {
11+
IAuthenticator authenticator;
12+
var env = new Environment();
13+
if (env.clientId == null) {
14+
if (env.userName == null) {
15+
throw new IllegalStateException("Either CLIENT_ID or VAAS_USER_NAME must be set");
16+
}
17+
authenticator = new ResourceOwnerPasswordGrantAuthenticator("vaas-customer", env.userName, env.password, new URI(env.tokenUrl));
18+
} else {
19+
authenticator = new ClientCredentialsGrantAuthenticator(env.clientId, env.clientSecret, new URI(env.tokenUrl));
20+
}
21+
22+
var timeoutInMs = 5000;
23+
var useCache = false;
24+
var useHashLookup = false;
25+
var config = new VaasConfig(timeoutInMs, useCache, useHashLookup, new URI(env.vaasUrl));
26+
var vaas = new Vaas(config, authenticator);
27+
var file = Path.of(env.scanPath);
28+
var verdict = vaas.forFile(file);
29+
System.out.printf("File %s was sync detected as %s", verdict.getSha256(), verdict.getVerdict());
30+
vaas.forFileAsync(file).thenAccept(vaasResult -> {
31+
System.out.printf("\nFile %s was async detected as %s", verdict.getSha256(), verdict.getVerdict());
32+
}).get();
33+
}
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package de.gdata.vaasexample;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class Environment {
9+
public String clientId;
10+
public String clientSecret;
11+
public String userName;
12+
public String password;
13+
public String scanPath;
14+
public String vaasUrl;
15+
public String tokenUrl;
16+
17+
public Environment () {
18+
clientId = System.getenv("CLIENT_ID");
19+
clientSecret = System.getenv("CLIENT_SECRET");
20+
userName = System.getenv("VAAS_USER_NAME");
21+
password = System.getenv("VAAS_PASSWORD");
22+
scanPath = getenv("SCAN_PATH");
23+
vaasUrl = System.getenv("VAAS_URL");
24+
if (vaasUrl == null) {
25+
vaasUrl = "wss://gateway.staging.vaas.gdatasecurity.de";
26+
}
27+
tokenUrl = System.getenv("VAAS_TOKEN_URL");
28+
if (tokenUrl == null) {
29+
tokenUrl = "https://account-staging.gdata.de/realms/vaas-staging/protocol/openid-connect/token";
30+
}
31+
}
32+
33+
public static String getenv(String key) {
34+
var value = System.getenv(key);
35+
if (value == null) {
36+
throw new IllegalStateException("The environment variable " + key + " must be set.");
37+
}
38+
return value;
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.gdata.vaasexample;
2+
3+
import de.gdata.vaas.*;
4+
import de.gdata.vaas.authentication.*;
5+
import java.net.URI;
6+
7+
import java.nio.file.Path;
8+
9+
public class ForFileScan {
10+
public static void main(String[] args) throws Exception {
11+
IAuthenticator authenticator;
12+
var env = new Environment();
13+
if (env.clientId == null) {
14+
if (env.userName == null) {
15+
throw new IllegalStateException("Either CLIENT_ID or VAAS_USER_NAME must be set");
16+
}
17+
authenticator = new ResourceOwnerPasswordGrantAuthenticator("vaas-customer", env.userName, env.password, new URI(env.tokenUrl));
18+
} else {
19+
authenticator = new ClientCredentialsGrantAuthenticator(env.clientId, env.clientSecret, new URI(env.tokenUrl));
20+
}
21+
22+
var config = new VaasConfig(new URI(env.vaasUrl));
23+
var vaas = new Vaas(config, authenticator);
24+
var file = Path.of(env.scanPath);
25+
var verdict = vaas.forFile(file);
26+
System.out.printf("File %s was sync detected as %s", verdict.getSha256(), verdict.getVerdict());
27+
vaas.forFileAsync(file).thenAccept(vaasResult -> {
28+
System.out.printf("\nFile %s was async detected as %s", verdict.getSha256(), verdict.getVerdict());
29+
}).get();
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package de.gdata.vaasexample;
2+
3+
import de.gdata.vaas.*;
4+
import de.gdata.vaas.authentication.*;
5+
import java.net.URI;
6+
7+
public class ForSha256Scan {
8+
public static void main(String[] args) throws Exception {
9+
IAuthenticator authenticator;
10+
var env = new Environment();
11+
if (env.clientId == null) {
12+
if (env.userName == null) {
13+
throw new IllegalStateException("Either CLIENT_ID or VAAS_USER_NAME must be set");
14+
}
15+
authenticator = new ResourceOwnerPasswordGrantAuthenticator("vaas-customer", env.userName, env.password, new URI(env.tokenUrl));
16+
} else {
17+
authenticator = new ClientCredentialsGrantAuthenticator(env.clientId, env.clientSecret, new URI(env.tokenUrl));
18+
}
19+
20+
var config = new VaasConfig(new URI(env.vaasUrl));
21+
var vaas = new Vaas(config, authenticator);
22+
var sha256 = new Sha256("275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f");
23+
var verdict = vaas.forSha256(sha256);
24+
System.out.printf("File %s was sync detected as %s", verdict.getSha256(), verdict.getVerdict());
25+
vaas.forSha256Async(sha256).thenAccept(vaasResult -> {
26+
System.out.printf("\nFile %s was async detected as %s", verdict.getSha256(), verdict.getVerdict());
27+
}).get();
28+
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package de.gdata.vaasexample;
2+
3+
import de.gdata.vaas.*;
4+
import de.gdata.vaas.authentication.*;
5+
import java.net.URI;
6+
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
10+
public class ForStreamScan {
11+
public static void main(String[] args) throws Exception {
12+
IAuthenticator authenticator;
13+
var env = new Environment();
14+
if (env.clientId == null) {
15+
if (env.userName == null) {
16+
throw new IllegalStateException("Either CLIENT_ID or VAAS_USER_NAME must be set");
17+
}
18+
authenticator = new ResourceOwnerPasswordGrantAuthenticator("vaas-customer", env.userName, env.password, new URI(env.tokenUrl));
19+
} else {
20+
authenticator = new ClientCredentialsGrantAuthenticator(env.clientId, env.clientSecret, new URI(env.tokenUrl));
21+
}
22+
23+
var config = new VaasConfig(new URI(env.vaasUrl));
24+
var vaas = new Vaas(config, authenticator);
25+
try (var inputStream = Files.newInputStream(Path.of(env.scanPath))) {
26+
var verdict = vaas.forStream(inputStream, Path.of(env.scanPath).toFile().length());
27+
System.out.printf("File %s was sync detected as %s", verdict.getSha256(), verdict.getVerdict());
28+
vaas.forStreamAsync(inputStream, Path.of(env.scanPath).toFile().length()).thenAccept(vaasResult -> {
29+
System.out.printf("\nFile %s was async detected as %s", verdict.getSha256(), verdict.getVerdict());
30+
}).get();
31+
}
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.gdata.vaasexample;
2+
3+
import de.gdata.vaas.*;
4+
import de.gdata.vaas.authentication.*;
5+
import java.net.URI;
6+
import java.net.URL;
7+
8+
public class ForUrlScan {
9+
public static void main(String[] args) throws Exception {
10+
IAuthenticator authenticator;
11+
var env = new Environment();
12+
if (env.clientId == null) {
13+
if (env.userName == null) {
14+
throw new IllegalStateException("Either CLIENT_ID or VAAS_USER_NAME must be set");
15+
}
16+
authenticator = new ResourceOwnerPasswordGrantAuthenticator("vaas-customer", env.userName, env.password, new URI(env.tokenUrl));
17+
} else {
18+
authenticator = new ClientCredentialsGrantAuthenticator(env.clientId, env.clientSecret, new URI(env.tokenUrl));
19+
}
20+
21+
var config = new VaasConfig(new URI(env.vaasUrl));
22+
var vaas = new Vaas(config, authenticator);
23+
var url = new URL("https://secure.eicar.org/eicar.com.txt");
24+
var verdict = vaas.forUrl(url);
25+
System.out.printf("File %s was sync detected as %s", verdict.getSha256(), verdict.getVerdict());
26+
vaas.forUrlAsync(url).thenAccept(vaasResult -> {
27+
System.out.printf("\nFile %s was async detected as %s", verdict.getSha256(), verdict.getVerdict());
28+
}).get();
29+
30+
}
31+
}

java/examples/VaasExample/src/main/java/de/gdata/vaasexample/Main.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)