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;
27 import net.pterodactylus.util.object.Default;
28 import net.pterodactylus.wotns.freenet.wot.Identity;
29 import net.pterodactylus.wotns.freenet.wot.IdentityManager;
30 import net.pterodactylus.wotns.freenet.wot.OwnIdentity;
31 import net.pterodactylus.wotns.freenet.wot.Trust;
32 import freenet.keys.FreenetURI;
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class Resolver {
41 private final IdentityManager identityManager;
43 private String ownIdentityId;
45 public Resolver(IdentityManager identityManager) {
46 this.identityManager = identityManager;
49 public void setOwnIdentityId(String ownIdentityId) {
50 this.ownIdentityId = ownIdentityId;
57 public FreenetURI resolveURI(String shortUri) throws MalformedURLException {
58 int firstSlash = shortUri.indexOf('/');
59 if (firstSlash == -1) {
60 throw new MalformedURLException("At least one slash is required.");
62 String shortName = shortUri.substring(0, firstSlash);
63 String target = shortUri.substring(firstSlash + 1);
64 Identity identity = locateIdentity(shortName);
65 System.out.println("located identity: " + identity);
66 if (identity == null) {
69 return new FreenetURI(identity.getProperty("tns." + target));
76 private Identity locateIdentity(String shortName) {
77 int atSign = shortName.indexOf('@');
78 String identityName = shortName;
81 identityName = shortName.substring(0, atSign);
82 keyStart = shortName.substring(atSign + 1);
84 @SuppressWarnings("hiding")
85 final OwnIdentity ownIdentity;
86 if (this.ownIdentityId == null) {
87 Set<OwnIdentity> ownIdentities = identityManager.getAllOwnIdentities();
88 if (!ownIdentities.isEmpty()) {
89 ownIdentity = ownIdentities.iterator().next();
94 ownIdentity = identityManager.getOwnIdentity(ownIdentityId);
96 if (ownIdentity == null) {
99 System.out.println("using own identity " + ownIdentity + " to resolve " + shortName);
100 Set<Identity> trustedIdentities = Default.forNull(identityManager.getTrustedIdentities(ownIdentity), Collections.<Identity> emptySet());
101 List<Identity> matchingIdentities = new ArrayList<Identity>();
102 System.out.println("checking " + trustedIdentities);
103 for (Identity identity : trustedIdentities) {
104 if (identity.getNickname().equals(identityName) && identity.getId().startsWith(keyStart)) {
105 matchingIdentities.add(identity);
108 if (matchingIdentities.isEmpty()) {
111 Collections.sort(matchingIdentities, new Comparator<Identity>() {
114 public int compare(Identity leftIdentity, Identity rightIdentity) {
115 Trust leftTrust = leftIdentity.getTrust(ownIdentity);
116 Trust rightTrust = rightIdentity.getTrust(ownIdentity);
117 int leftTrustCombined = ((leftTrust.getExplicit() != null) ? leftTrust.getExplicit() : 0) + ((leftTrust.getImplicit() != null) ? leftTrust.getImplicit() : 0);
118 int rightTrustCombined = ((rightTrust.getExplicit() != null) ? rightTrust.getExplicit() : 0) + ((rightTrust.getImplicit() != null) ? rightTrust.getImplicit() : 0);
119 return leftTrustCombined - rightTrustCombined;
122 return matchingIdentities.get(0);