X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Ffcp%2Fquelaton%2FClientPutCommandImpl.java;h=0228cce2b9432ae265563117d73446a39f051f97;hb=0c86b219616126f1a020e3b6a312156cad7c46ee;hp=43d6c99e858532ebb01c92d5f38ed6e24d05df03;hpb=1ce220842b96db2ea57e456c70977620943cb5de;p=jFCPlib.git diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java index 43d6c99..0228cce 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/ClientPutCommandImpl.java @@ -3,6 +3,7 @@ package net.pterodactylus.fcp.quelaton; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.Objects; import java.util.Optional; import java.util.concurrent.ExecutorService; @@ -13,8 +14,13 @@ import java.util.concurrent.atomic.AtomicReference; import net.pterodactylus.fcp.ClientPut; import net.pterodactylus.fcp.FcpMessage; import net.pterodactylus.fcp.Key; +import net.pterodactylus.fcp.ProtocolError; import net.pterodactylus.fcp.PutFailed; import net.pterodactylus.fcp.PutSuccessful; +import net.pterodactylus.fcp.TestDDAComplete; +import net.pterodactylus.fcp.TestDDAReply; +import net.pterodactylus.fcp.TestDDARequest; +import net.pterodactylus.fcp.TestDDAResponse; import net.pterodactylus.fcp.UploadFrom; import com.google.common.util.concurrent.ListenableFuture; @@ -48,31 +54,31 @@ class ClientPutCommandImpl implements ClientPutCommand { } @Override - public Keyed> redirectTo(Key key) { - this.redirectUri.set(Objects.requireNonNull(key, "key must not be null").getKey()); + public WithUri> redirectTo(String uri) { + this.redirectUri.set(Objects.requireNonNull(uri, "uri must not be null")); return this::key; } @Override - public Keyed> from(File file) { + public WithUri> from(File file) { this.file.set(Objects.requireNonNull(file, "file must not be null")); return this::key; } @Override - public Lengthed>> from(InputStream inputStream) { + public WithLength>> from(InputStream inputStream) { payload.set(Objects.requireNonNull(inputStream, "inputStream must not be null")); return this::length; } - private Keyed> length(long length) { + private WithUri> length(long length) { this.length.set(length); return this::key; } - private ListenableFuture> key(Key key) { + private ListenableFuture> key(String uri) { String identifier = new RandomIdentifierGenerator().generate(); - ClientPut clientPut = createClientPutCommand(key.getKey(), identifier); + ClientPut clientPut = createClientPutCommand(uri, identifier); return threadPool.submit(() -> new ClientPutReplySequence().send(clientPut).get()); } @@ -112,7 +118,8 @@ class ClientPutCommandImpl implements ClientPutCommand { private class ClientPutReplySequence extends FcpReplySequence> { - private final AtomicReference identifier = new AtomicReference<>(); + private final AtomicReference originalClientPut = new AtomicReference<>(); + private final AtomicReference directory = new AtomicReference<>(); private final AtomicReference finalKey = new AtomicReference<>(); private final AtomicBoolean putFinished = new AtomicBoolean(); @@ -132,29 +139,51 @@ class ClientPutCommandImpl implements ClientPutCommand { @Override public ListenableFuture> send(FcpMessage fcpMessage) throws IOException { - identifier.set(fcpMessage.getField("Identifier")); + originalClientPut.set(fcpMessage); + String filename = fcpMessage.getField("Filename"); + if (filename != null) { + directory.set(new File(filename).getParent()); + } return super.send(fcpMessage); } @Override protected void consumePutSuccessful(PutSuccessful putSuccessful) { - if (putSuccessful.getIdentifier().equals(identifier.get())) { - finalKey.set(new Key(putSuccessful.getURI())); - putFinished.set(true); - } + finalKey.set(new Key(putSuccessful.getURI())); + putFinished.set(true); } @Override protected void consumePutFailed(PutFailed putFailed) { - if (putFailed.getIdentifier().equals(identifier.get())) { + putFinished.set(true); + } + + @Override + protected void consumeProtocolError(ProtocolError protocolError) { + if (protocolError.getCode() == 25) { + setIdentifier(directory.get()); + sendMessage(new TestDDARequest(directory.get(), true, false)); + } else { putFinished.set(true); } } @Override - protected void consumeConnectionClosed(Throwable throwable) { - putFinished.set(true); + protected void consumeTestDDAReply(TestDDAReply testDDAReply) { + try { + String readContent = Files.readAllLines(new File(testDDAReply.getReadFilename()).toPath()).get(0); + sendMessage(new TestDDAResponse(directory.get(), readContent)); + } catch (IOException e) { + sendMessage(new TestDDAResponse(directory.get(), "failed-to-read")); + } } + + @Override + protected void consumeTestDDAComplete(TestDDAComplete testDDAComplete) { + setIdentifier(originalClientPut.get().getField("Identifier")); + sendMessage(originalClientPut.get()); + } + } }