Skip to content

Commit 70a68ce

Browse files
committed
Perform HEAD check for all property values
1 parent c173594 commit 70a68ce

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

.github/workflows/test.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
test:
10-
name: "Test"
10+
name: "Test Download"
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: 'Check out repository'
@@ -18,3 +18,14 @@ jobs:
1818
PATH=$JAVA_HOME_17_X64/bin:$PATH
1919
javac -d classes src/Download.java test/Test.java
2020
java -cp classes Test
21+
validate:
22+
name: "Validate Values"
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: 'Check out repository'
26+
uses: actions/checkout@v2
27+
- name: 'Run validation program'
28+
shell: bash
29+
run: |
30+
PATH=$JAVA_HOME_17_X64/bin:$PATH
31+
java test/Validate.java

test/Validate.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates.
3+
*
4+
* This source code is licensed under the UPL license found in the
5+
* LICENSE.txt file in the root directory of this source tree.
6+
*/
7+
8+
import java.net.URI;
9+
import java.net.http.HttpClient;
10+
import java.net.http.HttpClient.Redirect;
11+
import java.net.http.HttpRequest;
12+
import java.net.http.HttpRequest.BodyPublishers;
13+
import java.net.http.HttpResponse.BodyHandlers;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.util.List;
17+
import java.util.Properties;
18+
import java.util.TreeSet;
19+
import java.util.stream.Stream;
20+
21+
class Validate {
22+
23+
public static void main(String... args) throws Exception {
24+
var validate = new Validate();
25+
var paths = args.length == 0 ? findProperties() : Stream.of(args).map(Path::of).toList();
26+
for (var path : paths) validate.validateProperties(path);
27+
}
28+
29+
static List<Path> findProperties() throws Exception {
30+
var syntaxAndPattern = "glob:*.properties";
31+
System.out.println(syntaxAndPattern);
32+
var directory = Path.of("");
33+
var matcher = directory.getFileSystem().getPathMatcher(syntaxAndPattern);
34+
try (var stream = Files.find(directory, 9, (path, attributes) -> matcher.matches(path))) {
35+
return stream.toList();
36+
}
37+
}
38+
39+
final HttpClient http = HttpClient.newBuilder().followRedirects(Redirect.NORMAL).build();
40+
41+
void validateProperties(Path path) throws Exception {
42+
if (Files.notExists(path)) throw new IllegalArgumentException("no such file: " + path);
43+
System.out.println();
44+
System.out.println(path);
45+
var properties = new Properties();
46+
properties.load(Files.newBufferedReader(path));
47+
for (var key : new TreeSet<>(properties.stringPropertyNames())) {
48+
var value = properties.getProperty(key);
49+
validateProperty(key, value);
50+
}
51+
}
52+
53+
private void validateProperty(String key, String value) throws Exception {
54+
if (value.startsWith("http")) {
55+
var uri = URI.create(value);
56+
var request = HttpRequest.newBuilder(uri).method("HEAD", BodyPublishers.noBody()).build();
57+
var response = http.send(request, BodyHandlers.discarding());
58+
if (response.statusCode() == 200) {
59+
var size = response.headers().firstValueAsLong("Content-Length").orElse(-1);
60+
System.out.printf("%s%n", key);
61+
System.out.printf(" uri = %s%n", uri);
62+
System.out.printf(" size = %,11d bytes%n", size);
63+
} else {
64+
response
65+
.headers()
66+
.map()
67+
.forEach((header, entry) -> System.err.printf("%s -> %s%n", header, entry));
68+
throw new AssertionError("Status for '%s' not OK: %s".formatted(key, response));
69+
}
70+
return;
71+
}
72+
System.err.printf("Unknown property protocol %s=%s%n", key, value);
73+
}
74+
}

0 commit comments

Comments
 (0)