import java.io.IOException;
import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Optional;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
-import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
import net.pterodactylus.fcp.AllData;
import net.pterodactylus.fcp.ClientGet;
import net.pterodactylus.fcp.Priority;
import net.pterodactylus.fcp.ReturnType;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
/**
* Implementation of the {@link ClientGetCommand}.
*
*/
class ClientGetCommandImpl implements ClientGetCommand {
- private final ExecutorService threadPool;
+ private final ListeningExecutorService threadPool;
private final ConnectionSupplier connectionSupplier;
+ private final Supplier<String> identifierGenerator;
+ private final List<Consumer<String>> onRedirects = new ArrayList<>();
- private String identifier;
private boolean ignoreDataStore;
private boolean dataStoreOnly;
private Long maxSize;
private boolean realTime;
private boolean global;
- public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
- this.threadPool = threadPool;
+ public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
+ this.threadPool = MoreExecutors.listeningDecorator(threadPool);
this.connectionSupplier = connectionSupplier;
+ this.identifierGenerator = identifierGenerator;
}
@Override
- public ClientGetCommand identifier(String identifier) {
- this.identifier = identifier;
+ public ClientGetCommand onRedirect(Consumer<String> onRedirect) {
+ onRedirects.add(onRedirect);
return this;
}
}
@Override
- public Future<Optional<Data>> uri(String uri) {
+ public Executable<Optional<Data>> uri(String uri) {
+ return () -> threadPool.submit(() -> execute(uri));
+ }
+
+ private Optional<Data> execute(String uri) throws InterruptedException, ExecutionException, IOException {
+ ClientGet clientGet = createClientGetCommand(identifierGenerator.get(), uri);
+ try (ClientGetDialog clientGetDialog = new ClientGetDialog()) {
+ return clientGetDialog.send(clientGet).get();
+ }
+ }
+
+ private ClientGet createClientGetCommand(String identifier, String uri) {
ClientGet clientGet = new ClientGet(uri, identifier, ReturnType.direct);
if (ignoreDataStore) {
clientGet.setIgnoreDataStore(true);
if (global) {
clientGet.setGlobal(true);
}
- return threadPool.submit(() -> {
- FcpReplySequence<Optional<Data>> replySequence =
- new ClientGetReplySequence();
- return replySequence.send(clientGet).get();
- });
+ return clientGet;
}
- private class ClientGetReplySequence extends FcpReplySequence<Optional<Data>> {
+ private class ClientGetDialog extends FcpDialog<Optional<Data>> {
- private final AtomicBoolean finished = new AtomicBoolean();
- private final AtomicBoolean failed = new AtomicBoolean();
-
- private final String identifier = ClientGetCommandImpl.this.identifier;
-
- private String contentType;
- private long dataLength;
- private InputStream payload;
-
- public ClientGetReplySequence() throws IOException {
- super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get());
+ public ClientGetDialog() throws IOException {
+ super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get(),
+ Optional.<Data>empty());
}
@Override
- protected boolean isFinished() {
- return finished.get() || failed.get();
+ protected void consumeAllData(AllData allData) {
+ synchronized (this) {
+ String contentType = allData.getContentType();
+ long dataLength = allData.getDataLength();
+ try {
+ InputStream payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
+ setResult(Optional.of(createData(contentType, dataLength, payload)));
+ } catch (IOException e) {
+ // TODO – logging
+ finish();
+ }
+ }
}
- @Override
- protected Optional<Data> getResult() {
- return failed.get() ? Optional.empty() : Optional.of(new Data() {
+ private Data createData(String contentType, long dataLength, InputStream payload) {
+ return new Data() {
@Override
public String getMimeType() {
return contentType;
public InputStream getInputStream() {
return payload;
}
- });
- }
-
- @Override
- protected void consumeAllData(AllData allData) {
- if (allData.getIdentifier().equals(identifier)) {
- synchronized (this) {
- contentType = allData.getContentType();
- dataLength = allData.getDataLength();
- try {
- payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
- finished.set(true);
- } catch (IOException e) {
- // TODO – logging
- failed.set(true);
- }
- }
- }
+ };
}
@Override
protected void consumeGetFailed(GetFailed getFailed) {
- if (getFailed.getIdentifier().equals(identifier)) {
- failed.set(true);
+ if (getFailed.getCode() == 27) {
+ onRedirects.forEach(onRedirect -> onRedirect.accept(getFailed.getRedirectURI()));
+ sendMessage(createClientGetCommand(getIdentifier(), getFailed.getRedirectURI()));
+ } else {
+ finish();
}
}
- @Override
- protected void consumeConnectionClosed(Throwable throwable) {
- failed.set(true);
- }
-
}
}