f623e3d500ef53ec9f75e0b69e4df072e489d9db
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / main / Resolver.java
1 /*
2  * WoTNS - Resolver.java - Copyright © 2011 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.wotns.main;
19
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;
25 import java.util.Set;
26
27 import net.pterodactylus.util.collection.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;
33
34 /**
35  * TODO
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class Resolver {
40
41         private final IdentityManager identityManager;
42
43         private String ownIdentityId;
44
45         public Resolver(IdentityManager identityManager) {
46                 this.identityManager = identityManager;
47         }
48
49         public void setOwnIdentityId(String ownIdentityId) {
50                 this.ownIdentityId = ownIdentityId;
51         }
52
53         //
54         // ACTIONS
55         //
56
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.");
61                 }
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) {
67                         return null;
68                 }
69                 return new FreenetURI(identity.getProperty("tns." + target));
70         }
71
72         //
73         // PRIVATE METHODS
74         //
75
76         private Identity locateIdentity(String shortName) {
77                 int atSign = shortName.indexOf('@');
78                 String identityName = shortName;
79                 String keyStart = "";
80                 if (atSign > -1) {
81                         identityName = shortName.substring(0, atSign);
82                         keyStart = shortName.substring(atSign + 1);
83                 }
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();
90                         } else {
91                                 ownIdentity = null;
92                         }
93                 } else {
94                         ownIdentity = identityManager.getOwnIdentity(ownIdentityId);
95                 }
96                 if (ownIdentity == null) {
97                         return null;
98                 }
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);
106                         }
107                 }
108                 if (matchingIdentities.isEmpty()) {
109                         return null;
110                 }
111                 Collections.sort(matchingIdentities, new Comparator<Identity>() {
112
113                         @Override
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;
120                         }
121                 });
122                 return matchingIdentities.get(0);
123         }
124
125 }