Implement GetIdentity message.
[jFCPlib.git] / src / net / pterodactylus / fcp / plugin / WebOfTrustPlugin.java
1 /*
2  * jFCPlib - WebOfTrustPlugin.java -
3  * Copyright © 2009 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.fcp.plugin;
21
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28 import net.pterodactylus.fcp.highlevel.FcpClient;
29 import net.pterodactylus.fcp.highlevel.FcpException;
30
31 /**
32  * Simplifies handling of the web-of-trust plugin.
33  *
34  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35  */
36 public class WebOfTrustPlugin {
37
38         /** The FCP client to use. */
39         private final FcpClient fcpClient;
40
41         /**
42          * Creates a new web-of-trust plugin wrapper around the given FCP client.
43          *
44          * @param fcpClient
45          *            The FCP client to use for communication with the web-of-trust
46          *            plugin
47          */
48         public WebOfTrustPlugin(FcpClient fcpClient) {
49                 this.fcpClient = fcpClient;
50         }
51
52         /**
53          * Creates a new identity.
54          *
55          * @param nickname
56          *            The nickname of the new identity
57          * @param context
58          *            The context for the new identity
59          * @param publishTrustList
60          *            {@code true} if the new identity should publish its trust list
61          * @return The new identity
62          * @throws IOException
63          *             if an I/O error occurs
64          * @throws FcpException
65          *             if an FCP error occurs
66          */
67         public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList) throws IOException, FcpException {
68                 return createIdentity(nickname, context, publishTrustList, null, null);
69         }
70
71         /**
72          * Creates a new identity from the given request and insert URI.
73          *
74          * @param nickname
75          *            The nickname of the new identity
76          * @param context
77          *            The context for the new identity
78          * @param publishTrustList
79          *            {@code true} if the new identity should publish its trust list
80          * @param requestUri
81          *            The request URI of the identity
82          * @param insertUri
83          *            The insert URI of the identity
84          * @return The new identity
85          * @throws IOException
86          *             if an I/O error occurs
87          * @throws FcpException
88          *             if an FCP error occurs
89          */
90         public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList, String requestUri, String insertUri) throws IOException, FcpException {
91                 Map<String, String> parameters = new HashMap<String, String>();
92                 parameters.put("Message", "CreateIdentity");
93                 parameters.put("Nickname", nickname);
94                 parameters.put("Context", context);
95                 parameters.put("PublishTrustList", String.valueOf(publishTrustList));
96                 if ((requestUri != null) && (insertUri != null)) {
97                         parameters.put("RequestURI", requestUri);
98                         parameters.put("InsertURI", insertUri);
99                 }
100                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", parameters);
101                 if (!replies.get("Message").equals("IdentityCreated")) {
102                         throw new FcpException("WebOfTrust Plugin did not reply with “IdentityCreated” message!");
103                 }
104                 String identifier = replies.get("ID");
105                 String newRequestUri = replies.get("RequestURI");
106                 String newInsertUri = replies.get("InsertURI");
107                 return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri);
108         }
109
110         /**
111          * Returns all own identities of the web-of-trust plugins. Almost all other
112          * commands require an {@link OwnIdentity} to return meaningful values.
113          *
114          * @return All own identities of the web-of-trust plugin
115          * @throws IOException
116          *             if an I/O error occurs
117          * @throws FcpException
118          *             if an FCP error occurs
119          */
120         public Set<OwnIdentity> getOwnIdentites() throws IOException, FcpException {
121                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetOwnIdentities"));
122                 if (!replies.get("Message").equals("OwnIdentities")) {
123                         throw new FcpException("WebOfTrust Plugin did not reply with “OwnIdentities” message!");
124                 }
125                 Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
126                 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
127                         String identity = replies.get("Identity" + identityIndex);
128                         String nickname = replies.get("Nickname" + identityIndex);
129                         String requestUri = replies.get("RequestURI" + identityIndex);
130                         String insertUri = replies.get("InsertURI" + identityIndex);
131                         ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri));
132                 }
133                 return ownIdentities;
134         }
135
136         /**
137          * Returns the identity with the given identifier and the trust values
138          * depending on the given own identity.
139          *
140          * @param ownIdentity
141          *            The own identity that is used to calculate trust values
142          * @param identifier
143          *            The identifier of the identity to get
144          * @return The request identity
145          * @throws IOException
146          *             if an I/O error occurs
147          * @throws FcpException
148          *             if an FCP error occurs
149          */
150         public Identity getIdentity(OwnIdentity ownIdentity, String identifier) throws IOException, FcpException {
151                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentity", "TreeOwner", ownIdentity.getIdentifier(), "Identity", identifier));
152                 if (!replies.get("Message").equals("Identity")) {
153                         throw new FcpException("WebOfTrust Plugin did not reply with “Identity” message!");
154                 }
155                 String nickname = replies.get("Nickname");
156                 String requestUri = replies.get("RequestURI");
157                 Byte trust = null;
158                 try {
159                         trust = Byte.valueOf(replies.get("Trust"));
160                 } catch (NumberFormatException nfe1) {
161                         /* ignore. */
162                 }
163                 Integer score = null;
164                 try {
165                         score = Integer.valueOf(replies.get("Score"));
166                 } catch (NumberFormatException nfe1) {
167                         /* ignore. */
168                 }
169                 Integer rank = null;
170                 try {
171                         rank = Integer.valueOf(replies.get("Rank"));
172                 } catch (NumberFormatException nfe1) {
173                         /* ignore. */
174                 }
175                 return new Identity(identifier, nickname, requestUri, trust, score, rank);
176         }
177
178         //
179         // PRIVATE METHODS
180         //
181
182         /**
183          * Creates a map from each pair of parameters in the given array.
184          *
185          * @param parameters
186          *            The array of parameters
187          * @return The map created from the array
188          * @throws ArrayIndexOutOfBoundsException
189          *             if the given parameter array does not contains an even number
190          *             of elements
191          */
192         private Map<String, String> createParameters(String... parameters) throws ArrayIndexOutOfBoundsException {
193                 Map<String, String> parameterMap = new HashMap<String, String>();
194                 for (int index = 0; index < parameters.length; index += 2) {
195                         parameterMap.put(parameters[index], parameters[index + 1]);
196                 }
197                 return parameterMap;
198         }
199
200         /**
201          * Wrapper around a web-of-trust identity.
202          *
203          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
204          */
205         public static class Identity {
206
207                 /** The identity’s identifier. */
208                 private final String identifier;
209
210                 /** The identity’s nickname. */
211                 private final String nickname;
212
213                 /** The identity’s request URI. */
214                 private final String requestUri;
215
216                 /** The identity’s trust value. */
217                 private final Byte trust;
218
219                 /** The identity’s score value. */
220                 private final Integer score;
221
222                 /** The identity’s rank. */
223                 private final Integer rank;
224
225                 /**
226                  * Creates a new identity.
227                  *
228                  * @param identifier
229                  *            The identifies of the identity
230                  * @param nickname
231                  *            The nickname of the identity
232                  * @param requestUri
233                  *            The request URI of the identity
234                  * @param trust
235                  *            The trust value of the identity
236                  * @param score
237                  *            The score value of the identity
238                  * @param rank
239                  *            The rank of the identity
240                  */
241                 public Identity(String identifier, String nickname, String requestUri, Byte trust, Integer score, Integer rank) {
242                         this.identifier = identifier;
243                         this.nickname = nickname;
244                         this.requestUri = requestUri;
245                         this.trust = trust;
246                         this.score = score;
247                         this.rank = rank;
248                 }
249
250                 /**
251                  * Returns the identifier of this identity.
252                  *
253                  * @return This identity’s identifier
254                  */
255                 public String getIdentifier() {
256                         return identifier;
257                 }
258
259                 /**
260                  * Returns the nickname of this identity.
261                  *
262                  * @return This identity’s nickname
263                  */
264                 public String getNickname() {
265                         return nickname;
266                 }
267
268                 /**
269                  * Returns the request URI of this identity.
270                  *
271                  * @return This identity’s request URI
272                  */
273                 public String getRequestUri() {
274                         return requestUri;
275                 }
276
277                 /**
278                  * Returns the trust value of this identity.
279                  *
280                  * @return This identity’s trust value, or {@code null} if this
281                  *         identity’s trust value is not known
282                  */
283                 public Byte getTrust() {
284                         return trust;
285                 }
286
287                 /**
288                  * Returns the score value of this identity.
289                  *
290                  * @return This identity’s score value, or {@code null} if this
291                  *         identity’s score value is not known
292                  */
293                 public Integer getScore() {
294                         return score;
295                 }
296
297                 /**
298                  * Returns the rank of this identity.
299                  *
300                  * @return This identity’s rank, or {@code null} if this identity’s rank
301                  *         is not known
302                  */
303                 public Integer getRank() {
304                         return rank;
305                 }
306
307         }
308
309         /**
310          * Wrapper around a web-of-trust own identity.
311          *
312          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
313          */
314         public static class OwnIdentity {
315
316                 /** The identity’s identifier. */
317                 private final String identifier;
318
319                 /** The identity’s nickname. */
320                 private final String nickname;
321
322                 /** The identity’s request URI. */
323                 private final String requestUri;
324
325                 /** The identity’s insert URI. */
326                 private final String insertUri;
327
328                 /**
329                  * Creates a new web-of-trust own identity.
330                  *
331                  * @param identifier
332                  *            The identifier of the identity
333                  * @param nickname
334                  *            The nickname of the identity
335                  * @param requestUri
336                  *            The request URI of the identity
337                  * @param insertUri
338                  *            The insert URI of the identity
339                  */
340                 public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri) {
341                         this.identifier = identifier;
342                         this.nickname = nickname;
343                         this.requestUri = requestUri;
344                         this.insertUri = insertUri;
345                 }
346
347                 /**
348                  * Returns the identifier of this identity.
349                  *
350                  * @return This identity’s identifier
351                  */
352                 public String getIdentifier() {
353                         return identifier;
354                 }
355
356                 /**
357                  * Returns the nickname of this identity.
358                  *
359                  * @return This identity’s nickname
360                  */
361                 public String getNickname() {
362                         return nickname;
363                 }
364
365                 /**
366                  * Returns the request URI of this identity.
367                  *
368                  * @return This identity’s request URI
369                  */
370                 public String getRequestUri() {
371                         return requestUri;
372                 }
373
374                 /**
375                  * Returns the insert URI of this identity.
376                  *
377                  * @return This identity’s insert URI
378                  */
379                 public String getInsertUri() {
380                         return insertUri;
381                 }
382
383         }
384
385 }