More refactoring

This commit is contained in:
Paul Schaub 2022-03-19 14:36:33 +01:00
parent a6b52562a8
commit f04a322ac4
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
6 changed files with 32 additions and 31 deletions

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -19,7 +19,7 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class CertificateParserImpl implements CertificateParser { public class PGPainlessCertificateParser implements CertificateParser {
@Override @Override
public List<CertificateAndUserIds> read(InputStream inputStream) throws IOException { public List<CertificateAndUserIds> read(InputStream inputStream) throws IOException {
List<CertificateAndUserIds> certificatesAndUserIds = new ArrayList<>(); List<CertificateAndUserIds> certificatesAndUserIds = new ArrayList<>();

View file

@ -8,14 +8,14 @@ import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import pgp.certificate_store.Certificate; import pgp.certificate_store.Certificate;
import pgp.wkd.discovery.CertificateDiscoverer; import pgp.wkd.discovery.CertificateDiscoverer;
import pgp.wkd.discovery.HttpUrlConnectionCertificateFetcher; import pgp.wkd.discovery.HttpsUrlConnectionCertificateFetcher;
import pgp.wkd.MalformedUserIdException; import pgp.wkd.MalformedUserIdException;
import pgp.wkd.WKDAddress; import pgp.wkd.WKDAddress;
import pgp.wkd.WKDAddressHelper; import pgp.wkd.WKDAddressHelper;
import pgp.wkd.discovery.DiscoveryResult; import pgp.wkd.discovery.DiscoveryResult;
import pgp.wkd.discovery.CertificateFetcher; import pgp.wkd.discovery.CertificateFetcher;
import pgp.wkd.cli.CertNotFetchableException; import pgp.wkd.cli.CertNotFetchableException;
import pgp.wkd.cli.DiscoverImpl; import pgp.wkd.cli.HttpsCertificateDiscoverer;
import picocli.CommandLine; import picocli.CommandLine;
import java.io.IOException; import java.io.IOException;
@ -42,11 +42,11 @@ public class Fetch implements Runnable {
boolean armor = false; boolean armor = false;
// TODO: Better way to inject fetcher implementation // TODO: Better way to inject fetcher implementation
public static CertificateFetcher fetcher = new HttpUrlConnectionCertificateFetcher(); public static CertificateFetcher fetcher = new HttpsUrlConnectionCertificateFetcher();
@Override @Override
public void run() { public void run() {
CertificateDiscoverer certificateDiscoverer = new DiscoverImpl(fetcher); CertificateDiscoverer certificateDiscoverer = new HttpsCertificateDiscoverer(fetcher);
WKDAddress address = addressFromUserId(userId); WKDAddress address = addressFromUserId(userId);
DiscoveryResult result = certificateDiscoverer.discover(address); DiscoveryResult result = certificateDiscoverer.discover(address);

View file

@ -15,12 +15,12 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class CertificateDiscoveryImplementation implements CertificateDiscoverer { public class DefaultCertificateDiscoverer implements CertificateDiscoverer {
protected final CertificateParser reader; protected final CertificateParser reader;
protected final CertificateFetcher fetcher; protected final CertificateFetcher fetcher;
public CertificateDiscoveryImplementation(CertificateParser reader, CertificateFetcher fetcher) { public DefaultCertificateDiscoverer(CertificateParser reader, CertificateFetcher fetcher) {
this.reader = reader; this.reader = reader;
this.fetcher = fetcher; this.fetcher = fetcher;
} }

View file

@ -4,6 +4,7 @@
package pgp.wkd.discovery; package pgp.wkd.discovery;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.ConnectException; import java.net.ConnectException;
@ -14,11 +15,11 @@ import java.net.URL;
/** /**
* Implementation of {@link CertificateFetcher} using Java's {@link HttpURLConnection}. * 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 { public InputStream fetchFromUri(URI uri) throws IOException {
URL url = uri.toURL(); URL url = uri.toURL();
HttpURLConnection con = (HttpURLConnection) url.openConnection(); HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET"); con.setRequestMethod("GET");
con.setConnectTimeout(5000); con.setConnectTimeout(5000);
@ -27,7 +28,7 @@ public class HttpUrlConnectionCertificateFetcher extends AbstractUriCertificateF
int status = con.getResponseCode(); int status = con.getResponseCode();
if (status != 200) { 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(); return con.getInputStream();
} }