2 * Sone - FcpInterface.java - Copyright © 2011–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import java.util.Collections;
23 import java.util.HashMap;
25 import java.util.concurrent.atomic.AtomicBoolean;
26 import java.util.concurrent.atomic.AtomicReference;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.sone.core.Core;
31 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent;
32 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
33 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged;
34 import net.pterodactylus.sone.freenet.fcp.Command.AccessType;
35 import net.pterodactylus.sone.freenet.fcp.Command.ErrorResponse;
36 import net.pterodactylus.sone.freenet.fcp.Command.Response;
37 import net.pterodactylus.util.logging.Logging;
39 import freenet.pluginmanager.FredPluginFCP;
40 import freenet.pluginmanager.PluginNotFoundException;
41 import freenet.pluginmanager.PluginReplySender;
42 import freenet.support.SimpleFieldSet;
43 import freenet.support.api.Bucket;
45 import com.google.common.annotations.VisibleForTesting;
46 import com.google.common.eventbus.Subscribe;
47 import com.google.inject.Inject;
48 import com.google.inject.Singleton;
51 * Implementation of an FCP interface for other clients or plugins to
52 * communicate with Sone.
54 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 public class FcpInterface {
60 * The action level that full access for the FCP connection is required.
62 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
64 public enum FullAccessRequired {
66 /** No action requires full access. */
69 /** All writing actions require full access. */
72 /** All actions require full access. */
78 private static final Logger logger = Logging.getLogger(FcpInterface.class);
80 /** Whether the FCP interface is currently active. */
81 private final AtomicBoolean active = new AtomicBoolean();
83 /** What function full access is required for. */
84 private final AtomicReference<FullAccessRequired> fullAccessRequired = new AtomicReference<FullAccessRequired>(FullAccessRequired.ALWAYS);
86 /** All available FCP commands. */
87 private final Map<String, AbstractSoneCommand> commands = Collections.synchronizedMap(new HashMap<String, AbstractSoneCommand>());
90 * Creates a new FCP interface.
96 public FcpInterface(Core core) {
97 commands.put("Version", new VersionCommand(core));
98 commands.put("GetLocalSones", new GetLocalSonesCommand(core));
99 commands.put("GetSones", new GetSonesCommand(core));
100 commands.put("GetSone", new GetSoneCommand(core));
101 commands.put("GetPost", new GetPostCommand(core));
102 commands.put("GetPosts", new GetPostsCommand(core));
103 commands.put("GetPostFeed", new GetPostFeedCommand(core));
104 commands.put("LockSone", new LockSoneCommand(core));
105 commands.put("UnlockSone", new UnlockSoneCommand(core));
106 commands.put("LikePost", new LikePostCommand(core));
107 commands.put("LikeReply", new LikeReplyCommand(core));
108 commands.put("CreatePost", new CreatePostCommand(core));
109 commands.put("CreateReply", new CreateReplyCommand(core));
110 commands.put("DeletePost", new DeletePostCommand(core));
111 commands.put("DeleteReply", new DeleteReplyCommand(core));
123 private void setActive(boolean active) {
124 this.active.set(active);
128 FullAccessRequired getFullAccessRequired() {
129 return fullAccessRequired.get();
132 private void setFullAccessRequired(FullAccessRequired fullAccessRequired) {
133 this.fullAccessRequired.set(checkNotNull(fullAccessRequired, "fullAccessRequired must not be null"));
141 * Handles a plugin FCP request.
143 * @param pluginReplySender
146 * The message parameters
148 * The message data (may be {@code null})
150 * One of {@link FredPluginFCP#ACCESS_DIRECT},
151 * {@link FredPluginFCP#ACCESS_FCP_FULL},
152 * {@link FredPluginFCP#ACCESS_FCP_RESTRICTED}
154 public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
157 sendReply(pluginReplySender, null, new ErrorResponse(400, "FCP Interface deactivated"));
158 } catch (PluginNotFoundException pnfe1) {
159 logger.log(Level.FINE, "Could not set error to plugin.", pnfe1);
163 AbstractSoneCommand command = commands.get(parameters.get("Message"));
164 if ((accessType == FredPluginFCP.ACCESS_FCP_RESTRICTED) && (((fullAccessRequired.get() == FullAccessRequired.WRITING) && command.requiresWriteAccess()) || (fullAccessRequired.get() == FullAccessRequired.ALWAYS))) {
166 sendReply(pluginReplySender, null, new ErrorResponse(401, "Not authorized"));
167 } catch (PluginNotFoundException pnfe1) {
168 logger.log(Level.FINE, "Could not set error to plugin.", pnfe1);
173 if (command == null) {
174 sendReply(pluginReplySender, null, new ErrorResponse("Unrecognized Message: " + parameters.get("Message")));
177 String identifier = parameters.get("Identifier");
178 if ((identifier == null) || (identifier.length() == 0)) {
179 sendReply(pluginReplySender, null, new ErrorResponse("Missing Identifier."));
183 Response response = command.execute(parameters, data, AccessType.values()[accessType]);
184 sendReply(pluginReplySender, identifier, response);
185 } catch (Exception e1) {
186 logger.log(Level.WARNING, "Could not process FCP command “%s”.", command);
187 sendReply(pluginReplySender, identifier, new ErrorResponse("Error executing command: " + e1.getMessage()));
189 } catch (PluginNotFoundException pnfe1) {
190 logger.log(Level.WARNING, "Could not find destination plugin: " + pluginReplySender);
199 * Sends the given response to the given plugin.
201 * @param pluginReplySender
204 * The identifier (may be {@code null})
206 * The response to send
207 * @throws PluginNotFoundException
208 * if the plugin can not be found
210 private static void sendReply(PluginReplySender pluginReplySender, String identifier, Response response) throws PluginNotFoundException {
211 SimpleFieldSet replyParameters = response.getReplyParameters();
212 if (identifier != null) {
213 replyParameters.putOverwrite("Identifier", identifier);
215 if (response.hasData()) {
216 pluginReplySender.send(replyParameters, response.getData());
217 } else if (response.hasBucket()) {
218 pluginReplySender.send(replyParameters, response.getBucket());
220 pluginReplySender.send(replyParameters);
225 public void fcpInterfaceActivated(FcpInterfaceActivatedEvent fcpInterfaceActivatedEvent) {
230 public void fcpInterfaceDeactivated(FcpInterfaceDeactivatedEvent fcpInterfaceDeactivatedEvent) {
235 public void fullAccessRequiredChanged(FullAccessRequiredChanged fullAccessRequiredChanged) {
236 setFullAccessRequired(fullAccessRequiredChanged.getFullAccessRequired());