From: David ‘Bombe’ Roden Date: Fri, 12 Nov 2010 22:19:20 +0000 (+0100) Subject: Move creation of error JSON object to JSON page. X-Git-Tag: 0.2.3~7 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=dc3d0d58fd47e520c723aa7d258c2f7ae09e1ee7;p=Sone.git Move creation of error JSON object to JSON page. --- diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java index c297bb3..aa8849a 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java @@ -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()); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java index 4389e57..16c047d 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java @@ -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); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java index fa9de9b..b34ca5a 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java @@ -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); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java index 6d98b43..e90fa5f 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java @@ -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); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java index d27d82f..7b55faa 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java @@ -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 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"); } /** diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java index e8cd3d5..27fa081 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java @@ -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 // diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java index 23a5aec..258eb7f 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java @@ -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); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java index 6092643..7e620ef 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java @@ -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); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java index c062274..ecacf98 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java @@ -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); }