2 * jFCPlib - WebOfTrustPlugin.java - Copyright © 2009 David Roden
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 2 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package net.pterodactylus.fcp.plugin;
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.HashSet;
27 import net.pterodactylus.fcp.highlevel.FcpClient;
28 import net.pterodactylus.fcp.highlevel.FcpException;
31 * Simplifies handling of the web-of-trust plugin.
33 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35 public class WebOfTrustPlugin {
37 /** The FCP client to use. */
38 private final FcpClient fcpClient;
41 * Creates a new web-of-trust plugin wrapper around the given FCP client.
44 * The FCP client to use for communication with the web-of-trust
47 public WebOfTrustPlugin(FcpClient fcpClient) {
48 this.fcpClient = fcpClient;
52 * Creates a new identity.
55 * The nickname of the new identity
57 * The context for the new identity
58 * @param publishTrustList
59 * {@code true} if the new identity should publish its trust list
60 * @return The new identity
62 * if an I/O error occurs
63 * @throws FcpException
64 * if an FCP error occurs
66 public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList) throws IOException, FcpException {
67 return createIdentity(nickname, context, publishTrustList, null, null);
71 * Creates a new identity from the given request and insert URI.
74 * The nickname of the new identity
76 * The context for the new identity
77 * @param publishTrustList
78 * {@code true} if the new identity should publish its trust list
80 * The request URI of the identity
82 * The insert URI of the identity
83 * @return The new identity
85 * if an I/O error occurs
86 * @throws FcpException
87 * if an FCP error occurs
89 public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList, String requestUri, String insertUri) throws IOException, FcpException {
90 Map<String, String> parameters = new HashMap<String, String>();
91 parameters.put("Message", "CreateIdentity");
92 parameters.put("Nickname", nickname);
93 parameters.put("Context", context);
94 parameters.put("PublishTrustList", String.valueOf(publishTrustList));
95 if ((requestUri != null) && (insertUri != null)) {
96 parameters.put("RequestURI", requestUri);
97 parameters.put("InsertURI", insertUri);
99 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", parameters);
100 if (!replies.get("Message").equals("IdentityCreated")) {
101 throw new FcpException("WebOfTrust Plugin did not reply with “IdentityCreated” message!");
103 String identifier = replies.get("ID");
104 String newRequestUri = replies.get("RequestURI");
105 String newInsertUri = replies.get("InsertURI");
106 return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri);
110 * Returns all own identities of the web-of-trust plugins. Almost all other
111 * commands require an {@link OwnIdentity} to return meaningful values.
113 * @return All own identities of the web-of-trust plugin
114 * @throws IOException
115 * if an I/O error occurs
116 * @throws FcpException
117 * if an FCP error occurs
119 public Set<OwnIdentity> getOwnIdentites() throws IOException, FcpException {
120 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetOwnIdentities"));
121 if (!replies.get("Message").equals("OwnIdentities")) {
122 throw new FcpException("WebOfTrust Plugin did not reply with “OwnIdentities” message!");
124 Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
125 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
126 String identity = replies.get("Identity" + identityIndex);
127 String nickname = replies.get("Nickname" + identityIndex);
128 String requestUri = replies.get("RequestURI" + identityIndex);
129 String insertUri = replies.get("InsertURI" + identityIndex);
130 ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri));
132 return ownIdentities;
136 * Returns the trust given to the identity with the given identifier by the
137 * given own identity.
140 * The own identity that is used to calculate trust values
142 * The identifier of the identity whose trust to get
143 * @return The request identity trust
144 * @throws IOException
145 * if an I/O error occurs
146 * @throws FcpException
147 * if an FCP error occurs
149 public CalculatedTrust getIdentityTrust(OwnIdentity ownIdentity, String identifier) throws IOException, FcpException {
150 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentity", "TreeOwner", ownIdentity.getIdentifier(), "Identity", identifier));
151 if (!replies.get("Message").equals("Identity")) {
152 throw new FcpException("WebOfTrust Plugin did not reply with “Identity” message!");
156 trust = Byte.valueOf(replies.get("Trust"));
157 } catch (NumberFormatException nfe1) {
160 Integer score = null;
162 score = Integer.valueOf(replies.get("Score"));
163 } catch (NumberFormatException nfe1) {
168 rank = Integer.valueOf(replies.get("Rank"));
169 } catch (NumberFormatException nfe1) {
172 return new CalculatedTrust(trust, score, rank);
176 * Adds a new identity by its request URI.
179 * The request URI of the identity to add
180 * @return The added identity
181 * @throws IOException
182 * if an I/O error occurs
183 * @throws FcpException
184 * if an FCP error occurs
186 public Identity addIdentity(String requestUri) throws IOException, FcpException {
187 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "AddIdentity", "RequestURI", requestUri));
188 if (!replies.get("Message").equals("IdentityAdded")) {
189 throw new FcpException("WebOfTrust Plugin did not reply with “IdentityAdded” message!");
191 String identifier = replies.get("ID");
192 String nickname = replies.get("Nickname");
193 return new Identity(identifier, nickname, requestUri);
197 * Returns identities by the given score.
202 * The context to get the identities for
204 * {@code null} to return neutrally trusted identities, {@code
205 * true} to return positively trusted identities, {@code false}
206 * for negatively trusted identities
207 * @return The trusted identites
208 * @throws IOException
209 * if an I/O error occurs
210 * @throws FcpException
211 * if an FCP error occurs
213 public Set<Identity> getIdentitesByScore(OwnIdentity ownIdentity, String context, Boolean positive) throws IOException, FcpException {
214 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentitiesByScore", "TreeOwner", ownIdentity.getIdentifier(), "Context", context, "Selection", ((positive == null) ? "0" : (positive ? "+" : "-"))));
215 if (!replies.get("Message").equals("Identities")) {
216 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
218 Set<Identity> identities = new HashSet<Identity>();
219 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
220 String identifier = replies.get("Identity" + identityIndex);
221 String nickname = replies.get("Nickname" + identityIndex);
222 String requestUri = replies.get("RequestURI" + identityIndex);
223 identities.add(new Identity(identifier, nickname, requestUri));
229 * Returns the identities that trust the given identity.
232 * The identity to get the trusters for
234 * The context to get the trusters for
235 * @return The identities and their trust values
236 * @throws IOException
237 * if an I/O error occurs
238 * @throws FcpException
239 * if an FCP error occurs
241 public Map<Identity, IdentityTrust> getTrusters(Identity identity, String context) throws IOException, FcpException {
242 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrusters", "Identity", identity.getIdentifier(), "Context", context));
243 if (!replies.get("Message").equals("Identities")) {
244 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
246 Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
247 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
248 String identifier = replies.get("Identity" + identityIndex);
249 String nickname = replies.get("Nickname" + identityIndex);
250 String requestUri = replies.get("RequestURI" + identityIndex);
251 byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
252 String comment = replies.get("Comment" + identityIndex);
253 identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
255 return identityTrusts;
259 * Returns the identities that given identity trusts.
262 * The identity to get the trustees for
264 * The context to get the trustees for
265 * @return The identities and their trust values
266 * @throws IOException
267 * if an I/O error occurs
268 * @throws FcpException
269 * if an FCP error occurs
271 public Map<Identity, IdentityTrust> getTrustees(Identity identity, String context) throws IOException, FcpException {
272 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrustees", "Identity", identity.getIdentifier(), "Context", context));
273 if (!replies.get("Message").equals("Identities")) {
274 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
276 Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
277 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
278 String identifier = replies.get("Identity" + identityIndex);
279 String nickname = replies.get("Nickname" + identityIndex);
280 String requestUri = replies.get("RequestURI" + identityIndex);
281 byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
282 String comment = replies.get("Comment" + identityIndex);
283 identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
285 return identityTrusts;
289 * Sets the trust given to the given identify by the given own identity.
292 * The identity that gives the trust
294 * The identity that receives the trust
296 * The trust value (ranging from {@code -100} to {@code 100}
298 * The comment for setting the trust
299 * @throws IOException
300 * if an I/O error occurs
301 * @throws FcpException
302 * if an FCP error occurs
304 public void setTrust(OwnIdentity ownIdentity, Identity identity, byte trust, String comment) throws IOException, FcpException {
305 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "SetTrust", "Truster", ownIdentity.getIdentifier(), "Trustee", identity.getIdentifier(), "Value", String.valueOf(trust), "Comment", comment));
306 if (!replies.get("Message").equals("TrustSet")) {
307 throw new FcpException("WebOfTrust Plugin did not reply with “TrustSet” message!");
312 * Adds the given context to the given identity.
315 * The identity to add the context to
318 * @throws IOException
319 * if an I/O error occurs
320 * @throws FcpException
321 * if an FCP error occurs
323 public void addContext(OwnIdentity ownIdentity, String context) throws IOException, FcpException {
324 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "AddContext", "Identity", ownIdentity.getIdentifier(), "Context", context));
325 if (!replies.get("Message").equals("ContextAdded")) {
326 throw new FcpException("WebOfTrust Plugin did not reply with “ContextAdded” message!");
331 * Removes the given context from the given identity.
334 * The identity to remove the context from
336 * The context to remove
337 * @throws IOException
338 * if an I/O error occurs
339 * @throws FcpException
340 * if an FCP error occurs
342 public void removeContext(OwnIdentity ownIdentity, String context) throws IOException, FcpException {
343 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "RemoveContext", "Identity", ownIdentity.getIdentifier(), "Context", context));
344 if (!replies.get("Message").equals("ContextRemoved")) {
345 throw new FcpException("WebOfTrust Plugin did not reply with “ContextRemoved” message!");
350 * Sets the given property for the given identity.
353 * The identity to set a property for
355 * The name of the property to set
357 * The value of the property to set
358 * @throws IOException
359 * if an I/O error occurs
360 * @throws FcpException
361 * if an FCP error occurs
363 public void setProperty(OwnIdentity ownIdentity, String property, String value) throws IOException, FcpException {
364 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "SetProperty", "Identity", ownIdentity.getIdentifier(), "Property", property, "Value", value));
365 if (!replies.get("Message").equals("PropertyAdded")) {
366 throw new FcpException("WebOfTrust Plugin did not reply with “PropertyAdded” message!");
371 * Returns the value of the given property for the given identity.
374 * The identity to get a property for
376 * The name of the property to get
377 * @return The value of the property
378 * @throws IOException
379 * if an I/O error occurs
380 * @throws FcpException
381 * if an FCP error occurs
383 public String getProperty(OwnIdentity ownIdentity, String property) throws IOException, FcpException {
384 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetProperty", "Identity", ownIdentity.getIdentifier(), "Property", property));
385 if (!replies.get("Message").equals("PropertyValue")) {
386 throw new FcpException("WebOfTrust Plugin did not reply with “PropertyValue” message!");
388 return replies.get("Property");
392 * Removes the given property from the given identity.
395 * The identity to remove a property from
397 * The name of the property to remove
398 * @throws IOException
399 * if an I/O error occurs
400 * @throws FcpException
401 * if an FCP error occurs
403 public void removeProperty(OwnIdentity ownIdentity, String property) throws IOException, FcpException {
404 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "RemoveProperty", "Identity", ownIdentity.getIdentifier(), "Property", property));
405 if (!replies.get("Message").equals("PropertyRemoved")) {
406 throw new FcpException("WebOfTrust Plugin did not reply with “PropertyRemoved” message!");
415 * Creates a map from each pair of parameters in the given array.
418 * The array of parameters
419 * @return The map created from the array
420 * @throws ArrayIndexOutOfBoundsException
421 * if the given parameter array does not contains an even number
424 private Map<String, String> createParameters(String... parameters) throws ArrayIndexOutOfBoundsException {
425 Map<String, String> parameterMap = new HashMap<String, String>();
426 for (int index = 0; index < parameters.length; index += 2) {
427 parameterMap.put(parameters[index], parameters[index + 1]);
433 * Wrapper around a web-of-trust identity.
435 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
437 public static class Identity {
439 /** The identity’s identifier. */
440 private final String identifier;
442 /** The identity’s nickname. */
443 private final String nickname;
445 /** The identity’s request URI. */
446 private final String requestUri;
449 * Creates a new identity.
452 * The identifies of the identity
454 * The nickname of the identity
456 * The request URI of the identity
458 public Identity(String identifier, String nickname, String requestUri) {
459 this.identifier = identifier;
460 this.nickname = nickname;
461 this.requestUri = requestUri;
465 * Returns the identifier of this identity.
467 * @return This identity’s identifier
469 public String getIdentifier() {
474 * Returns the nickname of this identity.
476 * @return This identity’s nickname
478 public String getNickname() {
483 * Returns the request URI of this identity.
485 * @return This identity’s request URI
487 public String getRequestUri() {
495 public boolean equals(Object obj) {
496 if ((obj == null) || (obj.getClass() != this.getClass())) {
499 Identity identity = (Identity) obj;
500 return identifier.equals(identity.identifier);
507 public int hashCode() {
508 return identifier.hashCode();
514 * Container for the trust given from one identity to another.
516 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
518 public static class IdentityTrust {
520 /** The trust given to the identity. */
521 private final byte trust;
523 /** The command for the trust value. */
524 private final String comment;
527 * Creates a new identity trust container.
530 * The trust given to the identity
532 * The comment for the trust value
534 public IdentityTrust(byte trust, String comment) {
536 this.comment = comment;
540 * Returns the trust value given to the identity.
542 * @return The trust value
544 public byte getTrust() {
549 * Returns the comment for the trust value.
551 * @return The comment for the trust value
553 public String getComment() {
560 * Container that stores the trust that is calculated by taking all trustees
561 * and their trust lists into account.
563 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
565 public static class CalculatedTrust {
567 /** The calculated trust value. */
568 private final Byte trust;
570 /** The calculated score value. */
571 private final Integer score;
573 /** The calculated rank. */
574 private final Integer rank;
577 * Creates a new calculated trust container.
580 * The calculated trust value
582 * The calculated score value
584 * The calculated rank of the
586 public CalculatedTrust(Byte trust, Integer score, Integer rank) {
593 * Returns the calculated trust value.
595 * @return The calculated trust value, or {@code null} if the trust
598 public Byte getTrust() {
603 * Returns the calculated score value.
605 * @return The calculated score value, or {@code null} if the score
608 public Integer getScore() {
613 * Returns the calculated rank.
615 * @return The calculated rank, or {@code null} if the rank is not known
617 public Integer getRank() {
624 * Wrapper around a web-of-trust own identity.
626 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
628 public static class OwnIdentity extends Identity {
630 /** The identity’s insert URI. */
631 private final String insertUri;
634 * Creates a new web-of-trust own identity.
637 * The identifier of the identity
639 * The nickname of the identity
641 * The request URI of the identity
643 * The insert URI of the identity
645 public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri) {
646 super(identifier, nickname, requestUri);
647 this.insertUri = insertUri;
651 * Returns the insert URI of this identity.
653 * @return This identity’s insert URI
655 public String getInsertUri() {