2 * WoTNS - Resolver.java - Copyright © 2011 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.wotns.main;
20 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.util.logging.Logging;
30 import net.pterodactylus.util.object.Default;
31 import net.pterodactylus.wotns.freenet.wot.Identity;
32 import net.pterodactylus.wotns.freenet.wot.IdentityManager;
33 import net.pterodactylus.wotns.freenet.wot.OwnIdentity;
34 import net.pterodactylus.wotns.freenet.wot.Trust;
35 import freenet.keys.FreenetURI;
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class Resolver {
44 private static final Logger logger = Logging.getLogger(Resolver.class);
46 private final IdentityManager identityManager;
48 private String ownIdentityId;
50 public Resolver(IdentityManager identityManager) {
51 this.identityManager = identityManager;
54 public String getOwnIdentityId() {
58 public void setOwnIdentityId(String ownIdentityId) {
59 this.ownIdentityId = ownIdentityId;
66 public FreenetURI resolveURI(String shortUri) throws MalformedURLException {
67 int firstSlash = shortUri.indexOf('/');
68 if (firstSlash == -1) {
69 throw new MalformedURLException("At least one slash is required.");
71 String shortName = shortUri.substring(0, firstSlash);
72 String target = shortUri.substring(firstSlash + 1);
73 Identity identity = locateIdentity(shortName);
74 System.out.println("located identity: " + identity);
75 if (identity == null) {
78 return new FreenetURI(identity.getProperty("tns." + target));
85 private Identity locateIdentity(String shortName) {
86 int atSign = shortName.indexOf('@');
87 String identityName = shortName;
90 identityName = shortName.substring(0, atSign);
91 keyStart = shortName.substring(atSign + 1);
93 final OwnIdentity ownIdentity;
94 if (this.ownIdentityId != null) {
95 if (identityManager.getOwnIdentity(this.ownIdentityId) != null) {
96 ownIdentity = identityManager.getOwnIdentity(this.ownIdentityId);
98 ownIdentity = getFirstOwnIdentity();
101 ownIdentity = getFirstOwnIdentity();
103 if (ownIdentity == null) {
104 logger.log(Level.SEVERE, "Can not resolve “" + shortName + "” without a Web of Trust Identity!");
107 System.out.println("using own identity " + ownIdentity + " to resolve " + shortName);
108 Set<Identity> trustedIdentities = Default.forNull(identityManager.getTrustedIdentities(ownIdentity), Collections.<Identity> emptySet());
109 List<Identity> matchingIdentities = new ArrayList<Identity>();
110 System.out.println("checking " + trustedIdentities);
111 for (Identity identity : trustedIdentities) {
112 if (identity.getNickname().equals(identityName) && identity.getId().startsWith(keyStart)) {
113 matchingIdentities.add(identity);
116 if (matchingIdentities.isEmpty()) {
119 Collections.sort(matchingIdentities, new Comparator<Identity>() {
122 public int compare(Identity leftIdentity, Identity rightIdentity) {
123 Trust leftTrust = leftIdentity.getTrust(ownIdentity);
124 Trust rightTrust = rightIdentity.getTrust(ownIdentity);
125 int leftTrustCombined = ((leftTrust.getExplicit() != null) ? leftTrust.getExplicit() : 0) + ((leftTrust.getImplicit() != null) ? leftTrust.getImplicit() : 0);
126 int rightTrustCombined = ((rightTrust.getExplicit() != null) ? rightTrust.getExplicit() : 0) + ((rightTrust.getImplicit() != null) ? rightTrust.getImplicit() : 0);
127 return leftTrustCombined - rightTrustCombined;
130 return matchingIdentities.get(0);
133 private OwnIdentity getFirstOwnIdentity() {
134 Set<OwnIdentity> ownIdentities = identityManager.getAllOwnIdentities();
135 if (!ownIdentities.isEmpty()) {
136 return ownIdentities.iterator().next();