Add wrapper around web-of-trust plugin.
[jFCPlib.git] / src / net / pterodactylus / fcp / plugin / WebOfTrustPlugin.java
1 /*
2  * jFCPlib - WebOfTrustPlugin.java -
3  * Copyright © 2009 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.fcp.plugin;
21
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28 import net.pterodactylus.fcp.highlevel.FcpClient;
29 import net.pterodactylus.fcp.highlevel.FcpException;
30
31 /**
32  * Simplifies handling of the web-of-trust plugin.
33  *
34  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35  */
36 public class WebOfTrustPlugin {
37
38         /** The FCP client to use. */
39         private final FcpClient fcpClient;
40
41         /**
42          * Creates a new web-of-trust plugin wrapper around the given FCP client.
43          *
44          * @param fcpClient
45          *            The FCP client to use for communication with the web-of-trust
46          *            plugin
47          */
48         public WebOfTrustPlugin(FcpClient fcpClient) {
49                 this.fcpClient = fcpClient;
50         }
51
52         /**
53          * Returns all own identities of the web-of-trust plugins. Almost all other
54          * commands require an {@link OwnIdentity} to return meaningful values.
55          *
56          * @return All own identities of the web-of-trust plugin
57          * @throws IOException
58          *             if an I/O error occurs
59          * @throws FcpException
60          *             if an FCP error occurs
61          */
62         public Set<OwnIdentity> getOwnIdentites() throws IOException, FcpException {
63                 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetOwnIdentities"));
64                 if (!replies.get("Message").equals("OwnIdentities")) {
65                         throw new FcpException("WebOfTrust Plugin did not reply with “OwnIdentities” message!");
66                 }
67                 Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
68                 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
69                         String identity = replies.get("Identity" + identityIndex);
70                         String nickname = replies.get("Nickname" + identityIndex);
71                         String requestUri = replies.get("RequestURI" + identityIndex);
72                         String insertUri = replies.get("InsertURI" + identityIndex);
73                         ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri));
74                 }
75                 return ownIdentities;
76         }
77
78         //
79         // PRIVATE METHODS
80         //
81
82         /**
83          * Creates a map from each pair of parameters in the given array.
84          *
85          * @param parameters
86          *            The array of parameters
87          * @return The map created from the array
88          * @throws ArrayIndexOutOfBoundsException
89          *             if the given parameter array does not contains an even number
90          *             of elements
91          */
92         private Map<String, String> createParameters(String... parameters) throws ArrayIndexOutOfBoundsException {
93                 Map<String, String> parameterMap = new HashMap<String, String>();
94                 for (int index = 0; index < parameters.length; index += 2) {
95                         parameterMap.put(parameters[index], parameters[index + 1]);
96                 }
97                 return parameterMap;
98         }
99
100         /**
101          * Wrapper around a web-of-trust identity.
102          *
103          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
104          */
105         public static class Identity {
106
107                 /** The identity’s identifier. */
108                 private final String identifier;
109
110                 /** The identity’s nickname. */
111                 private final String nickname;
112
113                 /** The identity’s request URI. */
114                 private final String requestUri;
115
116                 /**
117                  * Creates a new identity.
118                  *
119                  * @param identifier
120                  *            The identifies of the identity
121                  * @param nickname
122                  *            The nickname of the identity
123                  * @param requestUri
124                  *            The request URI of the identity
125                  */
126                 public Identity(String identifier, String nickname, String requestUri) {
127                         this.identifier = identifier;
128                         this.nickname = nickname;
129                         this.requestUri = requestUri;
130                 }
131
132                 /**
133                  * Returns the identifier of this identity.
134                  *
135                  * @return This identity’s identifier
136                  */
137                 public String getIdentifier() {
138                         return identifier;
139                 }
140
141                 /**
142                  * Returns the nickname of this identity.
143                  *
144                  * @return This identity’s nickname
145                  */
146                 public String getNickname() {
147                         return nickname;
148                 }
149
150                 /**
151                  * Returns the request URI of this identity.
152                  *
153                  * @return This identity’s request URI
154                  */
155                 public String getRequestUri() {
156                         return requestUri;
157                 }
158
159         }
160
161         /**
162          * Wrapper around a web-of-trust own identity.
163          *
164          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
165          */
166         public static class OwnIdentity extends Identity {
167
168                 /** The identity’s insert URI. */
169                 private final String insertUri;
170
171                 /**
172                  * Creates a new web-of-trust own identity.
173                  *
174                  * @param identifier
175                  *            The identifier of the identity
176                  * @param nickname
177                  *            The nickname of the identity
178                  * @param requestUri
179                  *            The request URI of the identity
180                  * @param insertUri
181                  *            The insert URI of the identity
182                  */
183                 public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri) {
184                         super(identifier, nickname, requestUri);
185                         this.insertUri = insertUri;
186                 }
187
188                 /**
189                  * Returns the insert URI of this identity.
190                  *
191                  * @return This identity’s insert URI
192                  */
193                 public String getInsertUri() {
194                         return insertUri;
195                 }
196
197         }
198
199 }