Use default values if values can not be parsed.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / WebOfTrustConnector.java
index 4ef26cb..c4e8d89 100644 (file)
@@ -29,6 +29,7 @@ import net.pterodactylus.sone.freenet.plugin.ConnectorListener;
 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 freenet.support.SimpleFieldSet;
 import freenet.support.api.Bucket;
 
@@ -128,7 +129,7 @@ public class WebOfTrustConnector {
         *             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;
@@ -142,6 +143,10 @@ public class WebOfTrustConnector {
                        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;
@@ -312,7 +317,7 @@ public class WebOfTrustConnector {
         *            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) {
@@ -334,7 +339,7 @@ public class WebOfTrustConnector {
         *            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) {
@@ -394,14 +399,19 @@ public class WebOfTrustConnector {
                };
                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"));