2 * Sone - Command.java - Copyright © 2011–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.freenet.fcp;
20 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
21 import freenet.support.SimpleFieldSet;
22 import freenet.support.api.Bucket;
25 * Implementation of an FCP interface for other clients or plugins to
26 * communicate with Sone.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 public interface Command {
33 * Executes the command, returning a reply that will be sent back to the
37 * The parameters of the comand
39 * The data of the command (may be {@code null})
42 * @return A reply to send back to the plugin
43 * @throws FcpException
44 * if an error processing the parameters occurs
46 public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException;
49 * The access type of the request.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public static enum AccessType {
55 /** Access from another plugin. */
58 /** Access via restricted FCP. */
61 /** Access via FCP with full access. */
67 * Interface for command replies.
69 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
71 public static class Response {
73 /** The message name of the reponse. */
74 private final String messageName;
76 /** The reply parameters. */
77 private final SimpleFieldSet replyParameters;
79 /** The reply data, may be {@code null}. */
80 private final byte[] data;
82 /** The data bucket, may be {@code null}. */
83 private final Bucket bucket;
86 * Creates a new reply with the given parameters.
90 * @param replyParameters
91 * The reply parameters
93 public Response(String messageName, SimpleFieldSet replyParameters) {
94 this(messageName, replyParameters, null, null);
98 * Creates a new reply with the given parameters.
102 * @param replyParameters
103 * The reply parameters
105 * The data of the reply (may be {@code null})
107 public Response(String messageName, SimpleFieldSet replyParameters, byte[] data) {
108 this(messageName, replyParameters, data, null);
112 * Creates a new reply with the given parameters.
116 * @param replyParameters
117 * The reply parameters
119 * The bucket of the reply (may be {@code null})
121 public Response(String messageName, SimpleFieldSet replyParameters, Bucket bucket) {
122 this(messageName, replyParameters, null, bucket);
126 * Creates a new reply with the given parameters.
130 * @param replyParameters
131 * The reply parameters
133 * The data of the reply (may be {@code null})
135 * The bucket of the reply (may be {@code null})
137 private Response(String messageName, SimpleFieldSet replyParameters, byte[] data, Bucket bucket) {
138 this.messageName = messageName;
139 this.replyParameters = replyParameters;
141 this.bucket = bucket;
145 * Returns the reply parameters.
147 * @return The reply parameters
149 public SimpleFieldSet getReplyParameters() {
150 return new SimpleFieldSetBuilder(replyParameters).put("Message", messageName).get();
154 * Returns whether the reply has reply data.
157 * @return {@code true} if this reply has data, {@code false} otherwise
159 public boolean hasData() {
164 * Returns the data of the reply.
166 * @return The data of the reply
168 public byte[] getData() {
173 * Returns whether the reply has a data bucket.
176 * @return {@code true} if the reply has a data bucket, {@code false}
179 public boolean hasBucket() {
180 return bucket != null;
184 * Returns the data bucket of the reply.
186 * @return The data bucket of the reply
188 public Bucket getBucket() {
195 * Response implementation that can return an error message and an optional
198 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
200 public class ErrorResponse extends Response {
203 * Creates a new error response with the given message.
208 public ErrorResponse(String message) {
209 super("Error", new SimpleFieldSetBuilder().put("ErrorMessage", message).get());
213 * Creates a new error response with the given code and message.
220 public ErrorResponse(int code, String message) {
221 super("Error", new SimpleFieldSetBuilder().put("ErrorMessage", message).put("ErrorCode", code).get());