2 * jFCPlib - WebOfTrustPlugin.java -
3 * Copyright © 2009 David Roden
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.
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.
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.
20 package net.pterodactylus.fcp.plugin;
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.HashSet;
28 import net.pterodactylus.fcp.highlevel.FcpClient;
29 import net.pterodactylus.fcp.highlevel.FcpException;
32 * Simplifies handling of the web-of-trust plugin.
34 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 public class WebOfTrustPlugin {
38 /** The FCP client to use. */
39 private final FcpClient fcpClient;
42 * Creates a new web-of-trust plugin wrapper around the given FCP client.
45 * The FCP client to use for communication with the web-of-trust
48 public WebOfTrustPlugin(FcpClient fcpClient) {
49 this.fcpClient = fcpClient;
53 * Creates a new identity.
56 * The nickname of the new identity
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
63 * if an I/O error occurs
64 * @throws FcpException
65 * if an FCP error occurs
67 public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList) throws IOException, FcpException {
68 return createIdentity(nickname, context, publishTrustList, null, null);
72 * Creates a new identity from the given request and insert URI.
75 * The nickname of the new identity
77 * The context for the new identity
78 * @param publishTrustList
79 * {@code true} if the new identity should publish its trust list
81 * The request URI of the identity
83 * The insert URI of the identity
84 * @return The new identity
86 * if an I/O error occurs
87 * @throws FcpException
88 * if an FCP error occurs
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);
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!");
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);
111 * Returns all own identities of the web-of-trust plugins. Almost all other
112 * commands require an {@link OwnIdentity} to return meaningful values.
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
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!");
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));
133 return ownIdentities;
137 * Returns the trust given to the identity with the given identifier by the
138 * given own identity.
141 * The own identity that is used to calculate trust values
143 * The identifier of the identity whose trust to get
144 * @return The request identity trust
145 * @throws IOException
146 * if an I/O error occurs
147 * @throws FcpException
148 * if an FCP error occurs
150 public CalculatedTrust getIdentityTrust(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!");
157 trust = Byte.valueOf(replies.get("Trust"));
158 } catch (NumberFormatException nfe1) {
161 Integer score = null;
163 score = Integer.valueOf(replies.get("Score"));
164 } catch (NumberFormatException nfe1) {
169 rank = Integer.valueOf(replies.get("Rank"));
170 } catch (NumberFormatException nfe1) {
173 return new CalculatedTrust(trust, score, rank);
177 * Adds a new identity by its request URI.
180 * The request URI of the identity to add
181 * @return The added identity
182 * @throws IOException
183 * if an I/O error occurs
184 * @throws FcpException
185 * if an FCP error occurs
187 public Identity addIdentity(String requestUri) throws IOException, FcpException {
188 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "AddIdentity", "RequestURI", requestUri));
189 if (!replies.get("Message").equals("IdentityAdded")) {
190 throw new FcpException("WebOfTrust Plugin did not reply with “IdentityAdded” message!");
192 String identifier = replies.get("ID");
193 String nickname = replies.get("Nickname");
194 return new Identity(identifier, nickname, requestUri);
198 * Returns identities by the given score.
203 * The context to get the identities for
205 * {@code null} to return neutrally trusted identities, {@code
206 * true} to return positively trusted identities, {@code false}
207 * for negatively trusted identities
208 * @return The trusted identites
209 * @throws IOException
210 * if an I/O error occurs
211 * @throws FcpException
212 * if an FCP error occurs
214 public Set<Identity> getIdentitesByScore(OwnIdentity ownIdentity, String context, Boolean positive) throws IOException, FcpException {
215 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentitesByScore", "TreeOwner", ownIdentity.getIdentifier(), "Context", context, "Selection", ((positive == null) ? "0" : (positive ? "+" : "-"))));
216 if (!replies.get("Message").equals("Identities")) {
217 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
219 Set<Identity> identities = new HashSet<Identity>();
220 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
221 String identifier = replies.get("Identity" + identityIndex);
222 String nickname = replies.get("Nickname" + identityIndex);
223 String requestUri = replies.get("RequestURI" + identityIndex);
224 identities.add(new Identity(identifier, nickname, requestUri));
230 * Returns the identities that trust the given identity.
233 * The identity to get the trusters for
235 * The context to get the trusters for
236 * @return The identities and their trust values
237 * @throws IOException
238 * if an I/O error occurs
239 * @throws FcpException
240 * if an FCP error occurs
242 public Map<Identity, IdentityTrust> getTrusters(Identity identity, String context) throws IOException, FcpException {
243 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrusters", "Identity", identity.getIdentifier(), "Context", context));
244 if (!replies.get("Message").equals("Identities")) {
245 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
247 Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
248 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
249 String identifier = replies.get("Identity" + identityIndex);
250 String nickname = replies.get("Nickname" + identityIndex);
251 String requestUri = replies.get("RequestURI" + identityIndex);
252 byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
253 String comment = replies.get("Comment" + identityIndex);
254 identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
256 return identityTrusts;
260 * Returns the identities that given identity trusts.
263 * The identity to get the trustees for
265 * The context to get the trustees for
266 * @return The identities and their trust values
267 * @throws IOException
268 * if an I/O error occurs
269 * @throws FcpException
270 * if an FCP error occurs
272 public Map<Identity, IdentityTrust> getTrustees(Identity identity, String context) throws IOException, FcpException {
273 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrustees", "Identity", identity.getIdentifier(), "Context", context));
274 if (!replies.get("Message").equals("Identities")) {
275 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
277 Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
278 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
279 String identifier = replies.get("Identity" + identityIndex);
280 String nickname = replies.get("Nickname" + identityIndex);
281 String requestUri = replies.get("RequestURI" + identityIndex);
282 byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
283 String comment = replies.get("Comment" + identityIndex);
284 identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
286 return identityTrusts;
290 * Sets the trust given to the given identify by the given own identity.
293 * The identity that gives the trust
295 * The identity that receives the trust
297 * The trust value (ranging from {@code -100} to {@code 100}
299 * The comment for setting the trust
300 * @throws IOException
301 * if an I/O error occurs
302 * @throws FcpException
303 * if an FCP error occurs
305 public void setTrust(OwnIdentity ownIdentity, Identity identity, byte trust, String comment) throws IOException, FcpException {
306 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "SetTrust", "Truster", ownIdentity.getIdentifier(), "Trustee", identity.getIdentifier(), "Value", String.valueOf(trust), "Comment", comment));
307 if (!replies.get("Message").equals("TrustSet")) {
308 throw new FcpException("WebOfTrust Plugin did not reply with “TrustSet” message!");
317 * Creates a map from each pair of parameters in the given array.
320 * The array of parameters
321 * @return The map created from the array
322 * @throws ArrayIndexOutOfBoundsException
323 * if the given parameter array does not contains an even number
326 private Map<String, String> createParameters(String... parameters) throws ArrayIndexOutOfBoundsException {
327 Map<String, String> parameterMap = new HashMap<String, String>();
328 for (int index = 0; index < parameters.length; index += 2) {
329 parameterMap.put(parameters[index], parameters[index + 1]);
335 * Wrapper around a web-of-trust identity.
337 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
339 public static class Identity {
341 /** The identity’s identifier. */
342 private final String identifier;
344 /** The identity’s nickname. */
345 private final String nickname;
347 /** The identity’s request URI. */
348 private final String requestUri;
351 * Creates a new identity.
354 * The identifies of the identity
356 * The nickname of the identity
358 * The request URI of the identity
360 public Identity(String identifier, String nickname, String requestUri) {
361 this.identifier = identifier;
362 this.nickname = nickname;
363 this.requestUri = requestUri;
367 * Returns the identifier of this identity.
369 * @return This identity’s identifier
371 public String getIdentifier() {
376 * Returns the nickname of this identity.
378 * @return This identity’s nickname
380 public String getNickname() {
385 * Returns the request URI of this identity.
387 * @return This identity’s request URI
389 public String getRequestUri() {
396 * Container for the trust given from one identity to another.
398 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
400 public static class IdentityTrust {
402 /** The trust given to the identity. */
403 private final byte trust;
405 /** The command for the trust value. */
406 private final String comment;
409 * Creates a new identity trust container.
412 * The trust given to the identity
414 * The comment for the trust value
416 public IdentityTrust(byte trust, String comment) {
418 this.comment = comment;
422 * Returns the trust value given to the identity.
424 * @return The trust value
426 public byte getTrust() {
431 * Returns the comment for the trust value.
433 * @return The comment for the trust value
435 public String getComment() {
442 * Container that stores the trust that is calculated by taking all trustees
443 * and their trust lists into account.
445 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
447 public static class CalculatedTrust {
449 /** The calculated trust value. */
450 private final Byte trust;
452 /** The calculated score value. */
453 private final Integer score;
455 /** The calculated rank. */
456 private final Integer rank;
459 * Creates a new calculated trust container.
462 * The calculated trust value
464 * The calculated score value
466 * The calculated rank of the
468 public CalculatedTrust(Byte trust, Integer score, Integer rank) {
475 * Returns the calculated trust value.
477 * @return The calculated trust value, or {@code null} if the trust
480 public Byte getTrust() {
485 * Returns the calculated score value.
487 * @return The calculated score value, or {@code null} if the score
490 public Integer getScore() {
495 * Returns the calculated rank.
497 * @return The calculated rank, or {@code null} if the rank is not known
499 public Integer getRank() {
506 * Wrapper around a web-of-trust own identity.
508 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
510 public static class OwnIdentity extends Identity {
512 /** The identity’s insert URI. */
513 private final String insertUri;
516 * Creates a new web-of-trust own identity.
519 * The identifier of the identity
521 * The nickname of the identity
523 * The request URI of the identity
525 * The insert URI of the identity
527 public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri) {
528 super(identifier, nickname, requestUri);
529 this.insertUri = insertUri;
533 * Returns the insert URI of this identity.
535 * @return This identity’s insert URI
537 public String getInsertUri() {