Even more refactoring

This commit is contained in:
Paul Schaub 2022-03-21 11:25:03 +01:00
parent f04a322ac4
commit c2d4d283bc
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
14 changed files with 140 additions and 49 deletions

View file

@ -1,18 +0,0 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.wkd.cli;
public class CertNotFetchableException extends RuntimeException {
public static final int ERROR_CODE = 3;
public CertNotFetchableException(String message) {
super(message);
}
public CertNotFetchableException(String message, Throwable e) {
super(message, e);
}
}

View file

@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.wkd.cli;
import java.io.IOException;
/**
* {@link RuntimeException} wrapper for {@link IOException}.
* Background: We want to throw {@link IOException IOExceptions} in {@link Runnable#run()}.
*/
public class RuntimeIOException extends RuntimeException {
private final IOException ioException;
public RuntimeIOException(IOException ioe) {
super(ioe);
this.ioException = ioe;
}
public IOException getIoException() {
return ioException;
}
}

View file

@ -4,7 +4,8 @@
package pgp.wkd.cli;
import pgp.wkd.MissingUserIdException;
import pgp.wkd.exception.CertNotFetchableException;
import pgp.wkd.exception.RejectedCertificateException;
import pgp.wkd.cli.command.Fetch;
import picocli.CommandLine;
@ -28,8 +29,8 @@ public class WKDCLI {
.setExitCodeExceptionMapper(new CommandLine.IExitCodeExceptionMapper() {
@Override
public int getExitCode(Throwable exception) {
if (exception instanceof MissingUserIdException) {
return MissingUserIdException.ERROR_CODE;
if (exception instanceof RejectedCertificateException) {
return ((RejectedCertificateException) exception).getErrorCode();
} else if (exception instanceof CertNotFetchableException) {
return CertNotFetchableException.ERROR_CODE;
}

View file

@ -5,17 +5,15 @@
package pgp.wkd.cli.command;
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.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.HttpsCertificateDiscoverer;
import pgp.wkd.cli.RuntimeIOException;
import pgp.wkd.discovery.CertificateDiscoverer;
import pgp.wkd.discovery.CertificateFetcher;
import pgp.wkd.discovery.DiscoveryResult;
import pgp.wkd.discovery.HttpsUrlConnectionCertificateFetcher;
import pgp.wkd.exception.MalformedUserIdException;
import picocli.CommandLine;
import java.io.IOException;
@ -51,24 +49,14 @@ public class Fetch implements Runnable {
WKDAddress address = addressFromUserId(userId);
DiscoveryResult result = certificateDiscoverer.discover(address);
if (!result.isSuccessful()) {
throw new CertNotFetchableException("Cannot fetch cert.");
}
OutputStream outputStream = armor ? new ArmoredOutputStream(System.out) : System.out;
try {
if (armor) {
OutputStream out = new ArmoredOutputStream(System.out);
for (Certificate certificate : result.getCertificates()) {
Streams.pipeAll(certificate.getInputStream(), out);
}
out.close();
} else {
for (Certificate certificate : result.getCertificates()) {
Streams.pipeAll(certificate.getInputStream(), System.out);
}
result.write(outputStream);
if (outputStream instanceof ArmoredOutputStream) {
outputStream.close();
}
} catch (IOException e) {
throw new CertNotFetchableException("Certificate cannot be fetched.", e);
throw new RuntimeIOException(e);
}
}