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