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 freenet.support.SimpleFieldSet;
21 import freenet.support.api.Bucket;
24 * Implementation of an FCP interface for other clients or plugins to
25 * communicate with Sone.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29 public interface Command {
32 * Executes the command, returning a reply that will be sent back to the
36 * The parameters of the comand
38 * The data of the command (may be {@code null})
41 * @return A reply to send back to the plugin
42 * @throws FcpException
43 * if an error processing the parameters occurs
45 public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException;
48 * The access type of the request.
50 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52 public static enum AccessType {
54 /** Access from another plugin. */
57 /** Access via restricted FCP. */
60 /** Access via FCP with full access. */
66 * Interface for command replies.
68 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
70 public static class Response {
72 /** The reply parameters. */
73 private final SimpleFieldSet replyParameters;
75 /** The reply data, may be {@code null}. */
76 private final byte[] data;
78 /** The data bucket, may be {@code null}. */
79 private final Bucket bucket;
82 * Creates a new reply with the given parameters.
84 * @param replyParameters
85 * The reply parameters
87 public Response(SimpleFieldSet replyParameters) {
88 this(replyParameters, null, null);
92 * Creates a new reply with the given parameters.
94 * @param replyParameters
95 * The reply parameters
97 * The data of the reply (may be {@code null})
99 public Response(SimpleFieldSet replyParameters, byte[] data) {
100 this(replyParameters, data, null);
104 * Creates a new reply with the given parameters.
106 * @param replyParameters
107 * The reply parameters
109 * The bucket of the reply (may be {@code null})
111 public Response(SimpleFieldSet replyParameters, Bucket bucket) {
112 this(replyParameters, null, bucket);
116 * Creates a new reply with the given parameters.
118 * @param replyParameters
119 * The reply parameters
121 * The data of the reply (may be {@code null})
123 * The bucket of the reply (may be {@code null})
125 private Response(SimpleFieldSet replyParameters, byte[] data, Bucket bucket) {
126 this.replyParameters = replyParameters;
128 this.bucket = bucket;
132 * Returns the reply parameters.
134 * @return The reply parameters
136 public SimpleFieldSet getReplyParameters() {
137 return replyParameters;
141 * Returns whether the reply has reply data.
144 * @return {@code true} if this reply has data, {@code false} otherwise
146 public boolean hasData() {
151 * Returns the data of the reply.
153 * @return The data of the reply
155 public byte[] getData() {
160 * Returns whether the reply has a data bucket.
163 * @return {@code true} if the reply has a data bucket, {@code false}
166 public boolean hasBucket() {
167 return bucket != null;
171 * Returns the data bucket of the reply.
173 * @return The data bucket of the reply
175 public Bucket getBucket() {