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 String getOwnIdentityId() {
53 public void setOwnIdentityId(String ownIdentityId) {
54 this.ownIdentityId = ownIdentityId;
61 public FreenetURI resolveURI(String shortUri) throws MalformedURLException {
62 int firstSlash = shortUri.indexOf('/');
63 if (firstSlash == -1) {
64 throw new MalformedURLException("At least one slash is required.");
66 String shortName = shortUri.substring(0, firstSlash);
67 String target = shortUri.substring(firstSlash + 1);
68 Identity identity = locateIdentity(shortName);
69 System.out.println("located identity: " + identity);
70 if (identity == null) {
73 return new FreenetURI(identity.getProperty("tns." + target));
80 private Identity locateIdentity(String shortName) {
81 int atSign = shortName.indexOf('@');
82 String identityName = shortName;
85 identityName = shortName.substring(0, atSign);
86 keyStart = shortName.substring(atSign + 1);
88 @SuppressWarnings("hiding")
89 final OwnIdentity ownIdentity;
90 if (this.ownIdentityId == null) {
91 Set<OwnIdentity> ownIdentities = identityManager.getAllOwnIdentities();
92 if (!ownIdentities.isEmpty()) {
93 ownIdentity = ownIdentities.iterator().next();
98 ownIdentity = identityManager.getOwnIdentity(ownIdentityId);
100 if (ownIdentity == null) {
103 System.out.println("using own identity " + ownIdentity + " to resolve " + shortName);
104 Set<Identity> trustedIdentities = Default.forNull(identityManager.getTrustedIdentities(ownIdentity), Collections.<Identity> emptySet());
105 List<Identity> matchingIdentities = new ArrayList<Identity>();
106 System.out.println("checking " + trustedIdentities);
107 for (Identity identity : trustedIdentities) {
108 if (identity.getNickname().equals(identityName) && identity.getId().startsWith(keyStart)) {
109 matchingIdentities.add(identity);
112 if (matchingIdentities.isEmpty()) {
115 Collections.sort(matchingIdentities, new Comparator<Identity>() {
118 public int compare(Identity leftIdentity, Identity rightIdentity) {
119 Trust leftTrust = leftIdentity.getTrust(ownIdentity);
120 Trust rightTrust = rightIdentity.getTrust(ownIdentity);
121 int leftTrustCombined = ((leftTrust.getExplicit() != null) ? leftTrust.getExplicit() : 0) + ((leftTrust.getImplicit() != null) ? leftTrust.getImplicit() : 0);
122 int rightTrustCombined = ((rightTrust.getExplicit() != null) ? rightTrust.getExplicit() : 0) + ((rightTrust.getImplicit() != null) ? rightTrust.getImplicit() : 0);
123 return leftTrustCombined - rightTrustCombined;
126 return matchingIdentities.get(0);