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

37 lines
1.1 KiB
Java
Raw Normal View History

// 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-19 14:36:33 +01:00
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
/**
2022-03-17 15:27:28 +01:00
* Implementation of {@link CertificateFetcher} using Java's {@link HttpURLConnection}.
*/
2022-03-19 14:36:33 +01:00
public class HttpsUrlConnectionCertificateFetcher extends AbstractUriCertificateFetcher {
2022-03-17 15:27:28 +01:00
public InputStream fetchFromUri(URI uri) throws IOException {
URL url = uri.toURL();
2022-03-19 14:36:33 +01:00
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
con.setInstanceFollowRedirects(false);
int status = con.getResponseCode();
if (status != 200) {
2022-03-19 14:36:33 +01:00
throw new ConnectException("Connecting to URL '" + uri + "' failed. Status: " + status);
}
return con.getInputStream();
}
}