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-21 11:25:03 +01:00
|
|
|
import pgp.wkd.exception.CertNotFetchableException;
|
2022-03-10 16:56:46 +01:00
|
|
|
|
2022-03-17 15:27:28 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2022-03-21 11:25:03 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2022-03-10 16:56:46 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-03-17 15:27:28 +01:00
|
|
|
public class DiscoveryResult {
|
2022-03-10 16:56:46 +01:00
|
|
|
|
2022-03-17 15:27:28 +01:00
|
|
|
private List<DiscoveryResponse> items;
|
2022-03-10 16:56:46 +01:00
|
|
|
|
2022-03-17 15:27:28 +01:00
|
|
|
public DiscoveryResult(@Nonnull List<DiscoveryResponse> items) {
|
2022-03-10 16:56:46 +01:00
|
|
|
this.items = items;
|
|
|
|
}
|
|
|
|
|
2022-03-17 15:27:28 +01:00
|
|
|
@Nonnull
|
2022-03-10 16:56:46 +01:00
|
|
|
public List<Certificate> getCertificates() {
|
|
|
|
List<Certificate> certificates = new ArrayList<>();
|
|
|
|
|
2022-03-17 15:27:28 +01:00
|
|
|
for (DiscoveryResponse item : items) {
|
2022-03-10 16:56:46 +01:00
|
|
|
if (item.isSuccessful()) {
|
|
|
|
certificates.addAll(item.getCertificates());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return certificates;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSuccessful() {
|
2022-03-17 15:27:28 +01:00
|
|
|
for (DiscoveryResponse item : items) {
|
2022-03-10 16:56:46 +01:00
|
|
|
if (item.isSuccessful() && item.hasCertificates()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:25:03 +01:00
|
|
|
/**
|
|
|
|
* Write out the (successful) result (certificates) to the given {@link OutputStream}.
|
|
|
|
* This method does not close the output stream.
|
|
|
|
*
|
|
|
|
* @param outputStream output stream
|
|
|
|
*/
|
|
|
|
public void write(OutputStream outputStream) throws IOException {
|
|
|
|
if (!isSuccessful()) {
|
|
|
|
throw new CertNotFetchableException("Cannot fetch cert.");
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int read;
|
|
|
|
for (Certificate certificate : getCertificates()) {
|
|
|
|
InputStream certIn = certificate.getInputStream();
|
|
|
|
while ((read = certIn.read(buf)) != -1) {
|
|
|
|
outputStream.write(buf, 0, read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 15:27:28 +01:00
|
|
|
@Nonnull
|
|
|
|
public List<DiscoveryResponse> getItems() {
|
2022-03-10 16:56:46 +01:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2022-03-17 15:27:28 +01:00
|
|
|
@Nonnull
|
|
|
|
public List<DiscoveryResponse> getFailedItems() {
|
|
|
|
List<DiscoveryResponse> fails = new ArrayList<>();
|
|
|
|
for (DiscoveryResponse item : items) {
|
2022-03-10 16:56:46 +01:00
|
|
|
if (!item.isSuccessful()) {
|
|
|
|
fails.add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fails;
|
|
|
|
}
|
|
|
|
}
|