From f04a322ac462aaa57586b89c2d848c7dc7ada774 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 19 Mar 2022 14:36:33 +0100 Subject: [PATCH] More refactoring --- .../main/java/pgp/wkd/cli/DiscoverImpl.java | 21 ------------------- .../wkd/cli/HttpsCertificateDiscoverer.java | 21 +++++++++++++++++++ ....java => PGPainlessCertificateParser.java} | 2 +- .../main/java/pgp/wkd/cli/command/Fetch.java | 8 +++---- ...java => DefaultCertificateDiscoverer.java} | 4 ++-- ...HttpsUrlConnectionCertificateFetcher.java} | 7 ++++--- 6 files changed, 32 insertions(+), 31 deletions(-) delete mode 100644 wkd-java-cli/src/main/java/pgp/wkd/cli/DiscoverImpl.java create mode 100644 wkd-java-cli/src/main/java/pgp/wkd/cli/HttpsCertificateDiscoverer.java rename wkd-java-cli/src/main/java/pgp/wkd/cli/{CertificateParserImpl.java => PGPainlessCertificateParser.java} (95%) rename wkd-java/src/main/java/pgp/wkd/discovery/{CertificateDiscoveryImplementation.java => DefaultCertificateDiscoverer.java} (92%) rename wkd-java/src/main/java/pgp/wkd/discovery/{HttpUrlConnectionCertificateFetcher.java => HttpsUrlConnectionCertificateFetcher.java} (71%) diff --git a/wkd-java-cli/src/main/java/pgp/wkd/cli/DiscoverImpl.java b/wkd-java-cli/src/main/java/pgp/wkd/cli/DiscoverImpl.java deleted file mode 100644 index 0ab39c8..0000000 --- a/wkd-java-cli/src/main/java/pgp/wkd/cli/DiscoverImpl.java +++ /dev/null @@ -1,21 +0,0 @@ -package pgp.wkd.cli; - -import pgp.wkd.discovery.CertificateDiscoveryImplementation; -import pgp.wkd.discovery.CertificateParser; -import pgp.wkd.discovery.HttpUrlConnectionCertificateFetcher; -import pgp.wkd.discovery.CertificateFetcher; - -public class DiscoverImpl extends CertificateDiscoveryImplementation { - - public DiscoverImpl() { - super(new CertificateParserImpl(), new HttpUrlConnectionCertificateFetcher()); - } - - public DiscoverImpl(CertificateFetcher fetcher) { - super(new CertificateParserImpl(), fetcher); - } - - public DiscoverImpl(CertificateParser certificateParser, CertificateFetcher fetcher) { - super(certificateParser, fetcher); - } -} diff --git a/wkd-java-cli/src/main/java/pgp/wkd/cli/HttpsCertificateDiscoverer.java b/wkd-java-cli/src/main/java/pgp/wkd/cli/HttpsCertificateDiscoverer.java new file mode 100644 index 0000000..ca95886 --- /dev/null +++ b/wkd-java-cli/src/main/java/pgp/wkd/cli/HttpsCertificateDiscoverer.java @@ -0,0 +1,21 @@ +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); + } +} diff --git a/wkd-java-cli/src/main/java/pgp/wkd/cli/CertificateParserImpl.java b/wkd-java-cli/src/main/java/pgp/wkd/cli/PGPainlessCertificateParser.java similarity index 95% rename from wkd-java-cli/src/main/java/pgp/wkd/cli/CertificateParserImpl.java rename to wkd-java-cli/src/main/java/pgp/wkd/cli/PGPainlessCertificateParser.java index 3ed47eb..8aeb723 100644 --- a/wkd-java-cli/src/main/java/pgp/wkd/cli/CertificateParserImpl.java +++ b/wkd-java-cli/src/main/java/pgp/wkd/cli/PGPainlessCertificateParser.java @@ -19,7 +19,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; -public class CertificateParserImpl implements CertificateParser { +public class PGPainlessCertificateParser implements CertificateParser { @Override public List read(InputStream inputStream) throws IOException { List certificatesAndUserIds = new ArrayList<>(); diff --git a/wkd-java-cli/src/main/java/pgp/wkd/cli/command/Fetch.java b/wkd-java-cli/src/main/java/pgp/wkd/cli/command/Fetch.java index a1d33fb..d768fd2 100644 --- a/wkd-java-cli/src/main/java/pgp/wkd/cli/command/Fetch.java +++ b/wkd-java-cli/src/main/java/pgp/wkd/cli/command/Fetch.java @@ -8,14 +8,14 @@ import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.util.io.Streams; import pgp.certificate_store.Certificate; import pgp.wkd.discovery.CertificateDiscoverer; -import pgp.wkd.discovery.HttpUrlConnectionCertificateFetcher; +import pgp.wkd.discovery.HttpsUrlConnectionCertificateFetcher; import pgp.wkd.MalformedUserIdException; import pgp.wkd.WKDAddress; import pgp.wkd.WKDAddressHelper; import pgp.wkd.discovery.DiscoveryResult; import pgp.wkd.discovery.CertificateFetcher; import pgp.wkd.cli.CertNotFetchableException; -import pgp.wkd.cli.DiscoverImpl; +import pgp.wkd.cli.HttpsCertificateDiscoverer; import picocli.CommandLine; import java.io.IOException; @@ -42,11 +42,11 @@ public class Fetch implements Runnable { boolean armor = false; // TODO: Better way to inject fetcher implementation - public static CertificateFetcher fetcher = new HttpUrlConnectionCertificateFetcher(); + public static CertificateFetcher fetcher = new HttpsUrlConnectionCertificateFetcher(); @Override public void run() { - CertificateDiscoverer certificateDiscoverer = new DiscoverImpl(fetcher); + CertificateDiscoverer certificateDiscoverer = new HttpsCertificateDiscoverer(fetcher); WKDAddress address = addressFromUserId(userId); DiscoveryResult result = certificateDiscoverer.discover(address); diff --git a/wkd-java/src/main/java/pgp/wkd/discovery/CertificateDiscoveryImplementation.java b/wkd-java/src/main/java/pgp/wkd/discovery/DefaultCertificateDiscoverer.java similarity index 92% rename from wkd-java/src/main/java/pgp/wkd/discovery/CertificateDiscoveryImplementation.java rename to wkd-java/src/main/java/pgp/wkd/discovery/DefaultCertificateDiscoverer.java index 337676d..1b170aa 100644 --- a/wkd-java/src/main/java/pgp/wkd/discovery/CertificateDiscoveryImplementation.java +++ b/wkd-java/src/main/java/pgp/wkd/discovery/DefaultCertificateDiscoverer.java @@ -15,12 +15,12 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; -public class CertificateDiscoveryImplementation implements CertificateDiscoverer { +public class DefaultCertificateDiscoverer implements CertificateDiscoverer { protected final CertificateParser reader; protected final CertificateFetcher fetcher; - public CertificateDiscoveryImplementation(CertificateParser reader, CertificateFetcher fetcher) { + public DefaultCertificateDiscoverer(CertificateParser reader, CertificateFetcher fetcher) { this.reader = reader; this.fetcher = fetcher; } diff --git a/wkd-java/src/main/java/pgp/wkd/discovery/HttpUrlConnectionCertificateFetcher.java b/wkd-java/src/main/java/pgp/wkd/discovery/HttpsUrlConnectionCertificateFetcher.java similarity index 71% rename from wkd-java/src/main/java/pgp/wkd/discovery/HttpUrlConnectionCertificateFetcher.java rename to wkd-java/src/main/java/pgp/wkd/discovery/HttpsUrlConnectionCertificateFetcher.java index aea62e7..9f1ab6c 100644 --- a/wkd-java/src/main/java/pgp/wkd/discovery/HttpUrlConnectionCertificateFetcher.java +++ b/wkd-java/src/main/java/pgp/wkd/discovery/HttpsUrlConnectionCertificateFetcher.java @@ -4,6 +4,7 @@ package pgp.wkd.discovery; +import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; @@ -14,11 +15,11 @@ import java.net.URL; /** * Implementation of {@link CertificateFetcher} using Java's {@link HttpURLConnection}. */ -public class HttpUrlConnectionCertificateFetcher extends AbstractUriCertificateFetcher { +public class HttpsUrlConnectionCertificateFetcher extends AbstractUriCertificateFetcher { public InputStream fetchFromUri(URI uri) throws IOException { URL url = uri.toURL(); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); + HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setConnectTimeout(5000); @@ -27,7 +28,7 @@ public class HttpUrlConnectionCertificateFetcher extends AbstractUriCertificateF int status = con.getResponseCode(); if (status != 200) { - throw new ConnectException("Connecting to '" + uri + "' failed. Status: " + status); + throw new ConnectException("Connecting to URL '" + uri + "' failed. Status: " + status); } return con.getInputStream(); }