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.
*
*/
public class DefaultIdentity implements Identity {
+ /** The web of trust connector. */
+ private final WebOfTrustConnector webOfTrustConnector;
+
/** The ID of the identity. */
private final String id;
/** The properties of the identity. */
private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
+ /** Cached trust. */
+ private final Cache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public CacheItem<Trust> retrieve(OwnIdentity ownIdentity) throws CacheException {
+ try {
+ return new DefaultCacheItem<Trust>(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
* @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;
}
}
+ /**
+ * {@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
//
* 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;
}
*/
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;
+
}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.freenet.wot;
+
+/**
+ * Container class for trust in the web of trust.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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;
+ }
+
+}
}
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);
}
/**
+ * 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