2 * Sone - WebOfTrustConnector.java - Copyright © 2010–2019 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;
21 import java.util.logging.Logger;
22 import java.util.logging.*;
23 import javax.annotation.*;
25 import net.pterodactylus.sone.freenet.plugin.*;
27 import com.google.inject.*;
28 import freenet.support.*;
29 import freenet.support.api.*;
31 import static java.lang.String.*;
32 import static java.util.logging.Logger.*;
33 import static net.pterodactylus.sone.utils.NumberParsers.*;
36 * Connector for the Web of Trust plugin.
39 public class WebOfTrustConnector {
42 private static final Logger logger = getLogger(WebOfTrustConnector.class.getName());
44 /** The name of the WoT plugin. */
45 private static final String WOT_PLUGIN_NAME = "plugins.WebOfTrust.WebOfTrust";
47 /** The plugin connector. */
48 private final PluginConnector pluginConnector;
51 * Creates a new Web of Trust connector that uses the given plugin
54 * @param pluginConnector
55 * The plugin connector
58 public WebOfTrustConnector(PluginConnector pluginConnector) {
59 this.pluginConnector = pluginConnector;
67 * Stops the web of trust connector.
74 * Loads all own identities from the Web of Trust plugin.
76 * @return All own identity
77 * @throws WebOfTrustException
78 * if the own identities can not be loaded
80 public Set<OwnIdentity> loadAllOwnIdentities() throws WebOfTrustException {
81 PluginReply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetOwnIdentities").get());
82 SimpleFieldSet fields = reply.getFields();
83 int ownIdentityCounter = -1;
84 Set<OwnIdentity> ownIdentities = new HashSet<>();
86 String id = fields.get("Identity" + ++ownIdentityCounter);
90 String requestUri = fields.get("RequestURI" + ownIdentityCounter);
91 String insertUri = fields.get("InsertURI" + ownIdentityCounter);
92 String nickname = fields.get("Nickname" + ownIdentityCounter);
93 DefaultOwnIdentity ownIdentity = new DefaultOwnIdentity(id, nickname, requestUri, insertUri);
94 ownIdentity.setContexts(parseContexts("Contexts" + ownIdentityCounter + ".", fields));
95 ownIdentity.setProperties(parseProperties("Properties" + ownIdentityCounter + ".", fields));
96 ownIdentities.add(ownIdentity);
102 * Loads all identities that the given identities trusts with a score of
107 * @return All trusted identities
108 * @throws PluginException
109 * if an error occured talking to the Web of Trust plugin
111 public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity) throws PluginException {
112 return loadTrustedIdentities(ownIdentity, null);
116 * Loads all identities that the given identities trusts with a score of
117 * more than 0 and the (optional) given context.
122 * The context to filter, or {@code null}
123 * @return All trusted identities
124 * @throws PluginException
125 * if an error occured talking to the Web of Trust plugin
127 public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, @Nullable String context) throws PluginException {
128 PluginReply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).put("WantTrustValues", "true").get());
129 SimpleFieldSet fields = reply.getFields();
130 Set<Identity> identities = new HashSet<>();
131 int identityCounter = -1;
133 String id = fields.get("Identity" + ++identityCounter);
137 String nickname = fields.get("Nickname" + identityCounter);
138 String requestUri = fields.get("RequestURI" + identityCounter);
139 DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri);
140 identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields));
141 identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields));
142 Integer trust = parseInt(fields.get("Trust" + identityCounter), null);
143 int score = parseInt(fields.get("Score" + identityCounter), 0);
144 int rank = parseInt(fields.get("Rank" + identityCounter), 0);
145 identity.setTrust(ownIdentity, new Trust(trust, score, rank));
146 identities.add(identity);
152 * Adds the given context to the given identity.
155 * The identity to add the context to
158 * @throws PluginException
159 * if an error occured talking to the Web of Trust plugin
161 public void addContext(OwnIdentity ownIdentity, String context) throws PluginException {
162 performRequest(SimpleFieldSetConstructor.create().put("Message", "AddContext").put("Identity", ownIdentity.getId()).put("Context", context).get());
166 * Removes the given context from the given identity.
169 * The identity to remove the context from
171 * The context to remove
172 * @throws PluginException
173 * if an error occured talking to the Web of Trust plugin
175 public void removeContext(OwnIdentity ownIdentity, String context) throws PluginException {
176 performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveContext").put("Identity", ownIdentity.getId()).put("Context", context).get());
180 * Sets the property with the given name to the given value.
183 * The identity to set the property on
185 * The name of the property to set
188 * @throws PluginException
189 * if an error occured talking to the Web of Trust plugin
191 public void setProperty(OwnIdentity ownIdentity, String name, String value) throws PluginException {
192 performRequest(SimpleFieldSetConstructor.create().put("Message", "SetProperty").put("Identity", ownIdentity.getId()).put("Property", name).put("Value", value).get());
196 * Removes the property with the given name.
199 * The identity to remove the property from
201 * The name of the property to remove
202 * @throws PluginException
203 * if an error occured talking to the Web of Trust plugin
205 public void removeProperty(OwnIdentity ownIdentity, String name) throws PluginException {
206 performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveProperty").put("Identity", ownIdentity.getId()).put("Property", name).get());
210 * Returns the trust for the given identity assigned to it by the given own
216 * The identity to get the trust for
217 * @return The trust for the given identity
218 * @throws PluginException
219 * if an error occured talking to the Web of Trust plugin
221 public Trust getTrust(OwnIdentity ownIdentity, Identity identity) throws PluginException {
222 PluginReply getTrustReply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentity").put("Truster", ownIdentity.getId()).put("Identity", identity.getId()).get());
223 String trust = getTrustReply.getFields().get("Trust");
224 String score = getTrustReply.getFields().get("Score");
225 String rank = getTrustReply.getFields().get("Rank");
226 Integer explicit = null;
227 Integer implicit = null;
228 Integer distance = null;
230 explicit = Integer.valueOf(trust);
231 } catch (NumberFormatException nfe1) {
235 implicit = Integer.valueOf(score);
236 } catch (NumberFormatException nfe1) {
240 distance = Integer.valueOf(rank);
241 } catch (NumberFormatException nfe1) {
244 return new Trust(explicit, implicit, distance);
248 * Sets the trust for the given identity.
251 * The trusting identity
253 * The trusted identity
255 * The amount of trust (-100 thru 100)
257 * The comment or explanation of the trust value
258 * @throws PluginException
259 * if an error occured talking to the Web of Trust plugin
261 public void setTrust(OwnIdentity ownIdentity, Identity identity, int trust, String comment) throws PluginException {
262 performRequest(SimpleFieldSetConstructor.create().put("Message", "SetTrust").put("Truster", ownIdentity.getId()).put("Trustee", identity.getId()).put("Value", String.valueOf(trust)).put("Comment", comment).get());
266 * Removes any trust assignment of the given own identity for the given
272 * The identity to remove all trust for
273 * @throws WebOfTrustException
276 public void removeTrust(OwnIdentity ownIdentity, Identity identity) throws WebOfTrustException {
277 performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveTrust").put("Truster", ownIdentity.getId()).put("Trustee", identity.getId()).get());
281 * Pings the Web of Trust plugin. If the plugin can not be reached, a
282 * {@link PluginException} is thrown.
284 * @throws PluginException
285 * if the plugin is not loaded
287 public void ping() throws PluginException {
288 performRequest(SimpleFieldSetConstructor.create().put("Message", "Ping").get());
296 * Parses the contexts from the given fields.
299 * The prefix to use to access the contexts
301 * The fields to parse the contexts from
302 * @return The parsed contexts
304 private static Set<String> parseContexts(String prefix, SimpleFieldSet fields) {
305 Set<String> contexts = new HashSet<>();
306 int contextCounter = -1;
308 String context = fields.get(prefix + "Context" + ++contextCounter);
309 if (context == null) {
312 contexts.add(context);
318 * Parses the properties from the given fields.
321 * The prefix to use to access the properties
323 * The fields to parse the properties from
324 * @return The parsed properties
326 private static Map<String, String> parseProperties(String prefix, SimpleFieldSet fields) {
327 Map<String, String> properties = new HashMap<>();
328 int propertiesCounter = -1;
330 String propertyName = fields.get(prefix + "Property" + ++propertiesCounter + ".Name");
331 if (propertyName == null) {
334 String propertyValue = fields.get(prefix + "Property" + propertiesCounter + ".Value");
335 properties.put(propertyName, propertyValue);
341 * Sends a request containing the given fields and waits for the target
345 * The fields of the message
346 * @return The reply message
347 * @throws PluginException
348 * if the request could not be sent
350 private PluginReply performRequest(SimpleFieldSet fields) throws PluginException {
351 return performRequest(fields, null);
355 * Sends a request containing the given fields and waits for the target
359 * The fields of the message
361 * The payload of the message
362 * @return The reply message
363 * @throws PluginException
364 * if the request could not be sent
366 private PluginReply performRequest(SimpleFieldSet fields, Bucket data) throws PluginException {
367 logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message")));
368 PluginReply pluginReply = pluginConnector.sendRequest(WOT_PLUGIN_NAME, "", fields, data);
369 logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), pluginReply.getFields().get("Message")));
370 if ("Error".equals(pluginReply.getFields().get("Message"))) {
371 throw new PluginException("Could not perform request for " + fields.get("Message"));
377 * Helper method to create {@link SimpleFieldSet}s with terser code.
379 private static class SimpleFieldSetConstructor {
381 /** The field set being created. */
382 private SimpleFieldSet simpleFieldSet;
385 * Creates a new simple field set constructor.
388 * {@code true} if the resulting simple field set should be
389 * short-lived, {@code false} otherwise
391 private SimpleFieldSetConstructor(boolean shortLived) {
392 simpleFieldSet = new SimpleFieldSet(shortLived);
400 * Returns the created simple field set.
402 * @return The created simple field set
404 public SimpleFieldSet get() {
405 return simpleFieldSet;
409 * Sets the field with the given name to the given value.
412 * The name of the fleld
414 * The value of the field
415 * @return This constructor (for method chaining)
417 public SimpleFieldSetConstructor put(String name, String value) {
418 simpleFieldSet.putOverwrite(name, value);
427 * Creates a new simple field set constructor.
429 * @return The created simple field set constructor
431 public static SimpleFieldSetConstructor create() {
436 * Creates a new simple field set constructor.
439 * {@code true} if the resulting simple field set should be
440 * short-lived, {@code false} otherwise
441 * @return The created simple field set constructor
443 public static SimpleFieldSetConstructor create(boolean shortLived) {
444 return new SimpleFieldSetConstructor(shortLived);