import net.pterodactylus.sone.freenet.plugin.PluginConnector;
import net.pterodactylus.sone.freenet.plugin.PluginException;
import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.number.Numbers;
+
+import com.google.inject.Inject;
+
import freenet.support.SimpleFieldSet;
import freenet.support.api.Bucket;
* @param pluginConnector
* The plugin connector
*/
+ @Inject
public WebOfTrustConnector(PluginConnector pluginConnector) {
this.pluginConnector = pluginConnector;
}
* if an error occured talking to the Web of Trust plugin
*/
public Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity, String context) throws PluginException {
- Reply reply = performRequest(SimpleFieldSetConstructor.create().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.getId()).put("Selection", "+").put("Context", (context == null) ? "" : context).get());
+ 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<Identity>();
int identityCounter = -1;
DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri);
identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields));
identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields));
+ Integer trust = Numbers.safeParseInteger(fields.get("Trust" + identityCounter), null);
+ int score = Numbers.safeParseInteger(fields.get("Score" + identityCounter), 0);
+ int rank = Numbers.safeParseInteger(fields.get("Rank" + identityCounter), 0);
+ identity.setTrust(ownIdentity, new Trust(trust, score, rank));
identities.add(identity);
}
return identities;
* The fields to parse the contexts from
* @return The parsed contexts
*/
- private Set<String> parseContexts(String prefix, SimpleFieldSet fields) {
+ private static Set<String> parseContexts(String prefix, SimpleFieldSet fields) {
Set<String> contexts = new HashSet<String>();
int contextCounter = -1;
while (true) {
* The fields to parse the properties from
* @return The parsed properties
*/
- private Map<String, String> parseProperties(String prefix, SimpleFieldSet fields) {
+ private static Map<String, String> parseProperties(String prefix, SimpleFieldSet fields) {
Map<String, String> properties = new HashMap<String, String>();
int propertiesCounter = -1;
while (true) {
};
pluginConnector.addConnectorListener(WOT_PLUGIN_NAME, identifier, connectorListener);
synchronized (reply) {
- pluginConnector.sendRequest(WOT_PLUGIN_NAME, identifier, fields, data);
try {
- reply.wait();
- } catch (InterruptedException ie1) {
- logger.log(Level.WARNING, String.format("Got interrupted while waiting for reply on %s.", fields.get("Message")), ie1);
+ pluginConnector.sendRequest(WOT_PLUGIN_NAME, identifier, fields, data);
+ while (reply.getFields() == null) {
+ try {
+ reply.wait();
+ } catch (InterruptedException ie1) {
+ logger.log(Level.WARNING, String.format("Got interrupted while waiting for reply on %s.", fields.get("Message")), ie1);
+ }
+ }
+ } finally {
+ pluginConnector.removeConnectorListener(WOT_PLUGIN_NAME, identifier, connectorListener);
}
}
- pluginConnector.removeConnectorListener(WOT_PLUGIN_NAME, identifier, connectorListener);
logger.log(Level.FINEST, String.format("Received FCP Response for %s: %s", fields.get("Message"), (reply.getFields() != null) ? reply.getFields().get("Message") : null));
if ((reply.getFields() == null) || "Error".equals(reply.getFields().get("Message"))) {
throw new PluginException("Could not perform request for " + fields.get("Message"));