mirror of
https://codeberg.org/PGPainless/wkd-java.git
synced 2024-11-21 23:02:05 +01:00
More refactoring
This commit is contained in:
parent
a6b52562a8
commit
f04a322ac4
6 changed files with 32 additions and 31 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<CertificateAndUserIds> read(InputStream inputStream) throws IOException {
|
||||
List<CertificateAndUserIds> certificatesAndUserIds = new ArrayList<>();
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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();
|
||||
}
|
Loading…
Reference in a new issue