1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Optional;
6 import java.util.concurrent.ExecutionException;
7 import java.util.concurrent.ExecutorService;
8 import java.util.function.Supplier;
10 import net.pterodactylus.fcp.AllData;
11 import net.pterodactylus.fcp.ClientGet;
12 import net.pterodactylus.fcp.FcpUtils.TempInputStream;
13 import net.pterodactylus.fcp.GetFailed;
14 import net.pterodactylus.fcp.Priority;
15 import net.pterodactylus.fcp.ReturnType;
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
21 * Implementation of the {@link ClientGetCommand}.
23 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25 class ClientGetCommandImpl implements ClientGetCommand {
27 private final ListeningExecutorService threadPool;
28 private final ConnectionSupplier connectionSupplier;
29 private final Supplier<String> identifierGenerator;
31 private boolean ignoreDataStore;
32 private boolean dataStoreOnly;
34 private Priority priority;
35 private boolean realTime;
36 private boolean global;
38 public ClientGetCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
39 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
40 this.connectionSupplier = connectionSupplier;
41 this.identifierGenerator = identifierGenerator;
45 public ClientGetCommand ignoreDataStore() {
46 ignoreDataStore = true;
51 public ClientGetCommand dataStoreOnly() {
57 public ClientGetCommand maxSize(long maxSize) {
58 this.maxSize = maxSize;
63 public ClientGetCommand priority(Priority priority) {
64 this.priority = priority;
69 public ClientGetCommand realTime() {
75 public ClientGetCommand global() {
81 public Executable<Optional<Data>> uri(String uri) {
82 return () -> threadPool.submit(() -> execute(uri));
85 private Optional<Data> execute(String uri) throws InterruptedException, ExecutionException, IOException {
86 ClientGet clientGet = createClientGetCommand(uri);
87 try (ClientGetDialog clientGetDialog = new ClientGetDialog()) {
88 return clientGetDialog.send(clientGet).get();
92 private ClientGet createClientGetCommand(String uri) {
93 String identifier = identifierGenerator.get();
94 ClientGet clientGet = new ClientGet(uri, identifier, ReturnType.direct);
95 if (ignoreDataStore) {
96 clientGet.setIgnoreDataStore(true);
99 clientGet.setDataStoreOnly(true);
101 if (maxSize != null) {
102 clientGet.setMaxSize(maxSize);
104 if (priority != null) {
105 clientGet.setPriority(priority);
108 clientGet.setRealTimeFlag(true);
111 clientGet.setGlobal(true);
116 private class ClientGetDialog extends FcpDialog<Optional<Data>> {
118 public ClientGetDialog() throws IOException {
119 super(ClientGetCommandImpl.this.threadPool, ClientGetCommandImpl.this.connectionSupplier.get(), Optional.<Data>empty());
123 protected void consumeAllData(AllData allData) {
124 synchronized (this) {
125 String contentType = allData.getContentType();
126 long dataLength = allData.getDataLength();
128 InputStream payload = new TempInputStream(allData.getPayloadInputStream(), dataLength);
129 setResult(Optional.of(createData(contentType, dataLength, payload)));
130 } catch (IOException e) {
137 private Data createData(String contentType, long dataLength, InputStream payload) {
140 public String getMimeType() {
150 public InputStream getInputStream() {
157 protected void consumeGetFailed(GetFailed getFailed) {