17f8370d6d32c655e47a9a8ae257dd74dcb1fdf6
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / fcp / Command.java
1 /*
2  * Sone - Command.java - Copyright © 2011–2016 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 net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
21 import freenet.support.SimpleFieldSet;
22
23 /**
24  * Implementation of an FCP interface for other clients or plugins to
25  * communicate with Sone.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public interface Command {
30
31         /**
32          * Executes the command, returning a reply that will be sent back to the
33          * requesting plugin.
34          *
35          * @param parameters
36          *            The parameters of the comand
37          * @return A reply to send back to the plugin
38          * @throws FcpException
39          *             if an error processing the parameters occurs
40          */
41         public Response execute(SimpleFieldSet parameters) throws FcpException;
42
43         /**
44          * The access type of the request.
45          *
46          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47          */
48         public static enum AccessType {
49
50                 /** Access from another plugin. */
51                 DIRECT,
52
53                 /** Access via restricted FCP. */
54                 RESTRICTED_FCP,
55
56                 /** Access via FCP with full access. */
57                 FULL_FCP,
58
59         }
60
61         /**
62          * Interface for command replies.
63          *
64          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
65          */
66         public static class Response {
67
68                 /** The message name of the reponse. */
69                 private final String messageName;
70
71                 /** The reply parameters. */
72                 private final SimpleFieldSet replyParameters;
73
74                 /**
75                  * Creates a new reply with the given parameters.
76                  *
77                  * @param messageName
78                  *            The message name
79                  * @param replyParameters
80                  *            The reply parameters
81                  */
82                 public Response(String messageName, SimpleFieldSet replyParameters) {
83                         this.messageName = messageName;
84                         this.replyParameters = replyParameters;
85                 }
86
87                 /**
88                  * Returns the reply parameters.
89                  *
90                  * @return The reply parameters
91                  */
92                 public SimpleFieldSet getReplyParameters() {
93                         return new SimpleFieldSetBuilder(replyParameters).put("Message", messageName).get();
94                 }
95
96         }
97
98         /**
99          * Response implementation that can return an error message and an optional
100          * error code.
101          *
102          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
103          */
104         public class ErrorResponse extends Response {
105
106                 /**
107                  * Creates a new error response with the given message.
108                  *
109                  * @param message
110                  *            The error message
111                  */
112                 public ErrorResponse(String message) {
113                         super("Error", new SimpleFieldSetBuilder().put("ErrorMessage", message).get());
114                 }
115
116                 /**
117                  * Creates a new error response with the given code and message.
118                  *
119                  * @param code
120                  *            The error code
121                  * @param message
122                  *            The error message
123                  */
124                 public ErrorResponse(int code, String message) {
125                         super("Error", new SimpleFieldSetBuilder().put("ErrorMessage", message).put("ErrorCode", code).get());
126                 }
127
128         }
129
130 }