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

102 lines
3.0 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 pgp.certificate_store.Certificate;
2022-03-17 15:27:28 +01:00
import pgp.wkd.RejectedCertificate;
import pgp.wkd.WKDAddress;
2022-03-10 16:56:46 +01:00
2022-03-17 15:27:28 +01:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2022-03-21 13:11:00 +01:00
import java.net.URI;
2022-03-17 15:27:28 +01:00
import java.util.Collections;
2022-03-10 16:56:46 +01:00
import java.util.List;
2022-03-17 15:27:28 +01:00
public final class DiscoveryResponse {
2022-03-10 16:56:46 +01:00
private final DiscoveryMethod method;
private final WKDAddress address;
private final List<Certificate> certificates;
private final List<RejectedCertificate> rejectedCertificates;
2022-03-17 15:27:28 +01:00
private final Throwable fetchingFailure;
2022-03-10 16:56:46 +01:00
/**
2022-03-17 15:27:28 +01:00
* Constructor for a {@link DiscoveryResponse} object.
2022-03-10 16:56:46 +01:00
* @param method discovery method
* @param address wkd address used for discovery
* @param certificates list of successfully fetched certificates
* @param rejectedCertificates list of invalid fetched certificates (e.g. missing user-id)
2022-03-17 15:27:28 +01:00
* @param fetchingFailure general fetching error (e.g. connection error, 404...)
2022-03-10 16:56:46 +01:00
*/
2022-03-17 15:27:28 +01:00
private DiscoveryResponse(
2022-03-10 16:56:46 +01:00
DiscoveryMethod method,
WKDAddress address,
List<Certificate> certificates,
List<RejectedCertificate> rejectedCertificates,
2022-03-17 15:27:28 +01:00
Throwable fetchingFailure) {
2022-03-10 16:56:46 +01:00
this.method = method;
this.address = address;
this.certificates = certificates;
this.rejectedCertificates = rejectedCertificates;
2022-03-17 15:27:28 +01:00
this.fetchingFailure = fetchingFailure;
2022-03-10 16:56:46 +01:00
}
2022-03-17 15:27:28 +01:00
public static DiscoveryResponse success(
@Nonnull DiscoveryMethod method,
@Nonnull WKDAddress address,
@Nonnull List<Certificate> certificates,
@Nonnull List<RejectedCertificate> rejectedCertificates) {
return new DiscoveryResponse(method, address, certificates, rejectedCertificates, null);
2022-03-10 16:56:46 +01:00
}
2022-03-17 15:27:28 +01:00
public static DiscoveryResponse failure(
@Nonnull DiscoveryMethod method,
@Nonnull WKDAddress address,
@Nonnull Throwable fetchingFailure) {
return new DiscoveryResponse(method, address, Collections.emptyList(), Collections.emptyList(), fetchingFailure);
2022-03-10 16:56:46 +01:00
}
2022-03-17 15:27:28 +01:00
@Nonnull
2022-03-10 16:56:46 +01:00
public DiscoveryMethod getMethod() {
return method;
}
2022-03-17 15:27:28 +01:00
@Nonnull
2022-03-10 16:56:46 +01:00
public WKDAddress getAddress() {
return address;
}
2022-03-21 13:11:00 +01:00
public URI getUri() {
return getAddress().getUri(getMethod());
}
2022-03-10 16:56:46 +01:00
public boolean isSuccessful() {
2022-03-17 15:27:28 +01:00
return !hasFetchingFailure();
2022-03-10 16:56:46 +01:00
}
2022-03-17 15:27:28 +01:00
@Nonnull
2022-03-10 16:56:46 +01:00
public List<Certificate> getCertificates() {
return certificates;
}
2022-03-17 15:27:28 +01:00
@Nonnull
2022-03-10 16:56:46 +01:00
public List<RejectedCertificate> getRejectedCertificates() {
return rejectedCertificates;
}
2022-03-17 15:27:28 +01:00
@Nullable
public Throwable getFetchingFailure() {
return fetchingFailure;
2022-03-10 16:56:46 +01:00
}
public boolean hasCertificates() {
return certificates != null && !certificates.isEmpty();
}
2022-03-17 15:27:28 +01:00
public boolean hasFetchingFailure() {
return fetchingFailure != null;
2022-03-10 16:56:46 +01:00
}
}