-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathValidate.java
More file actions
74 lines (67 loc) · 2.69 KB
/
Validate.java
File metadata and controls
74 lines (67 loc) · 2.69 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
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
*
* This source code is licensed under the UPL license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Redirect;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;
import java.util.TreeSet;
import java.util.stream.Stream;
class Validate {
public static void main(String... args) throws Exception {
var validate = new Validate();
var paths = args.length == 0 ? findProperties() : Stream.of(args).map(Path::of).toList();
for (var path : paths) validate.validateProperties(path);
}
static List<Path> findProperties() throws Exception {
var syntaxAndPattern = "glob:*.properties";
System.out.println(syntaxAndPattern);
var directory = Path.of("");
var matcher = directory.getFileSystem().getPathMatcher(syntaxAndPattern);
try (var stream = Files.find(directory, 9, (path, attributes) -> matcher.matches(path))) {
return stream.toList();
}
}
final HttpClient http = HttpClient.newBuilder().followRedirects(Redirect.NORMAL).build();
void validateProperties(Path path) throws Exception {
if (Files.notExists(path)) throw new IllegalArgumentException("no such file: " + path);
System.out.println();
System.out.println(path);
var properties = new Properties();
properties.load(Files.newBufferedReader(path));
for (var key : new TreeSet<>(properties.stringPropertyNames())) {
var value = properties.getProperty(key);
validateProperty(key, value);
}
}
private void validateProperty(String key, String value) throws Exception {
if (value.startsWith("http")) {
var uri = URI.create(value);
var request = HttpRequest.newBuilder(uri).method("HEAD", BodyPublishers.noBody()).build();
var response = http.send(request, BodyHandlers.discarding());
if (response.statusCode() == 200) {
var size = response.headers().firstValueAsLong("Content-Length").orElse(-1);
System.out.printf("%s%n", key);
System.out.printf(" uri = %s%n", uri);
System.out.printf(" size = %,11d bytes%n", size);
} else {
response
.headers()
.map()
.forEach((header, entry) -> System.err.printf("%s -> %s%n", header, entry));
throw new AssertionError("Status for '%s' not OK: %s".formatted(key, response));
}
return;
}
System.err.printf("Unknown property protocol %s=%s%n", key, value);
}
}