Treat invalid own identity as no own identity.
[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.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;
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 String getOwnIdentityId() {
50                 return ownIdentityId;
51         }
52
53         public void setOwnIdentityId(String ownIdentityId) {
54                 this.ownIdentityId = ownIdentityId;
55         }
56
57         //
58         // ACTIONS
59         //
60
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.");
65                 }
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) {
71                         return null;
72                 }
73                 return new FreenetURI(identity.getProperty("tns." + target));
74         }
75
76         //
77         // PRIVATE METHODS
78         //
79
80         private Identity locateIdentity(String shortName) {
81                 int atSign = shortName.indexOf('@');
82                 String identityName = shortName;
83                 String keyStart = "";
84                 if (atSign > -1) {
85                         identityName = shortName.substring(0, atSign);
86                         keyStart = shortName.substring(atSign + 1);
87                 }
88                 @SuppressWarnings("hiding")
89                 final OwnIdentity ownIdentity;
90                 if ((this.ownIdentityId != null) && (identityManager.getOwnIdentity(this.ownIdentityId) != null)) {
91                         ownIdentity = identityManager.getOwnIdentity(this.ownIdentityId);
92                 } else if (this.ownIdentityId == null) {
93                         Set<OwnIdentity> ownIdentities = identityManager.getAllOwnIdentities();
94                         if (!ownIdentities.isEmpty()) {
95                                 ownIdentity = ownIdentities.iterator().next();
96                         } else {
97                                 ownIdentity = null;
98                         }
99                 } else {
100                         ownIdentity = null;
101                 }
102                 if (ownIdentity == null) {
103                         return null;
104                 }
105                 System.out.println("using own identity " + ownIdentity + " to resolve " + shortName);
106                 Set<Identity> trustedIdentities = Default.forNull(identityManager.getTrustedIdentities(ownIdentity), Collections.<Identity> emptySet());
107                 List<Identity> matchingIdentities = new ArrayList<Identity>();
108                 System.out.println("checking " + trustedIdentities);
109                 for (Identity identity : trustedIdentities) {
110                         if (identity.getNickname().equals(identityName) && identity.getId().startsWith(keyStart)) {
111                                 matchingIdentities.add(identity);
112                         }
113                 }
114                 if (matchingIdentities.isEmpty()) {
115                         return null;
116                 }
117                 Collections.sort(matchingIdentities, new Comparator<Identity>() {
118
119                         @Override
120                         public int compare(Identity leftIdentity, Identity rightIdentity) {
121                                 Trust leftTrust = leftIdentity.getTrust(ownIdentity);
122                                 Trust rightTrust = rightIdentity.getTrust(ownIdentity);
123                                 int leftTrustCombined = ((leftTrust.getExplicit() != null) ? leftTrust.getExplicit() : 0) + ((leftTrust.getImplicit() != null) ? leftTrust.getImplicit() : 0);
124                                 int rightTrustCombined = ((rightTrust.getExplicit() != null) ? rightTrust.getExplicit() : 0) + ((rightTrust.getImplicit() != null) ? rightTrust.getImplicit() : 0);
125                                 return leftTrustCombined - rightTrustCombined;
126                         }
127                 });
128                 return matchingIdentities.get(0);
129         }
130
131 }