Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / GenerateKeypairCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
6
7 import net.pterodactylus.fcp.FcpKeyPair;
8 import net.pterodactylus.fcp.GenerateSSK;
9 import net.pterodactylus.fcp.SSKKeypair;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.ListeningExecutorService;
13 import com.google.common.util.concurrent.MoreExecutors;
14
15 /**
16  * Implementation of the {@link GenerateKeypairCommand}.
17  *
18  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
19  */
20 class GenerateKeypairCommandImpl implements GenerateKeypairCommand {
21
22         private final ListeningExecutorService threadPool;
23         private final ConnectionSupplier connectionSupplier;
24
25         GenerateKeypairCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
26                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
27                 this.connectionSupplier = connectionSupplier;
28         }
29
30         @Override
31         public ListenableFuture<FcpKeyPair> execute() {
32                 return threadPool.submit(this::executeDialog);
33         }
34
35         private FcpKeyPair executeDialog() throws InterruptedException, ExecutionException, IOException {
36                 try (FcpKeyPairDialog fcpKeyPairDialog = new FcpKeyPairDialog()) {
37                         return fcpKeyPairDialog.send(new GenerateSSK()).get();
38                 }
39         }
40
41         private class FcpKeyPairDialog extends FcpDialog<FcpKeyPair> {
42
43                 public FcpKeyPairDialog() throws IOException {
44                         super(GenerateKeypairCommandImpl.this.threadPool, GenerateKeypairCommandImpl.this.connectionSupplier.get(), null);
45                 }
46
47                 @Override
48                 protected void consumeSSKKeypair(SSKKeypair sskKeypair) {
49                         setResult(new FcpKeyPair(sskKeypair.getRequestURI(), sskKeypair.getInsertURI()));
50                 }
51
52         }
53
54 }