Store and re-throw exception.
[jSite2.git] / src / net / pterodactylus / jsite / core / NodeManager.java
index a73a563..db63df9 100644 (file)
@@ -40,6 +40,7 @@ import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.fcp.SSKKeypair;
 import net.pterodactylus.fcp.highlevel.FcpClient;
 import net.pterodactylus.fcp.highlevel.FcpException;
 import net.pterodactylus.jsite.util.IdGenerator;
@@ -378,6 +379,18 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener {
        }
 
        /**
+        * Returns the FCP client for the given node.
+        *
+        * @param node
+        *            The node to get the FCP client for
+        * @return The FCP client for the given node, or {@code null} if the node
+        *         does not have an associated FCP client
+        */
+       FcpClient getFcpClient(Node node) {
+               return nodeClients.get(node);
+       }
+
+       /**
         * Generates a new SSK key pair.
         *
         * @return An array with the private key at index <code>0</code> and the
@@ -385,14 +398,23 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener {
         * @throws IOException
         *             if an I/O error occurs communicating with the node
         * @throws JSiteException
-        *             if there is a problem with the node
+        *             if there is no connected node
         */
        public String[] generateKeyPair() throws IOException, JSiteException {
                logger.log(Level.FINEST, "generateKeyPair()");
                if (nodes.isEmpty()) {
                        throw new NoNodeException("no node configured");
                }
-               return null;
+               FcpException fcpException = null;
+               for (FcpClient fcpClient : nodeClients.values()) {
+                       try {
+                               SSKKeypair sskKeypair = fcpClient.generateKeyPair();
+                               return new String[] { sskKeypair.getInsertURI(), sskKeypair.getRequestURI() };
+                       } catch (FcpException fcpe1) {
+                               fcpException = fcpe1;
+                       }
+               }
+               throw new JSiteException("Could not get SSK key pair from any node.", fcpException);
        }
 
        //