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