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

52 lines
1.6 KiB
Java

// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.wkd.discovery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pgp.wkd.WKDAddress;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
public abstract class AbstractUriCertificateFetcher implements CertificateFetcher {
private static final Logger LOGGER = LoggerFactory.getLogger(CertificateFetcher.class);
@Override
public InputStream fetchCertificate(WKDAddress address, DiscoveryMethod method) throws IOException {
URI uri = address.getUri(method);
try {
return fetchFromUri(uri);
} catch (IOException e) {
LOGGER.debug("Could not fetch key using " + method + " method from " + uri, e);
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;
}
}
/**
* 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
*/
protected abstract InputStream fetchFromUri(URI uri) throws IOException;
}