Add method to create a “success” JSON object.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / JsonPage.java
index 918cb97..b9bdc77 100644 (file)
@@ -85,9 +85,9 @@ public abstract class JsonPage implements Page {
         */
        protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
                try {
-                       Session session = webInterface.sessionManager().useSession(toadletContenxt);
+                       Session session = webInterface.getSessionManager().useSession(toadletContenxt);
                        if (create && (session == null)) {
-                               session = webInterface.sessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
+                               session = webInterface.getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
                        }
                        return session;
                } catch (freenet.clients.http.RedirectException re1) {
@@ -112,12 +112,7 @@ public abstract class JsonPage implements Page {
                if (soneId == null) {
                        return null;
                }
-               for (Sone sone : webInterface.core().getSones()) {
-                       if (sone.getId().equals(soneId)) {
-                               return sone;
-                       }
-               }
-               return null;
+               return webInterface.getCore().getLocalSone(soneId, false);
        }
 
        //
@@ -146,6 +141,30 @@ public abstract class JsonPage implements Page {
        }
 
        //
+       // PROTECTED METHODS
+       //
+
+       /**
+        * Creates a success reply.
+        *
+        * @return A reply signaling success
+        */
+       protected JsonObject createSuccessJsonObject() {
+               return new JsonObject().put("success", true);
+       }
+
+       /**
+        * 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
        //
 
@@ -164,7 +183,7 @@ public abstract class JsonPage implements Page {
        public Response handleRequest(Request request) {
                if (needsFormPassword()) {
                        String formPassword = request.getHttpRequest().getParam("formPassword");
-                       if (!webInterface.formPassword().equals(formPassword)) {
+                       if (!webInterface.getFormPassword().equals(formPassword)) {
                                return new Response(401, "Not authorized", "application/json", JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
                        }
                }