2 * jFCPlib - WebOfTrustPlugin.java -
3 * Copyright © 2009 David Roden
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.
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.
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.
20 package net.pterodactylus.fcp.plugin;
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.HashSet;
28 import net.pterodactylus.fcp.highlevel.FcpClient;
29 import net.pterodactylus.fcp.highlevel.FcpException;
32 * Simplifies handling of the web-of-trust plugin.
34 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36 public class WebOfTrustPlugin {
38 /** The FCP client to use. */
39 private final FcpClient fcpClient;
42 * Creates a new web-of-trust plugin wrapper around the given FCP client.
45 * The FCP client to use for communication with the web-of-trust
48 public WebOfTrustPlugin(FcpClient fcpClient) {
49 this.fcpClient = fcpClient;
53 * Creates a new identity.
56 * The nickname of the new identity
58 * The context for the new identity
59 * @param publishTrustList
60 * {@code true} if the new identity should publish its trust list
61 * @return The new identity
63 * if an I/O error occurs
64 * @throws FcpException
65 * if an FCP error occurs
67 public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList) throws IOException, FcpException {
68 return createIdentity(nickname, context, publishTrustList, null, null);
72 * Creates a new identity from the given request and insert URI.
75 * The nickname of the new identity
77 * The context for the new identity
78 * @param publishTrustList
79 * {@code true} if the new identity should publish its trust list
81 * The request URI of the identity
83 * The insert URI of the identity
84 * @return The new identity
86 * if an I/O error occurs
87 * @throws FcpException
88 * if an FCP error occurs
90 public OwnIdentity createIdentity(String nickname, String context, boolean publishTrustList, String requestUri, String insertUri) throws IOException, FcpException {
91 Map<String, String> parameters = new HashMap<String, String>();
92 parameters.put("Message", "CreateIdentity");
93 parameters.put("Nickname", nickname);
94 parameters.put("Context", context);
95 parameters.put("PublishTrustList", String.valueOf(publishTrustList));
96 if ((requestUri != null) && (insertUri != null)) {
97 parameters.put("RequestURI", requestUri);
98 parameters.put("InsertURI", insertUri);
100 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", parameters);
101 if (!replies.get("Message").equals("IdentityCreated")) {
102 throw new FcpException("WebOfTrust Plugin did not reply with “IdentityCreated” message!");
104 String identifier = replies.get("ID");
105 String newRequestUri = replies.get("RequestURI");
106 String newInsertUri = replies.get("InsertURI");
107 return new OwnIdentity(identifier, nickname, newRequestUri, newInsertUri);
111 * Returns all own identities of the web-of-trust plugins. Almost all other
112 * commands require an {@link OwnIdentity} to return meaningful values.
114 * @return All own identities of the web-of-trust plugin
115 * @throws IOException
116 * if an I/O error occurs
117 * @throws FcpException
118 * if an FCP error occurs
120 public Set<OwnIdentity> getOwnIdentites() throws IOException, FcpException {
121 Map<String, String> replies = fcpClient.sendPluginMessage("plugins.WoT.WoT", createParameters("Message", "GetOwnIdentities"));
122 if (!replies.get("Message").equals("OwnIdentities")) {
123 throw new FcpException("WebOfTrust Plugin did not reply with “OwnIdentities” message!");
125 Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
126 for (int identityIndex = 1; replies.containsKey("Identity" + identityIndex); identityIndex++) {
127 String identity = replies.get("Identity" + identityIndex);
128 String nickname = replies.get("Nickname" + identityIndex);
129 String requestUri = replies.get("RequestURI" + identityIndex);
130 String insertUri = replies.get("InsertURI" + identityIndex);
131 ownIdentities.add(new OwnIdentity(identity, nickname, requestUri, insertUri));
133 return ownIdentities;
141 * Creates a map from each pair of parameters in the given array.
144 * The array of parameters
145 * @return The map created from the array
146 * @throws ArrayIndexOutOfBoundsException
147 * if the given parameter array does not contains an even number
150 private Map<String, String> createParameters(String... parameters) throws ArrayIndexOutOfBoundsException {
151 Map<String, String> parameterMap = new HashMap<String, String>();
152 for (int index = 0; index < parameters.length; index += 2) {
153 parameterMap.put(parameters[index], parameters[index + 1]);
159 * Wrapper around a web-of-trust identity.
161 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
163 public static class Identity {
165 /** The identity’s identifier. */
166 private final String identifier;
168 /** The identity’s nickname. */
169 private final String nickname;
171 /** The identity’s request URI. */
172 private final String requestUri;
174 /** The identity’s trust value. */
175 private final Byte trust;
177 /** The identity’s score value. */
178 private final Integer score;
180 /** The identity’s rank. */
181 private final Integer rank;
184 * Creates a new identity.
187 * The identifies of the identity
189 * The nickname of the identity
191 * The request URI of the identity
193 * The trust value of the identity
195 * The score value of the identity
197 * The rank of the identity
199 public Identity(String identifier, String nickname, String requestUri, Byte trust, Integer score, Integer rank) {
200 this.identifier = identifier;
201 this.nickname = nickname;
202 this.requestUri = requestUri;
209 * Returns the identifier of this identity.
211 * @return This identity’s identifier
213 public String getIdentifier() {
218 * Returns the nickname of this identity.
220 * @return This identity’s nickname
222 public String getNickname() {
227 * Returns the request URI of this identity.
229 * @return This identity’s request URI
231 public String getRequestUri() {
236 * Returns the trust value of this identity.
238 * @return This identity’s trust value, or {@code null} if this
239 * identity’s trust value is not known
241 public Byte getTrust() {
246 * Returns the score value of this identity.
248 * @return This identity’s score value, or {@code null} if this
249 * identity’s score value is not known
251 public Integer getScore() {
256 * Returns the rank of this identity.
258 * @return This identity’s rank, or {@code null} if this identity’s rank
261 public Integer getRank() {
268 * Wrapper around a web-of-trust own identity.
270 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
272 public static class OwnIdentity {
274 /** The identity’s identifier. */
275 private final String identifier;
277 /** The identity’s nickname. */
278 private final String nickname;
280 /** The identity’s request URI. */
281 private final String requestUri;
283 /** The identity’s insert URI. */
284 private final String insertUri;
287 * Creates a new web-of-trust own identity.
290 * The identifier of the identity
292 * The nickname of the identity
294 * The request URI of the identity
296 * The insert URI of the identity
298 public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri) {
299 this.identifier = identifier;
300 this.nickname = nickname;
301 this.requestUri = requestUri;
302 this.insertUri = insertUri;
306 * Returns the identifier of this identity.
308 * @return This identity’s identifier
310 public String getIdentifier() {
315 * Returns the nickname of this identity.
317 * @return This identity’s nickname
319 public String getNickname() {
324 * Returns the request URI of this identity.
326 * @return This identity’s request URI
328 public String getRequestUri() {
333 * Returns the insert URI of this identity.
335 * @return This identity’s insert URI
337 public String getInsertUri() {