Move creation of error JSON object to JSON page.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 12 Nov 2010 22:19:20 +0000 (23:19 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 12 Nov 2010 22:19:20 +0000 (23:19 +0100)
src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java
src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java

index c297bb3..aa8849a 100644 (file)
@@ -53,11 +53,11 @@ public class CreateReplyAjaxPage extends JsonPage {
                String text = request.getHttpRequest().getParam("text").trim();
                Sone currentSone = getCurrentSone(request.getToadletContext());
                if (currentSone == null) {
-                       return new JsonObject().put("success", false).put("error", "auth-required");
+                       return createErrorJsonObject("auth-required");
                }
                Post post = webInterface.getCore().getPost(postId);
                if ((post == null) || (post.getSone() == null)) {
-                       return new JsonObject().put("success", false).put("error", "invalid-post-id");
+                       return createErrorJsonObject("invalid-post-id");
                }
                Reply reply = webInterface.getCore().createReply(currentSone, post, text);
                return new JsonObject().put("success", true).put("reply", reply.getId());
index 4389e57..16c047d 100644 (file)
@@ -52,13 +52,13 @@ public class DeletePostAjaxPage extends JsonPage {
                Post post = webInterface.getCore().getPost(postId);
                Sone currentSone = getCurrentSone(request.getToadletContext());
                if (post == null) {
-                       return new JsonObject().put("success", false).put("error", "invalid-post-id");
+                       return createErrorJsonObject("invalid-post-id");
                }
                if (currentSone == null) {
-                       return new JsonObject().put("success", false).put("error", "auth-required");
+                       return createErrorJsonObject("auth-required");
                }
                if (!post.getSone().equals(currentSone)) {
-                       return new JsonObject().put("success", false).put("error", "not-authorized");
+                       return createErrorJsonObject("not-authorized");
                }
                currentSone.removePost(post);
                return new JsonObject().put("success", true);
index fa9de9b..b34ca5a 100644 (file)
@@ -52,13 +52,13 @@ public class DeleteReplyAjaxPage extends JsonPage {
                Reply reply = webInterface.getCore().getReply(replyId);
                Sone currentSone = getCurrentSone(request.getToadletContext());
                if (reply == null) {
-                       return new JsonObject().put("success", false).put("error", "invalid-reply-id");
+                       return createErrorJsonObject("invalid-reply-id");
                }
                if (currentSone == null) {
-                       return new JsonObject().put("success", false).put("error", "auth-required");
+                       return createErrorJsonObject("auth-required");
                }
                if (!reply.getSone().equals(currentSone)) {
-                       return new JsonObject().put("success", false).put("error", "not-authorized");
+                       return createErrorJsonObject("not-authorized");
                }
                webInterface.getCore().deleteReply(reply);
                return new JsonObject().put("success", true);
index 6d98b43..e90fa5f 100644 (file)
@@ -45,11 +45,11 @@ public class FollowSoneAjaxPage extends JsonPage {
        protected JsonObject createJsonObject(Request request) {
                String soneId = request.getHttpRequest().getParam("sone");
                if (!webInterface.getCore().hasSone(soneId)) {
-                       return new JsonObject().put("success", false).put("error", "invalid-sone-id");
+                       return createErrorJsonObject("invalid-sone-id");
                }
                Sone currentSone = getCurrentSone(request.getToadletContext());
                if (currentSone == null) {
-                       return new JsonObject().put("success", false).put("error", "auth-required");
+                       return createErrorJsonObject("auth-required");
                }
                currentSone.addFriend(soneId);
                webInterface.getCore().saveSone(currentSone);
index d27d82f..7b55faa 100644 (file)
@@ -59,7 +59,7 @@ public class GetLikesAjaxPage extends JsonPage {
                String type = request.getHttpRequest().getParam("type", null);
                String id = request.getHttpRequest().getParam(type, null);
                if ((id == null) || (id.length() == 0)) {
-                       return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id");
+                       return createErrorJsonObject("invalid-" + type + "-id");
                }
                if ("post".equals(type)) {
                        Post post = webInterface.getCore().getPost(id);
@@ -70,7 +70,7 @@ public class GetLikesAjaxPage extends JsonPage {
                        Set<Sone> sones = webInterface.getCore().getLikes(reply);
                        return new JsonObject().put("success", true).put("likes", sones.size()).put("sones", getSones(sones));
                }
-               return new JsonObject().put("success", false).put("error", "invalid-type");
+               return createErrorJsonObject("invalid-type");
        }
 
        /**
index e8cd3d5..27fa081 100644 (file)
@@ -146,6 +146,21 @@ public abstract class JsonPage implements Page {
        }
 
        //
+       // PROTECTED METHODS
+       //
+
+       /**
+        * Creates an error reply.
+        *
+        * @param error
+        *            The error that has occured
+        * @return The JSON object, signalling failure and the error code
+        */
+       protected JsonObject createErrorJsonObject(String error) {
+               return new JsonObject().put("success", false).put("error", error);
+       }
+
+       //
        // PAGE METHODS
        //
 
index 23a5aec..258eb7f 100644 (file)
@@ -47,18 +47,18 @@ public class LikeAjaxPage extends JsonPage {
                String type = request.getHttpRequest().getParam("type", null);
                String id = request.getHttpRequest().getParam(type, null);
                if ((id == null) || (id.length() == 0)) {
-                       return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id");
+                       return createErrorJsonObject("invalid-" + type + "-id");
                }
                Sone currentSone = getCurrentSone(request.getToadletContext());
                if (currentSone == null) {
-                       return new JsonObject().put("success", false).put("error", "auth-required");
+                       return createErrorJsonObject("auth-required");
                }
                if ("post".equals(type)) {
                        currentSone.addLikedPostId(id);
                } else if ("reply".equals(type)) {
                        currentSone.addLikedReplyId(id);
                } else {
-                       return new JsonObject().put("success", false).put("error", "invalid-type");
+                       return createErrorJsonObject("invalid-type");
                }
                return new JsonObject().put("success", true);
        }
index 6092643..7e620ef 100644 (file)
@@ -45,11 +45,11 @@ public class UnfollowSoneAjaxPage extends JsonPage {
        protected JsonObject createJsonObject(Request request) {
                String soneId = request.getHttpRequest().getParam("sone");
                if (!webInterface.getCore().hasSone(soneId)) {
-                       return new JsonObject().put("success", false).put("error", "invalid-sone-id");
+                       return createErrorJsonObject("invalid-sone-id");
                }
                Sone currentSone = getCurrentSone(request.getToadletContext());
                if (currentSone == null) {
-                       return new JsonObject().put("success", false).put("error", "auth-required");
+                       return createErrorJsonObject("auth-required");
                }
                currentSone.removeFriend(soneId);
                webInterface.getCore().saveSone(currentSone);
index c062274..ecacf98 100644 (file)
@@ -47,18 +47,18 @@ public class UnlikeAjaxPage extends JsonPage {
                String type = request.getHttpRequest().getParam("type", null);
                String id = request.getHttpRequest().getParam(type, null);
                if ((id == null) || (id.length() == 0)) {
-                       return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id");
+                       return createErrorJsonObject("invalid-" + type + "-id");
                }
                Sone currentSone = getCurrentSone(request.getToadletContext());
                if (currentSone == null) {
-                       return new JsonObject().put("success", false).put("error", "auth-required");
+                       return createErrorJsonObject("auth-required");
                }
                if ("post".equals(type)) {
                        currentSone.removeLikedPostId(id);
                } else if ("reply".equals(type)) {
                        currentSone.removeLikedReplyId(id);
                } else {
-                       return new JsonObject().put("success", false).put("error", "invalid-type");
+                       return createErrorJsonObject("invalid-type");
                }
                return new JsonObject().put("success", true);
        }