|
| 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