Improve readability

This commit is contained in:
Paul Schaub 2022-03-21 12:33:59 +01:00
parent c2d4d283bc
commit 7a36c285b5
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 22 additions and 30 deletions

View file

@ -1,21 +0,0 @@
package pgp.wkd.cli;
import pgp.wkd.discovery.DefaultCertificateDiscoverer;
import pgp.wkd.discovery.CertificateParser;
import pgp.wkd.discovery.HttpsUrlConnectionCertificateFetcher;
import pgp.wkd.discovery.CertificateFetcher;
public class HttpsCertificateDiscoverer extends DefaultCertificateDiscoverer {
public HttpsCertificateDiscoverer() {
super(new PGPainlessCertificateParser(), new HttpsUrlConnectionCertificateFetcher());
}
public HttpsCertificateDiscoverer(CertificateFetcher fetcher) {
super(new PGPainlessCertificateParser(), fetcher);
}
public HttpsCertificateDiscoverer(CertificateParser certificateParser, CertificateFetcher fetcher) {
super(certificateParser, fetcher);
}
}

View file

@ -7,10 +7,10 @@ package pgp.wkd.cli.command;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import pgp.wkd.WKDAddress;
import pgp.wkd.WKDAddressHelper;
import pgp.wkd.cli.HttpsCertificateDiscoverer;
import pgp.wkd.cli.PGPainlessCertificateParser;
import pgp.wkd.cli.RuntimeIOException;
import pgp.wkd.discovery.CertificateDiscoverer;
import pgp.wkd.discovery.CertificateFetcher;
import pgp.wkd.discovery.DefaultCertificateDiscoverer;
import pgp.wkd.discovery.DiscoveryResult;
import pgp.wkd.discovery.HttpsUrlConnectionCertificateFetcher;
import pgp.wkd.exception.MalformedUserIdException;
@ -39,15 +39,14 @@ public class Fetch implements Runnable {
)
boolean armor = false;
// TODO: Better way to inject fetcher implementation
public static CertificateFetcher fetcher = new HttpsUrlConnectionCertificateFetcher();
private static CertificateDiscoverer discoverer = new DefaultCertificateDiscoverer(
new PGPainlessCertificateParser(), new HttpsUrlConnectionCertificateFetcher());
@Override
public void run() {
CertificateDiscoverer certificateDiscoverer = new HttpsCertificateDiscoverer(fetcher);
WKDAddress address = addressFromUserId(userId);
DiscoveryResult result = certificateDiscoverer.discover(address);
DiscoveryResult result = discoverer.discover(address);
OutputStream outputStream = armor ? new ArmoredOutputStream(System.out) : System.out;
try {
@ -56,10 +55,19 @@ public class Fetch implements Runnable {
outputStream.close();
}
} catch (IOException e) {
// we need to wrap the ioe, since run() does not declare it
throw new RuntimeIOException(e);
}
}
public static void setCertificateDiscoverer(CertificateDiscoverer discoverer) {
if (discoverer == null) {
throw new NullPointerException("CertificateDiscoverer cannot be null.");
}
Fetch.discoverer = discoverer;
}
private WKDAddress addressFromUserId(String userId) {
String email;
try {

View file

@ -27,8 +27,8 @@ public class DirectoryBasedCertificateFetcher implements CertificateFetcher {
@Override
public InputStream fetchCertificate(WKDAddress address, DiscoveryMethod method) throws IOException {
URI uri = address.getUri(method);
String path = uri.getPath();
File file = rootPath.resolve(path.substring(1)).toFile(); // get rid of leading slash at start of path
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;
}

View file

@ -7,8 +7,11 @@ package pgp.wkd.cli.test_suite;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import pgp.wkd.cli.PGPainlessCertificateParser;
import pgp.wkd.cli.WKDCLI;
import pgp.wkd.cli.command.Fetch;
import pgp.wkd.discovery.CertificateDiscoverer;
import pgp.wkd.discovery.DefaultCertificateDiscoverer;
import pgp.wkd.discovery.DiscoveryMethod;
import pgp.wkd.test_suite.TestCase;
import pgp.wkd.test_suite.TestSuite;
@ -39,7 +42,9 @@ public class TestSuiteTestRunner {
suite = generator.generateTestSuiteInDirectory(tempFile, DiscoveryMethod.direct);
// Fetch certificates from a local directory instead of the internetzzz.
Fetch.fetcher = new DirectoryBasedCertificateFetcher(tempPath);
CertificateDiscoverer discoverer = new DefaultCertificateDiscoverer(
new PGPainlessCertificateParser(), new DirectoryBasedCertificateFetcher(tempPath));
Fetch.setCertificateDiscoverer(discoverer);
}
@TestFactory