From: David ‘Bombe’ Roden Date: Wed, 15 Dec 2010 04:42:22 +0000 (+0100) Subject: Add method to retrieve the trust of an identity for an own identity. X-Git-Tag: 0.4^2~8^2~56 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=492506fb5a06f01a493cb9408ace3d435bd56f6b Add method to retrieve the trust of an identity for an own identity. --- diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java index 288a99e..d262d57 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultIdentity.java @@ -23,6 +23,14 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import net.pterodactylus.sone.freenet.plugin.PluginException; +import net.pterodactylus.util.cache.Cache; +import net.pterodactylus.util.cache.CacheException; +import net.pterodactylus.util.cache.CacheItem; +import net.pterodactylus.util.cache.DefaultCacheItem; +import net.pterodactylus.util.cache.MemoryCache; +import net.pterodactylus.util.cache.ValueRetriever; + /** * A Web of Trust identity. * @@ -30,6 +38,9 @@ import java.util.Set; */ public class DefaultIdentity implements Identity { + /** The web of trust connector. */ + private final WebOfTrustConnector webOfTrustConnector; + /** The ID of the identity. */ private final String id; @@ -45,9 +56,26 @@ public class DefaultIdentity implements Identity { /** The properties of the identity. */ private final Map properties = Collections.synchronizedMap(new HashMap()); + /** Cached trust. */ + private final Cache trustCache = new MemoryCache(new ValueRetriever() { + + @Override + @SuppressWarnings("synthetic-access") + public CacheItem retrieve(OwnIdentity ownIdentity) throws CacheException { + try { + return new DefaultCacheItem(webOfTrustConnector.getTrust(ownIdentity, DefaultIdentity.this)); + } catch (PluginException pe1) { + throw new CacheException("Could not retrieve trust for OwnIdentity: " + ownIdentity, pe1); + } + } + + }); + /** * Creates a new identity. * + * @param webOfTrustConnector + * The web of trust connector * @param id * The ID of the identity * @param nickname @@ -55,7 +83,8 @@ public class DefaultIdentity implements Identity { * @param requestUri * The request URI of the identity */ - public DefaultIdentity(String id, String nickname, String requestUri) { + public DefaultIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) { + this.webOfTrustConnector = webOfTrustConnector; this.id = id; this.nickname = nickname; this.requestUri = requestUri; @@ -207,6 +236,18 @@ public class DefaultIdentity implements Identity { } } + /** + * {@inheritDoc} + */ + @Override + public Trust getTrust(OwnIdentity ownIdentity) throws WebOfTrustException { + try { + return trustCache.get(ownIdentity); + } catch (CacheException ce1) { + throw new WebOfTrustException("Could not get trust for OwnIdentity: " + ownIdentity, ce1); + } + } + // // OBJECT METHODS // diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentity.java index 01ddc12..f89ddda 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentity.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentity.java @@ -50,7 +50,7 @@ public class DefaultOwnIdentity extends DefaultIdentity implements OwnIdentity { * The insert URI of the identity */ public DefaultOwnIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri, String insertUri) { - super(id, nickname, requestUri); + super(webOfTrustConnector, id, nickname, requestUri); this.webOfTrustConnector = webOfTrustConnector; this.insertUri = insertUri; } diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java b/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java index 6df6447..41540e5 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java @@ -83,4 +83,15 @@ public interface Identity { */ public String getProperty(String name); + /** + * Retrieves the trust that this identity receives from the given own + * identity. + * + * @param ownIdentity + * The own identity to get the trust for + * @return The trust assigned to this identity + * @throws WebOfTrustException + */ + public Trust getTrust(OwnIdentity ownIdentity) throws WebOfTrustException; + } diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java b/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java new file mode 100644 index 0000000..975dd3f --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java @@ -0,0 +1,82 @@ +/* + * Sone - Trust.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.freenet.wot; + +/** + * Container class for trust in the web of trust. + * + * @author David ‘Bombe’ Roden + */ +public class Trust { + + /** Explicitely assigned trust. */ + private final Integer explicit; + + /** Implicitely calculated trust. */ + private final Integer implicit; + + /** The distance from the owner of the trust tree. */ + private final Integer distance; + + /** + * Creates a new trust container. + * + * @param explicit + * The explicit trust + * @param implicit + * The implicit trust + * @param distance + * The distance + */ + public Trust(Integer explicit, Integer implicit, Integer distance) { + this.explicit = explicit; + this.implicit = implicit; + this.distance = distance; + } + + /** + * Returns the trust explicitely assigned to an identity. + * + * @return The explicitely assigned trust, or {@code null} if the identity + * is not in the own identity’s trust tree + */ + public Integer getExplicit() { + return explicit; + } + + /** + * Returns the implicitely assigned trust, or the calculated trust. + * + * @return The calculated trust, or {@code null} if the identity is not in + * the own identity’s trust tree + */ + public Integer getImplicit() { + return implicit; + } + + /** + * Returns the distance of the trusted identity from the trusting identity. + * + * @return The distance from the own identity, or {@code null} if the + * identity is not in the own identity’s trust tree + */ + public Integer getDistance() { + return distance; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java index 073db64..5d85082 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java @@ -136,7 +136,7 @@ public class WebOfTrustConnector implements ConnectorListener { } String nickname = fields.get("Nickname" + identityCounter); String requestUri = fields.get("RequestURI" + identityCounter); - DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri); + DefaultIdentity identity = new DefaultIdentity(this, id, nickname, requestUri); identity.setContextsPrivate(parseContexts("Contexts" + identityCounter + ".", fields)); identity.setPropertiesPrivate(parseProperties("Properties" + identityCounter + ".", fields)); identities.add(identity); @@ -219,6 +219,40 @@ public class WebOfTrustConnector implements ConnectorListener { } /** + * 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("TreeOwner", ownIdentity.getId()).put("Identity", identity.getId()).get(), "Identity"); + 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