94a098ea551a11a4860650122872727f4cb45f33
[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) {
95                         if (identityManager.getOwnIdentity(this.ownIdentityId) != null) {
96                                 ownIdentity = identityManager.getOwnIdentity(this.ownIdentityId);
97                         } else {
98                                 ownIdentity = getFirstOwnIdentity();
99                         }
100                 } else {
101                         ownIdentity = getFirstOwnIdentity();
102                 }
103                 if (ownIdentity == null) {
104                         logger.log(Level.SEVERE, "Can not resolve “" + shortName + "” without a Web of Trust Identity!");
105                         return null;
106                 }
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);
114                         }
115                 }
116                 if (matchingIdentities.isEmpty()) {
117                         return null;
118                 }
119                 Collections.sort(matchingIdentities, new Comparator<Identity>() {
120
121                         @Override
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;
128                         }
129                 });
130                 return matchingIdentities.get(0);
131         }
132
133         private OwnIdentity getFirstOwnIdentity() {
134                 Set<OwnIdentity> ownIdentities = identityManager.getAllOwnIdentities();
135                 if (!ownIdentities.isEmpty()) {
136                         return ownIdentities.iterator().next();
137                 }
138                 return null;
139         }
140
141 }