Name service.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / FreenetInterface.java
1 /*
2  * FreenetSone - FreenetInterface.java - Copyright © 2010 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import java.util.HashMap;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 import net.pterodactylus.util.logging.Logging;
25 import net.pterodactylus.util.service.AbstractService;
26 import freenet.client.FetchException;
27 import freenet.client.FetchResult;
28 import freenet.client.HighLevelSimpleClient;
29 import freenet.client.InsertException;
30 import freenet.keys.FreenetURI;
31 import freenet.node.Node;
32
33 /**
34  * Contains all necessary functionality for interacting with the Freenet node.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class FreenetInterface extends AbstractService {
39
40         /** The logger. */
41         private static final Logger logger = Logging.getLogger(FreenetInterface.class);
42
43         /** The node to interact with. */
44         @SuppressWarnings("unused")
45         private final Node node;
46
47         /** The high-level client to use for requests. */
48         private final HighLevelSimpleClient client;
49
50         /**
51          * Creates a new Freenet interface.
52          *
53          * @param node
54          *            The node to interact with
55          * @param client
56          *            The high-level client
57          */
58         public FreenetInterface(Node node, HighLevelSimpleClient client) {
59                 super("Sone Freenet Interface");
60                 this.node = node;
61                 this.client = client;
62         }
63
64         //
65         // ACTIONS
66         //
67
68         /**
69          * Fetches the given URI.
70          *
71          * @param uri
72          *            The URI to fetch
73          * @return The result of the fetch, or {@code null} if an error occured
74          */
75         public FetchResult fetchUri(FreenetURI uri) {
76                 logger.entering(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", uri);
77                 FetchResult fetchResult = null;
78                 try {
79                         fetchResult = client.fetch(uri);
80                 } catch (FetchException fe1) {
81                         logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1);
82                 } finally {
83                         logger.exiting(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", fetchResult);
84                 }
85                 return fetchResult;
86         }
87
88         /**
89          * Creates a key pair.
90          *
91          * @return The request key at index 0, the insert key at index 1
92          */
93         public String[] generateKeyPair() {
94                 FreenetURI[] keyPair = client.generateKeyPair("");
95                 return new String[] { keyPair[1].toString(), keyPair[0].toString() };
96         }
97
98         /**
99          * Inserts a directory into Freenet.
100          *
101          * @param insertUri
102          *            The insert URI
103          * @param manifestEntries
104          *            The directory entries
105          * @param defaultFile
106          *            The name of the default file
107          * @return The generated URI
108          * @throws SoneException
109          *             if an insert error occurs
110          */
111         public FreenetURI insertDirectory(FreenetURI insertUri, HashMap<String, Object> manifestEntries, String defaultFile) throws SoneException {
112                 try {
113                         return client.insertManifest(insertUri, manifestEntries, defaultFile);
114                 } catch (InsertException ie1) {
115                         throw new SoneException(null, ie1);
116                 }
117         }
118
119 }