wkd-java/wkd-java/src/main/java/pgp/wkd/discovery/AbstractUriCertificateFetch...

52 lines
1.6 KiB
Java
Raw Normal View History

2022-03-10 16:56:46 +01:00
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
2022-03-17 15:27:28 +01:00
package pgp.wkd.discovery;
2022-03-10 16:56:46 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2022-03-17 15:27:28 +01:00
import pgp.wkd.WKDAddress;
2022-03-10 16:56:46 +01:00
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
2022-03-17 15:27:28 +01:00
public abstract class AbstractUriCertificateFetcher implements CertificateFetcher {
2022-03-10 16:56:46 +01:00
2022-03-17 15:27:28 +01:00
private static final Logger LOGGER = LoggerFactory.getLogger(CertificateFetcher.class);
2022-03-10 16:56:46 +01:00
@Override
2022-03-17 15:27:28 +01:00
public InputStream fetchCertificate(WKDAddress address, DiscoveryMethod method) throws IOException {
2022-03-10 16:56:46 +01:00
URI uri = address.getUri(method);
try {
2022-03-17 15:27:28 +01:00
return fetchFromUri(uri);
2022-03-10 16:56:46 +01:00
} catch (IOException e) {
2022-03-17 15:27:28 +01:00
LOGGER.debug("Could not fetch key using " + method + " method from " + uri, e);
2022-03-10 16:56:46 +01:00
throw e;
}
}
@Override
public InputStream fetchPolicy(WKDAddress address, DiscoveryMethod method) throws IOException {
URI uri = address.getPolicyUri(method);
try {
return fetchFromUri(uri);
} catch (IOException e) {
LOGGER.debug("Could not fetch policy file using " + method + " method from " + uri, e);
throw e;
}
}
2022-03-10 16:56:46 +01:00
/**
* Fetch the contents of the file that the {@link URI} points to from the remote server.
* @param uri uri
* @return file contents
*
* @throws java.net.ConnectException in case the file or host does not exist
* @throws IOException in case of an IO-error
*/
2022-03-17 15:27:28 +01:00
protected abstract InputStream fetchFromUri(URI uri) throws IOException;
2022-03-10 16:56:46 +01:00
}