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 * Returns all own identities of the web-of-trust plugins. Almost all other
54 * commands require an {@link OwnIdentity} to return meaningful values.
56 * @return All own identities of the web-of-trust plugin
58 * if an I/O error occurs
59 * @throws FcpException
60 * if an FCP error occurs
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!");
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));
83 * Creates a map from each pair of parameters in the given array.
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
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]);
101 * Wrapper around a web-of-trust identity.
103 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
105 public static class Identity {
107 /** The identity’s identifier. */
108 private final String identifier;
110 /** The identity’s nickname. */
111 private final String nickname;
113 /** The identity’s request URI. */
114 private final String requestUri;
117 * Creates a new identity.
120 * The identifies of the identity
122 * The nickname of the identity
124 * The request URI of the identity
126 public Identity(String identifier, String nickname, String requestUri) {
127 this.identifier = identifier;
128 this.nickname = nickname;
129 this.requestUri = requestUri;
133 * Returns the identifier of this identity.
135 * @return This identity’s identifier
137 public String getIdentifier() {
142 * Returns the nickname of this identity.
144 * @return This identity’s nickname
146 public String getNickname() {
151 * Returns the request URI of this identity.
153 * @return This identity’s request URI
155 public String getRequestUri() {
162 * Wrapper around a web-of-trust own identity.
164 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
166 public static class OwnIdentity extends Identity {
168 /** The identity’s insert URI. */
169 private final String insertUri;
172 * Creates a new web-of-trust own identity.
175 * The identifier of the identity
177 * The nickname of the identity
179 * The request URI of the identity
181 * The insert URI of the identity
183 public OwnIdentity(String identifier, String nickname, String requestUri, String insertUri) {
184 super(identifier, nickname, requestUri);
185 this.insertUri = insertUri;
189 * Returns the insert URI of this identity.
191 * @return This identity’s insert URI
193 public String getInsertUri() {