Enhance JSON page to optionally require a form password.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / JsonPage.java
1 /*
2  * Sone - JsonPage.java - Copyright © 2010 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.web.ajax;
19
20 import net.pterodactylus.sone.web.WebInterface;
21 import net.pterodactylus.sone.web.page.Page;
22 import net.pterodactylus.util.json.JsonObject;
23 import net.pterodactylus.util.json.JsonUtils;
24
25 /**
26  * A JSON page is a specialized {@link Page} that will always return a JSON
27  * object to the browser, e.g. for use with AJAX or other scripting frameworks.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public abstract class JsonPage implements Page {
32
33         /** The path of the page. */
34         private final String path;
35
36         /** The Sone web interface. */
37         protected final WebInterface webInterface;
38
39         /**
40          * Creates a new JSON page at the given path.
41          *
42          * @param path
43          *            The path of the page
44          * @param webInterface
45          *            The Sone web interface
46          */
47         public JsonPage(String path, WebInterface webInterface) {
48                 this.path = path;
49                 this.webInterface = webInterface;
50         }
51
52         //
53         // METHODS FOR SUBCLASSES TO OVERRIDE
54         //
55
56         /**
57          * This method is called to create the JSON object that is returned back to
58          * the browser.
59          *
60          * @param request
61          *            The request to handle
62          * @return The created JSON object
63          */
64         protected abstract JsonObject createJsonObject(Request request);
65
66         /**
67          * Returns whether this command needs the form password for authentication
68          * and to prevent abuse.
69          *
70          * @return {@code true} if the form password (given as “formPassword”) is
71          *         required, {@code false} otherwise
72          */
73         protected boolean needsFormPassword() {
74                 return true;
75         }
76
77         //
78         // PAGE METHODS
79         //
80
81         /**
82          * {@inheritDoc}
83          */
84         @Override
85         public String getPath() {
86                 return path;
87         }
88
89         /**
90          * {@inheritDoc}
91          */
92         @Override
93         public Response handleRequest(Request request) {
94                 if (needsFormPassword()) {
95                         String formPassword = request.getHttpRequest().getParam("formPassword");
96                         if (!webInterface.formPassword().equals(formPassword)) {
97                                 return new Response(401, "Not authorized", "application/json", JsonUtils.format(new JsonObject().put("success", false)));
98                         }
99                 }
100                 JsonObject jsonObject = createJsonObject(request);
101                 return new Response(200, "OK", "application/json", JsonUtils.format(jsonObject));
102         }
103
104 }