Add error response.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / fcp / Command.java
index a4284aa..2a083ee 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.freenet.fcp;
 
+import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
 import freenet.support.SimpleFieldSet;
 import freenet.support.api.Bucket;
 
@@ -39,8 +40,10 @@ public interface Command {
         * @param accessType
         *            The access type
         * @return A reply to send back to the plugin
+        * @throws FcpException
+        *             if an error processing the parameters occurs
         */
-       public Reply execute(SimpleFieldSet parameters, Bucket data, AccessType accessType);
+       public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException;
 
        /**
         * The access type of the request.
@@ -65,7 +68,7 @@ public interface Command {
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
-       public static class Reply {
+       public static class Response {
 
                /** The reply parameters. */
                private final SimpleFieldSet replyParameters;
@@ -82,7 +85,7 @@ public interface Command {
                 * @param replyParameters
                 *            The reply parameters
                 */
-               public Reply(SimpleFieldSet replyParameters) {
+               public Response(SimpleFieldSet replyParameters) {
                        this(replyParameters, null, null);
                }
 
@@ -94,7 +97,7 @@ public interface Command {
                 * @param data
                 *            The data of the reply (may be {@code null})
                 */
-               public Reply(SimpleFieldSet replyParameters, byte[] data) {
+               public Response(SimpleFieldSet replyParameters, byte[] data) {
                        this(replyParameters, data, null);
                }
 
@@ -106,7 +109,7 @@ public interface Command {
                 * @param bucket
                 *            The bucket of the reply (may be {@code null})
                 */
-               public Reply(SimpleFieldSet replyParameters, Bucket bucket) {
+               public Response(SimpleFieldSet replyParameters, Bucket bucket) {
                        this(replyParameters, null, bucket);
                }
 
@@ -120,7 +123,7 @@ public interface Command {
                 * @param bucket
                 *            The bucket of the reply (may be {@code null})
                 */
-               private Reply(SimpleFieldSet replyParameters, byte[] data, Bucket bucket) {
+               private Response(SimpleFieldSet replyParameters, byte[] data, Bucket bucket) {
                        this.replyParameters = replyParameters;
                        this.data = data;
                        this.bucket = bucket;
@@ -176,4 +179,36 @@ public interface Command {
 
        }
 
+       /**
+        * Response implementation that can return an error message and an optional
+        * error code.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public class ErrorResponse extends Response {
+
+               /**
+                * Creates a new error response with the given message.
+                *
+                * @param message
+                *            The error message
+                */
+               public ErrorResponse(String message) {
+                       super(new SimpleFieldSetBuilder().put("ErrorMessage", message).get());
+               }
+
+               /**
+                * Creates a new error response with the given code and message.
+                *
+                * @param code
+                *            The error code
+                * @param message
+                *            The error message
+                */
+               public ErrorResponse(int code, String message) {
+                       super(new SimpleFieldSetBuilder().put("ErrorMessage", message).put("ErrorCode", code).get());
+               }
+
+       }
+
 }