Add method to return an int from the parameters.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / 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.freenet.fcp;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import net.pterodactylus.sone.core.Core;
25 import net.pterodactylus.sone.freenet.fcp.Command.AccessType;
26 import net.pterodactylus.sone.freenet.fcp.Command.Reply;
27 import freenet.pluginmanager.FredPluginFCP;
28 import freenet.pluginmanager.PluginNotFoundException;
29 import freenet.pluginmanager.PluginReplySender;
30 import freenet.support.SimpleFieldSet;
31 import freenet.support.api.Bucket;
32
33 /**
34  * Implementation of an FCP interface for other clients or plugins to
35  * communicate with Sone.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class FcpInterface {
40
41         /** All available FCP commands. */
42         private final Map<String, Command> commands = Collections.synchronizedMap(new HashMap<String, Command>());
43
44         /**
45          * Creates a new FCP interface.
46          *
47          * @param core
48          *            The core
49          */
50         public FcpInterface(Core core) {
51                 commands.put("Version", new VersionCommand());
52                 commands.put("GetPostFeed", new GetPostFeedCommand(core));
53         }
54
55         /**
56          * Handles a plugin FCP request.
57          *
58          * @param pluginReplySender
59          *            The reply sender
60          * @param parameters
61          *            The message parameters
62          * @param data
63          *            The message data (may be {@code null})
64          * @param accessType
65          *            One of {@link FredPluginFCP#ACCESS_DIRECT},
66          *            {@link FredPluginFCP#ACCESS_FCP_FULL},
67          *            {@link FredPluginFCP#ACCESS_FCP_RESTRICTED}
68          */
69         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
70                 Command command = commands.get(parameters.get("Message"));
71                 if (command == null) {
72                         /* TODO - return error? */
73                         return;
74                 }
75                 String identifier = parameters.get("Identifier");
76                 if ((identifier == null) || (identifier.length() == 0)) {
77                         /* TODO - return error? */
78                         return;
79                 }
80                 try {
81                         Reply reply = command.execute(parameters, data, AccessType.values()[accessType]);
82                         SimpleFieldSet replyParameters = reply.getReplyParameters();
83                         replyParameters.putOverwrite("Identifier", identifier);
84                         if (reply.hasData()) {
85                                 pluginReplySender.send(replyParameters, reply.getData());
86                         } else if (reply.hasBucket()) {
87                                 pluginReplySender.send(replyParameters, reply.getBucket());
88                         } else {
89                                 pluginReplySender.send(replyParameters);
90                         }
91                 } catch (FcpException fe1) {
92                         /* TODO - log, report */
93                 } catch (PluginNotFoundException pnfe1) {
94                         /* TODO - log */
95                 }
96         }
97
98 }