wkd-java/wkd-java-cli/src/test/java/pgp/wkd/cli/test_suite/DirectoryBasedCertificateFe...

46 lines
1.4 KiB
Java
Raw Normal View History

// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.wkd.cli.test_suite;
2022-03-17 15:27:28 +01:00
import pgp.wkd.discovery.DiscoveryMethod;
2022-03-10 16:56:46 +01:00
import pgp.wkd.WKDAddress;
2022-03-17 15:27:28 +01:00
import pgp.wkd.discovery.CertificateFetcher;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Path;
2022-03-17 15:27:28 +01:00
public class DirectoryBasedCertificateFetcher implements CertificateFetcher {
// The directory containing the .well-known subdirectory
private final Path rootPath;
2022-03-17 15:27:28 +01:00
public DirectoryBasedCertificateFetcher(Path rootPath) {
this.rootPath = rootPath;
}
@Override
2022-03-17 15:27:28 +01:00
public InputStream fetchCertificate(WKDAddress address, DiscoveryMethod method) throws IOException {
return inputStreamFromFile(address, method);
}
@Override
public InputStream fetchPolicy(WKDAddress address, DiscoveryMethod method) throws IOException {
return inputStreamFromFile(address, method);
}
private InputStream inputStreamFromFile(WKDAddress address, DiscoveryMethod method) throws FileNotFoundException {
2022-03-10 16:56:46 +01:00
URI uri = address.getUri(method);
2022-03-21 12:33:59 +01:00
String path = uri.getPath().substring(1); // get rid of leading slash at start of path
File file = rootPath.resolve(path).toFile();
FileInputStream fileIn = new FileInputStream(file);
return fileIn;
}
}