12eff7e95435c30dee72238e36ecb9ad21068d58
[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 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
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;
36
37 /**
38  * TODO
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class Resolver {
43
44         private static final Logger logger = Logging.getLogger(Resolver.class);
45
46         private final IdentityManager identityManager;
47
48         private String ownIdentityId;
49
50         public Resolver(IdentityManager identityManager) {
51                 this.identityManager = identityManager;
52         }
53
54         public String getOwnIdentityId() {
55                 return ownIdentityId;
56         }
57
58         public void setOwnIdentityId(String ownIdentityId) {
59                 this.ownIdentityId = ownIdentityId;
60         }
61
62         //
63         // ACTIONS
64         //
65
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.");
70                 }
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) {
76                         return null;
77                 }
78                 return new FreenetURI(identity.getProperty("tns." + target));
79         }
80
81         //
82         // PRIVATE METHODS
83         //
84
85         private Identity locateIdentity(String shortName) {
86                 int atSign = shortName.indexOf('@');
87                 String identityName = shortName;
88                 String keyStart = "";
89                 if (atSign > -1) {
90                         identityName = shortName.substring(0, atSign);
91                         keyStart = shortName.substring(atSign + 1);
92                 }
93                 final OwnIdentity ownIdentity;
94                 if ((this.ownIdentityId != null) && (identityManager.getOwnIdentity(this.ownIdentityId) != null)) {
95                         ownIdentity = identityManager.getOwnIdentity(this.ownIdentityId);
96                 } else if (this.ownIdentityId == null) {
97                         Set<OwnIdentity> ownIdentities = identityManager.getAllOwnIdentities();
98                         if (!ownIdentities.isEmpty()) {
99                                 ownIdentity = ownIdentities.iterator().next();
100                         } else {
101                                 ownIdentity = null;
102                         }
103                 } else {
104                         logger.log(Level.SEVERE, "Can not resolve “" + shortName + "” without a Web of Trust Identity!");
105                         ownIdentity = null;
106                 }
107                 if (ownIdentity == null) {
108                         return null;
109                 }
110                 System.out.println("using own identity " + ownIdentity + " to resolve " + shortName);
111                 Set<Identity> trustedIdentities = Default.forNull(identityManager.getTrustedIdentities(ownIdentity), Collections.<Identity> emptySet());
112                 List<Identity> matchingIdentities = new ArrayList<Identity>();
113                 System.out.println("checking " + trustedIdentities);
114                 for (Identity identity : trustedIdentities) {
115                         if (identity.getNickname().equals(identityName) && identity.getId().startsWith(keyStart)) {
116                                 matchingIdentities.add(identity);
117                         }
118                 }
119                 if (matchingIdentities.isEmpty()) {
120                         return null;
121                 }
122                 Collections.sort(matchingIdentities, new Comparator<Identity>() {
123
124                         @Override
125                         public int compare(Identity leftIdentity, Identity rightIdentity) {
126                                 Trust leftTrust = leftIdentity.getTrust(ownIdentity);
127                                 Trust rightTrust = rightIdentity.getTrust(ownIdentity);
128                                 int leftTrustCombined = ((leftTrust.getExplicit() != null) ? leftTrust.getExplicit() : 0) + ((leftTrust.getImplicit() != null) ? leftTrust.getImplicit() : 0);
129                                 int rightTrustCombined = ((rightTrust.getExplicit() != null) ? rightTrust.getExplicit() : 0) + ((rightTrust.getImplicit() != null) ? rightTrust.getImplicit() : 0);
130                                 return leftTrustCombined - rightTrustCombined;
131                         }
132                 });
133                 return matchingIdentities.get(0);
134         }
135
136 }