2 * Sone - Command.java - Copyright © 2011 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 reply parameters. */
74 private final SimpleFieldSet replyParameters;
76 /** The reply data, may be {@code null}. */
77 private final byte[] data;
79 /** The data bucket, may be {@code null}. */
80 private final Bucket bucket;
83 * Creates a new reply with the given parameters.
85 * @param replyParameters
86 * The reply parameters
88 public Response(SimpleFieldSet replyParameters) {
89 this(replyParameters, null, null);
93 * Creates a new reply with the given parameters.
95 * @param replyParameters
96 * The reply parameters
98 * The data of the reply (may be {@code null})
100 public Response(SimpleFieldSet replyParameters, byte[] data) {
101 this(replyParameters, data, null);
105 * Creates a new reply with the given parameters.
107 * @param replyParameters
108 * The reply parameters
110 * The bucket of the reply (may be {@code null})
112 public Response(SimpleFieldSet replyParameters, Bucket bucket) {
113 this(replyParameters, null, bucket);
117 * Creates a new reply with the given parameters.
119 * @param replyParameters
120 * The reply parameters
122 * The data of the reply (may be {@code null})
124 * The bucket of the reply (may be {@code null})
126 private Response(SimpleFieldSet replyParameters, byte[] data, Bucket bucket) {
127 this.replyParameters = replyParameters;
129 this.bucket = bucket;
133 * Returns the reply parameters.
135 * @return The reply parameters
137 public SimpleFieldSet getReplyParameters() {
138 return replyParameters;
142 * Returns whether the reply has reply data.
145 * @return {@code true} if this reply has data, {@code false} otherwise
147 public boolean hasData() {
152 * Returns the data of the reply.
154 * @return The data of the reply
156 public byte[] getData() {
161 * Returns whether the reply has a data bucket.
164 * @return {@code true} if the reply has a data bucket, {@code false}
167 public boolean hasBucket() {
168 return bucket != null;
172 * Returns the data bucket of the reply.
174 * @return The data bucket of the reply
176 public Bucket getBucket() {
183 * Response implementation that can return an error message and an optional
186 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
188 public class ErrorResponse extends Response {
191 * Creates a new error response with the given message.
196 public ErrorResponse(String message) {
197 super(new SimpleFieldSetBuilder().put("ErrorMessage", message).get());
201 * Creates a new error response with the given code and message.
208 public ErrorResponse(int code, String message) {
209 super(new SimpleFieldSetBuilder().put("ErrorMessage", message).put("ErrorCode", code).get());