From: David ‘Bombe’ Roden Date: Fri, 16 Oct 2015 06:30:50 +0000 (+0200) Subject: Send generated URIs on ClientPutDiskDir to registered consumers X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6090cca7d3eedfc2c8e41c8ff162f4705519705c;p=jFCPlib.git Send generated URIs on ClientPutDiskDir to registered consumers --- diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java index dad6750..1bcd116 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommand.java @@ -15,6 +15,7 @@ import net.pterodactylus.fcp.RequestProgress; public interface ClientPutDiskDirCommand { ClientPutDiskDirCommand onProgress(Consumer requestProgressConsumer); + ClientPutDiskDirCommand onKeyGenerated(Consumer keyGenerated); WithUri fromDirectory(File directory); diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java index 2cf10c7..d223da2 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutDiskDirCommandImpl.java @@ -18,6 +18,7 @@ import net.pterodactylus.fcp.ProtocolError; import net.pterodactylus.fcp.PutSuccessful; import net.pterodactylus.fcp.RequestProgress; import net.pterodactylus.fcp.SimpleProgress; +import net.pterodactylus.fcp.URIGenerated; import net.pterodactylus.fcp.Verbosity; import com.google.common.util.concurrent.ListeningExecutorService; @@ -36,6 +37,7 @@ public class ClientPutDiskDirCommandImpl implements ClientPutDiskDirCommand { private final AtomicReference directory = new AtomicReference<>(); private final AtomicReference uri = new AtomicReference<>(); private final List> requestProgressConsumers = new CopyOnWriteArrayList<>(); + private final List> keyGeneratedConsumers = new CopyOnWriteArrayList<>(); public ClientPutDiskDirCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier identifierGenerator) { this.threadPool = MoreExecutors.listeningDecorator(threadPool); @@ -50,6 +52,12 @@ public class ClientPutDiskDirCommandImpl implements ClientPutDiskDirCommand { } @Override + public ClientPutDiskDirCommand onKeyGenerated(Consumer keyGenerated) { + keyGeneratedConsumers.add(Objects.requireNonNull(keyGenerated)); + return this; + } + + @Override public WithUri fromDirectory(File directory) { this.directory.set(Objects.requireNonNull(directory).getPath()); return this::uri; @@ -97,6 +105,11 @@ public class ClientPutDiskDirCommandImpl implements ClientPutDiskDirCommand { } @Override + protected void consumeURIGenerated(URIGenerated uriGenerated) { + keyGeneratedConsumers.forEach(consumer -> consumer.accept(uriGenerated.getURI())); + } + + @Override protected void consumeProtocolError(ProtocolError protocolError) { finish(); } diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 77573c0..0a39451 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -1706,6 +1706,18 @@ public class DefaultFcpClientTest { )); } + @Test + public void generatedUriIsSentToConsumerCorrectly() throws InterruptedException, ExecutionException, IOException { + List generatedKeys = new ArrayList<>(); + Future> key = fcpClient.clientPutDiskDir().onKeyGenerated(generatedKeys::add) + .fromDirectory(new File("")).uri("CHK@").execute(); + connectAndAssert(this::matchesClientPutDiskDir); + replyWithGeneratedUri(); + replyWithPutSuccessful(identifier); + assertThat(key.get().get().getKey(), is("KSK@foo.txt")); + assertThat(generatedKeys, contains("KSK@foo.txt")); + } + private Matcher> matchesClientPutDiskDir(String... additionalLines) { List lines = new ArrayList<>(Arrays.asList("Identifier=" + identifier, "URI=CHK@", "Filename=" + new File("").getPath())); Arrays.asList(additionalLines).forEach(lines::add);