Add “DeletePost” FCP command.
[Sone.git] / src / main / java / net / pterodactylus / sone / fcp / FcpInterface.java
1 /*
2  * Sone - FcpInterface.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.sone.fcp;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26 import net.pterodactylus.sone.core.Core;
27 import net.pterodactylus.sone.freenet.fcp.Command;
28 import net.pterodactylus.sone.freenet.fcp.Command.AccessType;
29 import net.pterodactylus.sone.freenet.fcp.Command.ErrorResponse;
30 import net.pterodactylus.sone.freenet.fcp.Command.Response;
31 import net.pterodactylus.sone.freenet.fcp.FcpException;
32 import net.pterodactylus.util.logging.Logging;
33 import freenet.pluginmanager.FredPluginFCP;
34 import freenet.pluginmanager.PluginNotFoundException;
35 import freenet.pluginmanager.PluginReplySender;
36 import freenet.support.SimpleFieldSet;
37 import freenet.support.api.Bucket;
38
39 /**
40  * Implementation of an FCP interface for other clients or plugins to
41  * communicate with Sone.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class FcpInterface {
46
47         /** The logger. */
48         private static final Logger logger = Logging.getLogger(FcpInterface.class);
49
50         /** Whether the FCP interface is currently active. */
51         private volatile boolean active;
52
53         /** All available FCP commands. */
54         private final Map<String, Command> commands = Collections.synchronizedMap(new HashMap<String, Command>());
55
56         /**
57          * Creates a new FCP interface.
58          *
59          * @param core
60          *            The core
61          */
62         public FcpInterface(Core core) {
63                 commands.put("Version", new VersionCommand());
64                 commands.put("GetLocalSones", new GetLocalSonesCommand(core));
65                 commands.put("GetPost", new GetPostCommand(core));
66                 commands.put("GetPosts", new GetPostsCommand(core));
67                 commands.put("GetPostFeed", new GetPostFeedCommand(core));
68                 commands.put("LikePost", new LikePostCommand(core));
69                 commands.put("LikeReply", new LikeReplyCommand(core));
70                 commands.put("CreatePost", new CreatePostCommand(core));
71                 commands.put("CreateReply", new CreateReplyCommand(core));
72                 commands.put("DeletePost", new DeletePostCommand(core));
73         }
74
75         //
76         // ACCESSORS
77         //
78
79         /**
80          * Sets whether the FCP interface should handle requests. If {@code active}
81          * is {@code false}, all requests are answered with an error.
82          *
83          * @param active
84          *            {@code true} to activate the FCP interface, {@code false} to
85          *            deactivate the FCP interface
86          */
87         public void setActive(boolean active) {
88                 this.active = active;
89         }
90
91         //
92         // ACTIONS
93         //
94
95         /**
96          * Handles a plugin FCP request.
97          *
98          * @param pluginReplySender
99          *            The reply sender
100          * @param parameters
101          *            The message parameters
102          * @param data
103          *            The message data (may be {@code null})
104          * @param accessType
105          *            One of {@link FredPluginFCP#ACCESS_DIRECT},
106          *            {@link FredPluginFCP#ACCESS_FCP_FULL},
107          *            {@link FredPluginFCP#ACCESS_FCP_RESTRICTED}
108          */
109         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
110                 if (!active) {
111                         try {
112                                 sendReply(pluginReplySender, null, new ErrorResponse(400, "FCP Interface deactivated"));
113                         } catch (PluginNotFoundException pnfe1) {
114                                 logger.log(Level.FINE, "Could not set error to plugin.", pnfe1);
115                         }
116                         return;
117                 }
118                 Command command = commands.get(parameters.get("Message"));
119                 try {
120                         if (command == null) {
121                                 sendReply(pluginReplySender, null, new ErrorResponse("Unrecognized Message: " + parameters.get("Message")));
122                                 return;
123                         }
124                         String identifier = parameters.get("Identifier");
125                         if ((identifier == null) || (identifier.length() == 0)) {
126                                 sendReply(pluginReplySender, null, new ErrorResponse("Missing Identifier."));
127                                 return;
128                         }
129                         try {
130                                 Response response = command.execute(parameters, data, AccessType.values()[accessType]);
131                                 sendReply(pluginReplySender, identifier, response);
132                         } catch (FcpException fe1) {
133                                 sendReply(pluginReplySender, identifier, new ErrorResponse("Error executing command: " + fe1.getMessage()));
134                         }
135                 } catch (PluginNotFoundException pnfe1) {
136                         logger.log(Level.WARNING, "Could not find destination plugin: " + pluginReplySender);
137                 }
138         }
139
140         //
141         // PRIVATE METHODS
142         //
143
144         /**
145          * Sends the given response to the given plugin.
146          *
147          * @param pluginReplySender
148          *            The reply sender
149          * @param identifier
150          *            The identifier (may be {@code null})
151          * @param response
152          *            The response to send
153          * @throws PluginNotFoundException
154          *             if the plugin can not be found
155          */
156         private void sendReply(PluginReplySender pluginReplySender, String identifier, Response response) throws PluginNotFoundException {
157                 SimpleFieldSet replyParameters = response.getReplyParameters();
158                 if (identifier != null) {
159                         replyParameters.putOverwrite("Identifier", identifier);
160                 }
161                 if (response.hasData()) {
162                         pluginReplySender.send(replyParameters, response.getData());
163                 } else if (response.hasBucket()) {
164                         pluginReplySender.send(replyParameters, response.getBucket());
165                 } else {
166                         pluginReplySender.send(replyParameters);
167                 }
168         }
169
170 }