package net.pterodactylus.sone.core;
+import java.util.HashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.service.AbstractService;
+import freenet.client.FetchException;
+import freenet.client.FetchResult;
import freenet.client.HighLevelSimpleClient;
+import freenet.client.InsertException;
+import freenet.keys.FreenetURI;
import freenet.node.Node;
/**
*/
public class FreenetInterface extends AbstractService {
+ /** The logger. */
+ private static final Logger logger = Logging.getLogger(FreenetInterface.class);
+
/** The node to interact with. */
+ @SuppressWarnings("unused")
private final Node node;
/** The high-level client to use for requests. */
*
* @param node
* The node to interact with
+ * @param client
+ * The high-level client
*/
public FreenetInterface(Node node, HighLevelSimpleClient client) {
+ super("Sone Freenet Interface");
this.node = node;
this.client = client;
}
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Fetches the given URI.
+ *
+ * @param uri
+ * The URI to fetch
+ * @return The result of the fetch, or {@code null} if an error occured
+ */
+ public FetchResult fetchUri(FreenetURI uri) {
+ logger.entering(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", uri);
+ FetchResult fetchResult = null;
+ try {
+ fetchResult = client.fetch(uri);
+ } catch (FetchException fe1) {
+ logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1);
+ } finally {
+ logger.exiting(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", fetchResult);
+ }
+ return fetchResult;
+ }
+
+ /**
+ * Creates a key pair.
+ *
+ * @return The request key at index 0, the insert key at index 1
+ */
+ public String[] generateKeyPair() {
+ FreenetURI[] keyPair = client.generateKeyPair("");
+ return new String[] { keyPair[1].toString(), keyPair[0].toString() };
+ }
+
+ /**
+ * Inserts a directory into Freenet.
+ *
+ * @param insertUri
+ * The insert URI
+ * @param manifestEntries
+ * The directory entries
+ * @param defaultFile
+ * The name of the default file
+ * @return The generated URI
+ * @throws SoneException
+ * if an insert error occurs
+ */
+ public FreenetURI insertDirectory(FreenetURI insertUri, HashMap<String, Object> manifestEntries, String defaultFile) throws SoneException {
+ try {
+ return client.insertManifest(insertUri, manifestEntries, defaultFile);
+ } catch (InsertException ie1) {
+ throw new SoneException(null, ie1);
+ }
+ }
+
}