2 * Sone - WebOfTrustConnector.java - Copyright © 2010–2013 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.sone.freenet.wot;
20 import java.util.HashMap;
21 import java.util.HashSet;
24 import java.util.concurrent.atomic.AtomicLong;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
28 import net.pterodactylus.sone.freenet.plugin.PluginConnector;
29 import net.pterodactylus.sone.freenet.plugin.PluginException;
30 import net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent;
31 import net.pterodactylus.util.logging.Logging;
32 import net.pterodactylus.util.number.Numbers;
34 import com.google.common.collect.MapMaker;
35 import com.google.common.eventbus.Subscribe;
36 import com.google.inject.Inject;
38 import freenet.support.SimpleFieldSet;
39 import freenet.support.api.Bucket;
42 * Connector for the Web of Trust plugin.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class WebOfTrustConnector {
49 private static final Logger logger = Logging.getLogger(WebOfTrustConnector.class);
51 /** The name of the WoT plugin. */
52 private static final String WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust";
54 /** Counter for connection identifiers. */
55 private final AtomicLong counter = new AtomicLong();
57 /** The plugin connector. */
58 private final PluginConnector pluginConnector;
60 /** Map for replies. */
61 private final Map<PluginIdentifier, Reply> replies = new MapMaker().makeMap();
64 * Creates a new Web of Trust connector that uses the given plugin
67 * @param pluginConnector
68 * The plugin connector
71 public WebOfTrustConnector(PluginConnector pluginConnector) {
72 this.pluginConnector = pluginConnector;
80 * Loads all own identities from the Web of Trust plugin.
82 * @return All own identity
83 * @throws WebOfTrustException
84 * if the own identities can not be loaded
86 public Set<OwnIdentity> loadAllOwnIdentities() throws WebOfTrustException {
87 Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetOwnIdentities").get());
88 SimpleFieldSet fields = reply.getFields();
89 int ownIdentityCounter = -1;
90 Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
92 String id = fields.get("Identity" + ++ownIdentityCounter);
96 String requestUri = fields.get("RequestURI" + ownIdentityCounter);
97 String insertUri = fields.get("InsertURI" + ownIdentityCounter);
98 String nickname = fields.get("Nickname" + ownIdentityCounter);
99 DefaultOwnIdentity ownIdentity = new DefaultOwnIdentity(id, nickname, requestUri, insertUri);
100 ownIdentity.setContexts(parseContexts("Contexts" + ownIdentityCounter + ".", fields));
101 ownIdentity.setProperties(parseProperties("Properties" + ownIdentityCounter + ".", fields));
102 ownIdentities.add(ownIdentity);
104 return ownIdentities;
108 * Loads all identities that the given identities trusts with a score of
109 * more than 0 and the (optional) given context.
114 * The context to filter, or {@code null}
115 * @return All trusted identities
116 * @throws PluginException
117 * if an error occured talking to the Web of Trust plugin
119 public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, String context) throws PluginException {
120 Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).put("WantTrustValues", "true").get());
121 SimpleFieldSet fields = reply.getFields();
122 Set<Identity> identities = new HashSet<Identity>();
123 int identityCounter = -1;
125 String id = fields.get("Identity" + ++identityCounter);
129 String nickname = fields.get("Nickname" + identityCounter);
130 String requestUri = fields.get("RequestURI" + identityCounter);
131 DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri);
132 identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields));
133 identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields));
134 Integer trust = Numbers.safeParseInteger(fields.get("Trust" + identityCounter), null);
135 int score = Numbers.safeParseInteger(fields.get("Score" + identityCounter), 0);
136 int rank = Numbers.safeParseInteger(fields.get("Rank" + identityCounter), 0);
137 identity.setTrust(ownIdentity, new Trust(trust, score, rank));
138 identities.add(identity);
144 * Adds the given context to the given identity.
147 * The identity to add the context to
150 * @throws PluginException
151 * if an error occured talking to the Web of Trust plugin
153 public void addContext(OwnIdentity ownIdentity, String context) throws PluginException {
154 performRequest(SimpleFieldSetConstructor.create().put("Message", "AddContext").put("Identity", ownIdentity.getId()).put("Context", context).get());
158 * Removes the given context from the given identity.
161 * The identity to remove the context from
163 * The context to remove
164 * @throws PluginException
165 * if an error occured talking to the Web of Trust plugin
167 public void removeContext(OwnIdentity ownIdentity, String context) throws PluginException {
168 performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveContext").put("Identity", ownIdentity.getId()).put("Context", context).get());
172 * Returns the value of the property with the given name.
175 * The identity whose properties to check
177 * The name of the property to return
178 * @return The value of the property, or {@code null} if there is no value
179 * @throws PluginException
180 * if an error occured talking to the Web of Trust plugin
182 public String getProperty(Identity identity, String name) throws PluginException {
183 Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetProperty").put("Identity", identity.getId()).put("Property", name).get());
184 return reply.getFields().get("Property");
188 * Sets the property with the given name to the given value.
191 * The identity to set the property on
193 * The name of the property to set
196 * @throws PluginException
197 * if an error occured talking to the Web of Trust plugin
199 public void setProperty(OwnIdentity ownIdentity, String name, String value) throws PluginException {
200 performRequest(SimpleFieldSetConstructor.create().put("Message", "SetProperty").put("Identity", ownIdentity.getId()).put("Property", name).put("Value", value).get());
204 * Removes the property with the given name.
207 * The identity to remove the property from
209 * The name of the property to remove
210 * @throws PluginException
211 * if an error occured talking to the Web of Trust plugin
213 public void removeProperty(OwnIdentity ownIdentity, String name) throws PluginException {
214 performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveProperty").put("Identity", ownIdentity.getId()).put("Property", name).get());
218 * Returns the trust for the given identity assigned to it by the given own
224 * The identity to get the trust for
225 * @return The trust for the given identity
226 * @throws PluginException
227 * if an error occured talking to the Web of Trust plugin
229 public Trust getTrust(OwnIdentity ownIdentity, Identity identity) throws PluginException {
230 Reply getTrustReply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentity").put("Truster", ownIdentity.getId()).put("Identity", identity.getId()).get());
231 String trust = getTrustReply.getFields().get("Trust");
232 String score = getTrustReply.getFields().get("Score");
233 String rank = getTrustReply.getFields().get("Rank");
234 Integer explicit = null;
235 Integer implicit = null;
236 Integer distance = null;
238 explicit = Integer.valueOf(trust);
239 } catch (NumberFormatException nfe1) {
243 implicit = Integer.valueOf(score);
244 distance = Integer.valueOf(rank);
245 } catch (NumberFormatException nfe1) {
248 return new Trust(explicit, implicit, distance);
252 * Sets the trust for the given identity.
255 * The trusting identity
257 * The trusted identity
259 * The amount of trust (-100 thru 100)
261 * The comment or explanation of the trust value
262 * @throws PluginException
263 * if an error occured talking to the Web of Trust plugin
265 public void setTrust(OwnIdentity ownIdentity, Identity identity, int trust, String comment) throws PluginException {
266 performRequest(SimpleFieldSetConstructor.create().put("Message", "SetTrust").put("Truster", ownIdentity.getId()).put("Trustee", identity.getId()).put("Value", String.valueOf(trust)).put("Comment", comment).get());
270 * Removes any trust assignment of the given own identity for the given
276 * The identity to remove all trust for
277 * @throws WebOfTrustException
280 public void removeTrust(OwnIdentity ownIdentity, Identity identity) throws WebOfTrustException {
281 performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveTrust").put("Truster", ownIdentity.getId()).put("Trustee", identity.getId()).get());
285 * Pings the Web of Trust plugin. If the plugin can not be reached, a
286 * {@link PluginException} is thrown.
288 * @throws PluginException
289 * if the plugin is not loaded
291 public void ping() throws PluginException {
292 performRequest(SimpleFieldSetConstructor.create().put("Message", "Ping").get());
300 * Parses the contexts from the given fields.
303 * The prefix to use to access the contexts
305 * The fields to parse the contexts from
306 * @return The parsed contexts
308 private static Set<String> parseContexts(String prefix, SimpleFieldSet fields) {
309 Set<String> contexts = new HashSet<String>();
310 int contextCounter = -1;
312 String context = fields.get(prefix + "Context" + ++contextCounter);
313 if (context == null) {
316 contexts.add(context);
322 * Parses the properties from the given fields.
325 * The prefix to use to access the properties
327 * The fields to parse the properties from
328 * @return The parsed properties
330 private static Map<String, String> parseProperties(String prefix, SimpleFieldSet fields) {
331 Map<String, String> properties = new HashMap<String, String>();
332 int propertiesCounter = -1;
334 String propertyName = fields.get(prefix + "Property" + ++propertiesCounter + ".Name");
335 if (propertyName == null) {
338 String propertyValue = fields.get(prefix + "Property" + propertiesCounter + ".Value");
339 properties.put(propertyName, propertyValue);
345 * Sends a request containing the given fields and waits for the target
349 * The fields of the message
350 * @return The reply message
351 * @throws PluginException
352 * if the request could not be sent
354 private Reply performRequest(SimpleFieldSet fields) throws PluginException {
355 return performRequest(fields, null);
359 * Sends a request containing the given fields and waits for the target
363 * The fields of the message
365 * The payload of the message
366 * @return The reply message
367 * @throws PluginException
368 * if the request could not be sent
370 private Reply performRequest(SimpleFieldSet fields, Bucket data) throws PluginException {
371 String identifier = "FCP-Command-" + System.currentTimeMillis() + "-" + counter.getAndIncrement();
372 Reply reply = new Reply();
373 PluginIdentifier pluginIdentifier = new PluginIdentifier(WOT_PLUGIN_NAME, identifier);
374 replies.put(pluginIdentifier, reply);
376 logger.log(Level.FINE, String.format("Sending FCP Request: %s", fields.get("Message")));
377 synchronized (reply) {
379 pluginConnector.sendRequest(WOT_PLUGIN_NAME, identifier, fields, data);
380 while (reply.getFields() == null) {
383 } catch (InterruptedException ie1) {
384 logger.log(Level.WARNING, String.format("Got interrupted while waiting for reply on %s.", fields.get("Message")), ie1);
388 replies.remove(pluginIdentifier);
391 logger.log(Level.FINEST, String.format("Received FCP Response for %s: %s", fields.get("Message"), (reply.getFields() != null) ? reply.getFields().get("Message") : null));
392 if ((reply.getFields() == null) || "Error".equals(reply.getFields().get("Message"))) {
393 throw new PluginException("Could not perform request for " + fields.get("Message"));
399 * Notifies the connector that a plugin reply was received.
401 * @param receivedReplyEvent
405 public void receivedReply(ReceivedReplyEvent receivedReplyEvent) {
406 PluginIdentifier pluginIdentifier = new PluginIdentifier(receivedReplyEvent.pluginName(), receivedReplyEvent.identifier());
407 Reply reply = replies.remove(pluginIdentifier);
411 logger.log(Level.FINEST, String.format("Received Reply from Plugin: %s", receivedReplyEvent.fieldSet().get("Message")));
412 synchronized (reply) {
413 reply.setFields(receivedReplyEvent.fieldSet());
419 * Container for the data of the reply from a plugin.
421 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
423 private static class Reply {
425 /** The fields of the reply. */
426 private SimpleFieldSet fields;
428 /** Empty constructor. */
434 * Returns the fields of the reply.
436 * @return The fields of the reply
438 public SimpleFieldSet getFields() {
443 * Sets the fields of the reply.
446 * The fields of the reply
448 public void setFields(SimpleFieldSet fields) {
449 this.fields = fields;
455 * Helper method to create {@link SimpleFieldSet}s with terser code.
457 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
459 private static class SimpleFieldSetConstructor {
461 /** The field set being created. */
462 private SimpleFieldSet simpleFieldSet;
465 * Creates a new simple field set constructor.
468 * {@code true} if the resulting simple field set should be
469 * short-lived, {@code false} otherwise
471 private SimpleFieldSetConstructor(boolean shortLived) {
472 simpleFieldSet = new SimpleFieldSet(shortLived);
480 * Returns the created simple field set.
482 * @return The created simple field set
484 public SimpleFieldSet get() {
485 return simpleFieldSet;
489 * Sets the field with the given name to the given value.
492 * The name of the fleld
494 * The value of the field
495 * @return This constructor (for method chaining)
497 public SimpleFieldSetConstructor put(String name, String value) {
498 simpleFieldSet.putOverwrite(name, value);
507 * Creates a new simple field set constructor.
509 * @return The created simple field set constructor
511 public static SimpleFieldSetConstructor create() {
516 * Creates a new simple field set constructor.
519 * {@code true} if the resulting simple field set should be
520 * short-lived, {@code false} otherwise
521 * @return The created simple field set constructor
523 public static SimpleFieldSetConstructor create(boolean shortLived) {
524 SimpleFieldSetConstructor simpleFieldSetConstructor = new SimpleFieldSetConstructor(shortLived);
525 return simpleFieldSetConstructor;
531 * Container for identifying plugins. Plugins are identified by their plugin
532 * name and their unique identifier.
534 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
536 private static class PluginIdentifier {
538 /** The plugin name. */
539 private final String pluginName;
541 /** The plugin identifier. */
542 private final String identifier;
545 * Creates a new plugin identifier.
548 * The name of the plugin
550 * The identifier of the plugin
552 public PluginIdentifier(String pluginName, String identifier) {
553 this.pluginName = pluginName;
554 this.identifier = identifier;
565 public int hashCode() {
566 return pluginName.hashCode() ^ identifier.hashCode();
573 public boolean equals(Object object) {
574 if (!(object instanceof PluginIdentifier)) {
577 PluginIdentifier pluginIdentifier = (PluginIdentifier) object;
578 return pluginName.equals(pluginIdentifier.pluginName) && identifier.equals(pluginIdentifier.identifier);