2 * jFCPlib - WebOfTrustPlugin.java - Copyright © 2009–2016 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 3 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, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.fcp.plugin;
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.HashSet;
26 import net.pterodactylus.fcp.highlevel.FcpClient;
27 import net.pterodactylus.fcp.highlevel.FcpException;
30 * Simplifies handling of the web-of-trust plugin.
32 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34 public class WebOfTrustPlugin {
36 /** The FCP client to use. */
37 private final FcpClient fcpClient;
40 * Creates a new web-of-trust plugin wrapper around the given FCP client.
43 * The FCP client to use for communication with the web-of-trust
46 public WebOfTrustPlugin(FcpClient fcpClient) {
47 this.fcpClient = fcpClient;
51 * Creates a new identity.
54 * The nickname of the new identity
56 * The context for the new identity
57 * @param publishTrustList
58 * {@code true} if the new identity should publish its trust list
59 * @return The new identity
61 * if an I/O error occurs
62 * @throws FcpException
63 * if an FCP error occurs
65 public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList) throws IOException, FcpException {
66 return createIdentity(nickname, context, publishTrustList, null, null);
70 * Creates a new identity from the given request and insert URI.
73 * The nickname of the new identity
75 * The context for the new identity
76 * @param publishTrustList
77 * {@code true} if the new identity should publish its trust list
79 * The request URI of the identity
81 * The insert URI of the identity
82 * @return The new identity
84 * if an I/O error occurs
85 * @throws FcpException
86 * if an FCP error occurs
88 public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList, String requestUri, String insertUri) throws IOException, FcpException {
89 Map<String, String> parameters = new HashMap<String, String>();
90 parameters.put("Message", "CreateIdentity");
91 parameters.put("Nickname", nickname);
92 parameters.put("Context", context);
93 parameters.put("PublishTrustList", String.valueOf(publishTrustList));
94 if ((requestUri != null) && (insertUri != null)) {
95 parameters.put("RequestURI", requestUri);
96 parameters.put("InsertURI", insertUri);
98 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", parameters);
99 if (!replies.get("Message").equals("IdentityCreated")) {
100 throw new FcpException("WebOfTrust Plugin did not reply with “IdentityCreated” message!");
102 String identifier = replies.get("ID");
103 String newRequestUri = replies.get("RequestURI");
104 String newInsertUri = replies.get("InsertURI");
105 return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri);
109 * Returns all own identities of the web-of-trust plugins. Almost all other
110 * commands require an {@link OwnIdentity} to return meaningful values.
112 * @return All own identities of the web-of-trust plugin
113 * @throws IOException
114 * if an I/O error occurs
115 * @throws FcpException
116 * if an FCP error occurs
118 public Set<OwnIdentity> getOwnIdentites() throws IOException, FcpException {
119 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetOwnIdentities"));
120 if (!replies.get("Message").equals("OwnIdentities")) {
121 throw new FcpException("WebOfTrust Plugin did not reply with “OwnIdentities” message!");
123 Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
124 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
125 String identity = replies.get("Identity" + identityIndex);
126 String nickname = replies.get("Nickname" + identityIndex);
127 String requestUri = replies.get("RequestURI" + identityIndex);
128 String insertUri = replies.get("InsertURI" + identityIndex);
129 ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri));
131 return ownIdentities;
135 * Returns the trust given to the identity with the given identifier by the
136 * given own identity.
139 * The own identity that is used to calculate trust values
141 * The identifier of the identity whose trust to get
142 * @return The request identity trust
143 * @throws IOException
144 * if an I/O error occurs
145 * @throws FcpException
146 * if an FCP error occurs
148 public CalculatedTrust getIdentityTrust(OwnIdentity ownIdentity, String identifier) throws IOException, FcpException {
149 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentity", "TreeOwner", ownIdentity.getIdentifier(), "Identity", identifier));
150 if (!replies.get("Message").equals("Identity")) {
151 throw new FcpException("WebOfTrust Plugin did not reply with “Identity” message!");
155 trust = Byte.valueOf(replies.get("Trust"));
156 } catch (NumberFormatException nfe1) {
159 Integer score = null;
161 score = Integer.valueOf(replies.get("Score"));
162 } catch (NumberFormatException nfe1) {
167 rank = Integer.valueOf(replies.get("Rank"));
168 } catch (NumberFormatException nfe1) {
171 return new CalculatedTrust(trust, score, rank);
175 * Adds a new identity by its request URI.
178 * The request URI of the identity to add
179 * @return The added identity
180 * @throws IOException
181 * if an I/O error occurs
182 * @throws FcpException
183 * if an FCP error occurs
185 public Identity addIdentity(String requestUri) throws IOException, FcpException {
186 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "AddIdentity", "RequestURI", requestUri));
187 if (!replies.get("Message").equals("IdentityAdded")) {
188 throw new FcpException("WebOfTrust Plugin did not reply with “IdentityAdded” message!");
190 String identifier = replies.get("ID");
191 String nickname = replies.get("Nickname");
192 return new Identity(identifier, nickname, requestUri);
196 * Returns identities by the given score.
201 * The context to get the identities for
203 * {@code null} to return neutrally trusted identities, {@code
204 * true} to return positively trusted identities, {@code false}
205 * for negatively trusted identities
206 * @return The trusted identites
207 * @throws IOException
208 * if an I/O error occurs
209 * @throws FcpException
210 * if an FCP error occurs
212 public Set<Identity> getIdentitesByScore(OwnIdentity ownIdentity, String context, Boolean positive) throws IOException, FcpException {
213 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetIdentitiesByScore", "TreeOwner", ownIdentity.getIdentifier(), "Context", context, "Selection", ((positive == null) ? "0" : (positive ? "+" : "-"))));
214 if (!replies.get("Message").equals("Identities")) {
215 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
217 Set<Identity> identities = new HashSet<Identity>();
218 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
219 String identifier = replies.get("Identity" + identityIndex);
220 String nickname = replies.get("Nickname" + identityIndex);
221 String requestUri = replies.get("RequestURI" + identityIndex);
222 identities.add(new Identity(identifier, nickname, requestUri));
228 * Returns the identities that trust the given identity.
231 * The identity to get the trusters for
233 * The context to get the trusters for
234 * @return The identities and their trust values
235 * @throws IOException
236 * if an I/O error occurs
237 * @throws FcpException
238 * if an FCP error occurs
240 public Map<Identity, IdentityTrust> getTrusters(Identity identity, String context) throws IOException, FcpException {
241 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrusters", "Identity", identity.getIdentifier(), "Context", context));
242 if (!replies.get("Message").equals("Identities")) {
243 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
245 Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
246 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
247 String identifier = replies.get("Identity" + identityIndex);
248 String nickname = replies.get("Nickname" + identityIndex);
249 String requestUri = replies.get("RequestURI" + identityIndex);
250 byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
251 String comment = replies.get("Comment" + identityIndex);
252 identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
254 return identityTrusts;
258 * Returns the identities that given identity trusts.
261 * The identity to get the trustees for
263 * The context to get the trustees for
264 * @return The identities and their trust values
265 * @throws IOException
266 * if an I/O error occurs
267 * @throws FcpException
268 * if an FCP error occurs
270 public Map<Identity, IdentityTrust> getTrustees(Identity identity, String context) throws IOException, FcpException {
271 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetTrustees", "Identity", identity.getIdentifier(), "Context", context));
272 if (!replies.get("Message").equals("Identities")) {
273 throw new FcpException("WebOfTrust Plugin did not reply with “Identities” message!");
275 Map<Identity, IdentityTrust> identityTrusts = new HashMap<Identity, IdentityTrust>();
276 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
277 String identifier = replies.get("Identity" + identityIndex);
278 String nickname = replies.get("Nickname" + identityIndex);
279 String requestUri = replies.get("RequestURI" + identityIndex);
280 byte trust = Byte.parseByte(replies.get("Value" + identityIndex));
281 String comment = replies.get("Comment" + identityIndex);
282 identityTrusts.put(new Identity(identifier, nickname, requestUri), new IdentityTrust(trust, comment));
284 return identityTrusts;
288 * Sets the trust given to the given identify by the given own identity.
291 * The identity that gives the trust
293 * The identity that receives the trust
295 * The trust value (ranging from {@code -100} to {@code 100}
297 * The comment for setting the trust
298 * @throws IOException
299 * if an I/O error occurs
300 * @throws FcpException
301 * if an FCP error occurs
303 public void setTrust(OwnIdentity ownIdentity, Identity identity, byte trust, String comment) throws IOException, FcpException {
304 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "SetTrust", "Truster", ownIdentity.getIdentifier(), "Trustee", identity.getIdentifier(), "Value", String.valueOf(trust), "Comment", comment));
305 if (!replies.get("Message").equals("TrustSet")) {
306 throw new FcpException("WebOfTrust Plugin did not reply with “TrustSet” message!");
311 * Adds the given context to the given identity.
314 * The identity to add the context to
317 * @throws IOException
318 * if an I/O error occurs
319 * @throws FcpException
320 * if an FCP error occurs
322 public void addContext(OwnIdentity ownIdentity, String context) throws IOException, FcpException {
323 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "AddContext", "Identity", ownIdentity.getIdentifier(), "Context", context));
324 if (!replies.get("Message").equals("ContextAdded")) {
325 throw new FcpException("WebOfTrust Plugin did not reply with “ContextAdded” message!");
330 * Removes the given context from the given identity.
333 * The identity to remove the context from
335 * The context to remove
336 * @throws IOException
337 * if an I/O error occurs
338 * @throws FcpException
339 * if an FCP error occurs
341 public void removeContext(OwnIdentity ownIdentity, String context) throws IOException, FcpException {
342 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "RemoveContext", "Identity", ownIdentity.getIdentifier(), "Context", context));
343 if (!replies.get("Message").equals("ContextRemoved")) {
344 throw new FcpException("WebOfTrust Plugin did not reply with “ContextRemoved” message!");
349 * Sets the given property for the given identity.
352 * The identity to set a property for
354 * The name of the property to set
356 * The value of the property to set
357 * @throws IOException
358 * if an I/O error occurs
359 * @throws FcpException
360 * if an FCP error occurs
362 public void setProperty(OwnIdentity ownIdentity, String property, String value) throws IOException, FcpException {
363 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "SetProperty", "Identity", ownIdentity.getIdentifier(), "Property", property, "Value", value));
364 if (!replies.get("Message").equals("PropertyAdded")) {
365 throw new FcpException("WebOfTrust Plugin did not reply with “PropertyAdded” message!");
370 * Returns the value of the given property for the given identity.
373 * The identity to get a property for
375 * The name of the property to get
376 * @return The value of the property
377 * @throws IOException
378 * if an I/O error occurs
379 * @throws FcpException
380 * if an FCP error occurs
382 public String getProperty(OwnIdentity ownIdentity, String property) throws IOException, FcpException {
383 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetProperty", "Identity", ownIdentity.getIdentifier(), "Property", property));
384 if (!replies.get("Message").equals("PropertyValue")) {
385 throw new FcpException("WebOfTrust Plugin did not reply with “PropertyValue” message!");
387 return replies.get("Property");
391 * Removes the given property from the given identity.
394 * The identity to remove a property from
396 * The name of the property to remove
397 * @throws IOException
398 * if an I/O error occurs
399 * @throws FcpException
400 * if an FCP error occurs
402 public void removeProperty(OwnIdentity ownIdentity, String property) throws IOException, FcpException {
403 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "RemoveProperty", "Identity", ownIdentity.getIdentifier(), "Property", property));
404 if (!replies.get("Message").equals("PropertyRemoved")) {
405 throw new FcpException("WebOfTrust Plugin did not reply with “PropertyRemoved” message!");
414 * Creates a map from each pair of parameters in the given array.
417 * The array of parameters
418 * @return The map created from the array
419 * @throws ArrayIndexOutOfBoundsException
420 * if the given parameter array does not contains an even number
423 private Map<String, String> createParameters(String... parameters) throws ArrayIndexOutOfBoundsException {
424 Map<String, String> parameterMap = new HashMap<String, String>();
425 for (int index = 0; index < parameters.length; index += 2) {
426 parameterMap.put(parameters[index], parameters[index + 1]);