2a083eefc6f05afcbab2e6ea5fc2fd533900c11c
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / fcp / Command.java
1 /*
2  * Sone - Command.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 net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
21 import freenet.support.SimpleFieldSet;
22 import freenet.support.api.Bucket;
23
24 /**
25  * Implementation of an FCP interface for other clients or plugins to
26  * communicate with Sone.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public interface Command {
31
32         /**
33          * Executes the command, returning a reply that will be sent back to the
34          * requesting plugin.
35          *
36          * @param parameters
37          *            The parameters of the comand
38          * @param data
39          *            The data of the command (may be {@code null})
40          * @param accessType
41          *            The access type
42          * @return A reply to send back to the plugin
43          * @throws FcpException
44          *             if an error processing the parameters occurs
45          */
46         public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException;
47
48         /**
49          * The access type of the request.
50          *
51          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52          */
53         public static enum AccessType {
54
55                 /** Access from another plugin. */
56                 DIRECT,
57
58                 /** Access via restricted FCP. */
59                 RESTRICTED_FCP,
60
61                 /** Access via FCP with full access. */
62                 FULL_FCP,
63
64         }
65
66         /**
67          * Interface for command replies.
68          *
69          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
70          */
71         public static class Response {
72
73                 /** The reply parameters. */
74                 private final SimpleFieldSet replyParameters;
75
76                 /** The reply data, may be {@code null}. */
77                 private final byte[] data;
78
79                 /** The data bucket, may be {@code null}. */
80                 private final Bucket bucket;
81
82                 /**
83                  * Creates a new reply with the given parameters.
84                  *
85                  * @param replyParameters
86                  *            The reply parameters
87                  */
88                 public Response(SimpleFieldSet replyParameters) {
89                         this(replyParameters, null, null);
90                 }
91
92                 /**
93                  * Creates a new reply with the given parameters.
94                  *
95                  * @param replyParameters
96                  *            The reply parameters
97                  * @param data
98                  *            The data of the reply (may be {@code null})
99                  */
100                 public Response(SimpleFieldSet replyParameters, byte[] data) {
101                         this(replyParameters, data, null);
102                 }
103
104                 /**
105                  * Creates a new reply with the given parameters.
106                  *
107                  * @param replyParameters
108                  *            The reply parameters
109                  * @param bucket
110                  *            The bucket of the reply (may be {@code null})
111                  */
112                 public Response(SimpleFieldSet replyParameters, Bucket bucket) {
113                         this(replyParameters, null, bucket);
114                 }
115
116                 /**
117                  * Creates a new reply with the given parameters.
118                  *
119                  * @param replyParameters
120                  *            The reply parameters
121                  * @param data
122                  *            The data of the reply (may be {@code null})
123                  * @param bucket
124                  *            The bucket of the reply (may be {@code null})
125                  */
126                 private Response(SimpleFieldSet replyParameters, byte[] data, Bucket bucket) {
127                         this.replyParameters = replyParameters;
128                         this.data = data;
129                         this.bucket = bucket;
130                 }
131
132                 /**
133                  * Returns the reply parameters.
134                  *
135                  * @return The reply parameters
136                  */
137                 public SimpleFieldSet getReplyParameters() {
138                         return replyParameters;
139                 }
140
141                 /**
142                  * Returns whether the reply has reply data.
143                  *
144                  * @see #getData()
145                  * @return {@code true} if this reply has data, {@code false} otherwise
146                  */
147                 public boolean hasData() {
148                         return data != null;
149                 }
150
151                 /**
152                  * Returns the data of the reply.
153                  *
154                  * @return The data of the reply
155                  */
156                 public byte[] getData() {
157                         return data;
158                 }
159
160                 /**
161                  * Returns whether the reply has a data bucket.
162                  *
163                  * @see #getBucket()
164                  * @return {@code true} if the reply has a data bucket, {@code false}
165                  *         otherwise
166                  */
167                 public boolean hasBucket() {
168                         return bucket != null;
169                 }
170
171                 /**
172                  * Returns the data bucket of the reply.
173                  *
174                  * @return The data bucket of the reply
175                  */
176                 public Bucket getBucket() {
177                         return bucket;
178                 }
179
180         }
181
182         /**
183          * Response implementation that can return an error message and an optional
184          * error code.
185          *
186          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
187          */
188         public class ErrorResponse extends Response {
189
190                 /**
191                  * Creates a new error response with the given message.
192                  *
193                  * @param message
194                  *            The error message
195                  */
196                 public ErrorResponse(String message) {
197                         super(new SimpleFieldSetBuilder().put("ErrorMessage", message).get());
198                 }
199
200                 /**
201                  * Creates a new error response with the given code and message.
202                  *
203                  * @param code
204                  *            The error code
205                  * @param message
206                  *            The error message
207                  */
208                 public ErrorResponse(int code, String message) {
209                         super(new SimpleFieldSetBuilder().put("ErrorMessage", message).put("ErrorCode", code).get());
210                 }
211
212         }
213
214 }