Send back identifier, if available.
[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         /** All available FCP commands. */
51         private final Map<String, Command> commands = Collections.synchronizedMap(new HashMap<String, Command>());
52
53         /**
54          * Creates a new FCP interface.
55          *
56          * @param core
57          *            The core
58          */
59         public FcpInterface(Core core) {
60                 commands.put("Version", new VersionCommand());
61                 commands.put("GetLocalSones", new GetLocalSonesCommand(core));
62                 commands.put("GetPosts", new GetPostsCommand(core));
63                 commands.put("GetPostFeed", new GetPostFeedCommand(core));
64         }
65
66         /**
67          * Handles a plugin FCP request.
68          *
69          * @param pluginReplySender
70          *            The reply sender
71          * @param parameters
72          *            The message parameters
73          * @param data
74          *            The message data (may be {@code null})
75          * @param accessType
76          *            One of {@link FredPluginFCP#ACCESS_DIRECT},
77          *            {@link FredPluginFCP#ACCESS_FCP_FULL},
78          *            {@link FredPluginFCP#ACCESS_FCP_RESTRICTED}
79          */
80         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
81                 Command command = commands.get(parameters.get("Message"));
82                 try {
83                         if (command == null) {
84                                 sendReply(pluginReplySender, null, new ErrorResponse("Unrecognized Message: " + parameters.get("Message")));
85                                 return;
86                         }
87                         String identifier = parameters.get("Identifier");
88                         if ((identifier == null) || (identifier.length() == 0)) {
89                                 sendReply(pluginReplySender, null, new ErrorResponse("Missing Identifier."));
90                                 return;
91                         }
92                         try {
93                                 Response response = command.execute(parameters, data, AccessType.values()[accessType]);
94                                 sendReply(pluginReplySender, identifier, response);
95                         } catch (FcpException fe1) {
96                                 sendReply(pluginReplySender, identifier, new ErrorResponse("Error executing command: " + fe1.getMessage()));
97                         }
98                 } catch (PluginNotFoundException pnfe1) {
99                         logger.log(Level.WARNING, "Could not find destination plugin: " + pluginReplySender);
100                 }
101         }
102
103         //
104         // PRIVATE METHODS
105         //
106
107         /**
108          * Sends the given response to the given plugin.
109          *
110          * @param pluginReplySender
111          *            The reply sender
112          * @param identifier
113          *            The identifier (may be {@code null})
114          * @param response
115          *            The response to send
116          * @throws PluginNotFoundException
117          *             if the plugin can not be found
118          */
119         private void sendReply(PluginReplySender pluginReplySender, String identifier, Response response) throws PluginNotFoundException {
120                 SimpleFieldSet replyParameters = response.getReplyParameters();
121                 if (identifier != null) {
122                         replyParameters.putOverwrite("Identifier", identifier);
123                 }
124                 if (response.hasData()) {
125                         pluginReplySender.send(replyParameters, response.getData());
126                 } else if (response.hasBucket()) {
127                         pluginReplySender.send(replyParameters, response.getBucket());
128                 } else {
129                         pluginReplySender.send(replyParameters);
130                 }
131         }
132
133 }