+ /**
+ * Loads all identities that the given identities trusts with a score of
+ * more than 0.
+ *
+ * @param ownIdentity
+ * The own identity
+ * @return All trusted identities
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity) throws PluginException {
+ return loadTrustedIdentities(ownIdentity, null);
+ }
+
+ /**
+ * Loads all identities that the given identities trusts with a score of
+ * more than 0 and the (optional) given context.
+ *
+ * @param ownIdentity
+ * The own identity
+ * @param context
+ * The context to filter, or {@code null}
+ * @return All trusted identities
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, @Nullable String context) throws PluginException {
+ Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context ==null) ? "" : context).put("WantTrustValues", "true").get());
+ SimpleFieldSet fields = reply.getFields();
+ Set<Identity> identities = new HashSet<>();
+ int identityCounter = -1;
+ while (true) {
+ String id = fields.get("Identity" + ++identityCounter);
+ if (id == null) {
+ break;
+ }
+ String nickname = fields.get("Nickname" + identityCounter);
+ String requestUri = fields.get("RequestURI" + identityCounter);
+ DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri);
+ identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields));
+ identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields));
+ Integer trust = parseInt(fields.get("Trust" + identityCounter), null);
+ int score = parseInt(fields.get("Score" + identityCounter), 0);
+ int rank = parseInt(fields.get("Rank" + identityCounter), 0);
+ identity.setTrust(ownIdentity, new Trust(trust, score, rank));
+ identities.add(identity);
+ }
+ return identities;
+ }
+
+ /**
+ * Adds the given context to the given identity.
+ *
+ * @param ownIdentity
+ * The identity to add the context to
+ * @param context
+ * The context to add
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public void addContext(OwnIdentity ownIdentity, String context) throws PluginException {
+ performRequest(SimpleFieldSetConstructor.create().put("Message", "AddContext").put("Identity", ownIdentity.getId()).put("Context", context).get());
+ }
+
+ /**
+ * Removes the given context from the given identity.
+ *
+ * @param ownIdentity
+ * The identity to remove the context from
+ * @param context
+ * The context to remove
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public void removeContext(OwnIdentity ownIdentity, String context) throws PluginException {
+ performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveContext").put("Identity", ownIdentity.getId()).put("Context", context).get());
+ }
+
+ /**
+ * Returns the value of the property with the given name.
+ *
+ * @param identity
+ * The identity whose properties to check
+ * @param name
+ * The name of the property to return
+ * @return The value of the property, or {@code null} if there is no value
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public String getProperty(Identity identity, String name) throws PluginException {
+ Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetProperty").put("Identity", identity.getId()).put("Property", name).get());
+ return reply.getFields().get("Property");
+ }
+
+ /**
+ * Sets the property with the given name to the given value.
+ *
+ * @param ownIdentity
+ * The identity to set the property on
+ * @param name
+ * The name of the property to set
+ * @param value
+ * The value to set
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public void setProperty(OwnIdentity ownIdentity, String name, String value) throws PluginException {
+ performRequest(SimpleFieldSetConstructor.create().put("Message", "SetProperty").put("Identity", ownIdentity.getId()).put("Property", name).put("Value", value).get());
+ }
+
+ /**
+ * Removes the property with the given name.
+ *
+ * @param ownIdentity
+ * The identity to remove the property from
+ * @param name
+ * The name of the property to remove
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public void removeProperty(OwnIdentity ownIdentity, String name) throws PluginException {
+ performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveProperty").put("Identity", ownIdentity.getId()).put("Property", name).get());
+ }
+
+ /**
+ * Returns the trust for the given identity assigned to it by the given own
+ * identity.
+ *
+ * @param ownIdentity
+ * The own identity
+ * @param identity
+ * The identity to get the trust for
+ * @return The trust for the given identity
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public Trust getTrust(OwnIdentity ownIdentity, Identity identity) throws PluginException {
+ Reply getTrustReply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentity").put("Truster", ownIdentity.getId()).put("Identity", identity.getId()).get());
+ String trust = getTrustReply.getFields().get("Trust");
+ String score = getTrustReply.getFields().get("Score");
+ String rank = getTrustReply.getFields().get("Rank");
+ Integer explicit = null;
+ Integer implicit = null;
+ Integer distance = null;
+ try {
+ explicit = Integer.valueOf(trust);
+ } catch (NumberFormatException nfe1) {
+ /* ignore. */
+ }
+ try {
+ implicit = Integer.valueOf(score);
+ distance = Integer.valueOf(rank);
+ } catch (NumberFormatException nfe1) {
+ /* ignore. */
+ }
+ return new Trust(explicit, implicit, distance);
+ }
+
+ /**
+ * Sets the trust for the given identity.
+ *
+ * @param ownIdentity
+ * The trusting identity
+ * @param identity
+ * The trusted identity
+ * @param trust
+ * The amount of trust (-100 thru 100)
+ * @param comment
+ * The comment or explanation of the trust value
+ * @throws PluginException
+ * if an error occured talking to the Web of Trust plugin
+ */
+ public void setTrust(OwnIdentity ownIdentity, Identity identity, int trust, String comment) throws PluginException {
+ performRequest(SimpleFieldSetConstructor.create().put("Message", "SetTrust").put("Truster", ownIdentity.getId()).put("Trustee", identity.getId()).put("Value", String.valueOf(trust)).put("Comment", comment).get());
+ }
+
+ /**
+ * Removes any trust assignment of the given own identity for the given
+ * identity.
+ *
+ * @param ownIdentity
+ * The own identity
+ * @param identity
+ * The identity to remove all trust for
+ * @throws WebOfTrustException
+ * if an error occurs
+ */
+ public void removeTrust(OwnIdentity ownIdentity, Identity identity) throws WebOfTrustException {
+ performRequest(SimpleFieldSetConstructor.create().put("Message", "RemoveTrust").put("Truster", ownIdentity.getId()).put("Trustee", identity.getId()).get());
+ }
+
+ /**
+ * Pings the Web of Trust plugin. If the plugin can not be reached, a
+ * {@link PluginException} is thrown.
+ *
+ * @throws PluginException
+ * if the plugin is not loaded
+ */
+ public void ping() throws PluginException {
+ performRequest(SimpleFieldSetConstructor.create().put("Message", "Ping").get());
+ }
+