Add method to return the first WoT identity.
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / main / Resolver.java
index 1c00144..4e53528 100644 (file)
@@ -23,7 +23,10 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
+import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.object.Default;
 import net.pterodactylus.wotns.freenet.wot.Identity;
 import net.pterodactylus.wotns.freenet.wot.IdentityManager;
@@ -38,6 +41,8 @@ import freenet.keys.FreenetURI;
  */
 public class Resolver {
 
+       private static final Logger logger = Logging.getLogger(Resolver.class);
+
        private final IdentityManager identityManager;
 
        private String ownIdentityId;
@@ -46,6 +51,10 @@ public class Resolver {
                this.identityManager = identityManager;
        }
 
+       public String getOwnIdentityId() {
+               return ownIdentityId;
+       }
+
        public void setOwnIdentityId(String ownIdentityId) {
                this.ownIdentityId = ownIdentityId;
        }
@@ -81,9 +90,10 @@ public class Resolver {
                        identityName = shortName.substring(0, atSign);
                        keyStart = shortName.substring(atSign + 1);
                }
-               @SuppressWarnings("hiding")
                final OwnIdentity ownIdentity;
-               if (this.ownIdentityId == null) {
+               if ((this.ownIdentityId != null) && (identityManager.getOwnIdentity(this.ownIdentityId) != null)) {
+                       ownIdentity = identityManager.getOwnIdentity(this.ownIdentityId);
+               } else if (this.ownIdentityId == null) {
                        Set<OwnIdentity> ownIdentities = identityManager.getAllOwnIdentities();
                        if (!ownIdentities.isEmpty()) {
                                ownIdentity = ownIdentities.iterator().next();
@@ -91,7 +101,8 @@ public class Resolver {
                                ownIdentity = null;
                        }
                } else {
-                       ownIdentity = identityManager.getOwnIdentity(ownIdentityId);
+                       logger.log(Level.SEVERE, "Can not resolve “" + shortName + "” without a Web of Trust Identity!");
+                       ownIdentity = null;
                }
                if (ownIdentity == null) {
                        return null;
@@ -122,4 +133,12 @@ public class Resolver {
                return matchingIdentities.get(0);
        }
 
+       private OwnIdentity getFirstOwnIdentity() {
+               Set<OwnIdentity> ownIdentities = identityManager.getAllOwnIdentities();
+               if (!ownIdentities.isEmpty()) {
+                       return ownIdentities.iterator().next();
+               }
+               return null;
+       }
+
 }