Merge branch 'next' into dev/image
[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 java.io.IOException;
21
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.sone.web.WebInterface;
24 import net.pterodactylus.sone.web.page.FreenetRequest;
25 import net.pterodactylus.util.json.JsonObject;
26 import net.pterodactylus.util.json.JsonUtils;
27 import net.pterodactylus.util.web.Page;
28 import net.pterodactylus.util.web.Response;
29 import freenet.clients.http.SessionManager.Session;
30 import freenet.clients.http.ToadletContext;
31
32 /**
33  * A JSON page is a specialized {@link Page} that will always return a JSON
34  * object to the browser, e.g. for use with AJAX or other scripting frameworks.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public abstract class JsonPage implements Page<FreenetRequest> {
39
40         /** The path of the page. */
41         private final String path;
42
43         /** The Sone web interface. */
44         protected final WebInterface webInterface;
45
46         /**
47          * Creates a new JSON page at the given path.
48          *
49          * @param path
50          *            The path of the page
51          * @param webInterface
52          *            The Sone web interface
53          */
54         public JsonPage(String path, WebInterface webInterface) {
55                 this.path = path;
56                 this.webInterface = webInterface;
57         }
58
59         //
60         // ACCESSORS
61         //
62
63         /**
64          * Returns the current session, creating a new session if there is no
65          * current session.
66          *
67          * @param toadletContenxt
68          *            The toadlet context
69          * @return The current session, or {@code null} if there is no current
70          *         session
71          */
72         protected Session getCurrentSession(ToadletContext toadletContenxt) {
73                 return webInterface.getCurrentSession(toadletContenxt);
74         }
75
76         /**
77          * Returns the current session, creating a new session if there is no
78          * current session and {@code create} is {@code true}.
79          *
80          * @param toadletContenxt
81          *            The toadlet context
82          * @param create
83          *            {@code true} to create a new session if there is no current
84          *            session, {@code false} otherwise
85          * @return The current session, or {@code null} if there is no current
86          *         session
87          */
88         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
89                 return webInterface.getCurrentSession(toadletContenxt, create);
90         }
91
92         /**
93          * Returns the currently logged in Sone.
94          *
95          * @param toadletContext
96          *            The toadlet context
97          * @return The currently logged in Sone, or {@code null} if no Sone is
98          *         currently logged in
99          */
100         protected Sone getCurrentSone(ToadletContext toadletContext) {
101                 return webInterface.getCurrentSone(toadletContext);
102         }
103
104         /**
105          * Returns the currently logged in Sone.
106          *
107          * @param toadletContext
108          *            The toadlet context
109          * @param create
110          *            {@code true} to create a new session if no session exists,
111          *            {@code false} to not create a new session
112          * @return The currently logged in Sone, or {@code null} if no Sone is
113          *         currently logged in
114          */
115         protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
116                 return webInterface.getCurrentSone(toadletContext, create);
117         }
118
119         //
120         // METHODS FOR SUBCLASSES TO OVERRIDE
121         //
122
123         /**
124          * This method is called to create the JSON object that is returned back to
125          * the browser.
126          *
127          * @param request
128          *            The request to handle
129          * @return The created JSON object
130          */
131         protected abstract JsonObject createJsonObject(FreenetRequest request);
132
133         /**
134          * Returns whether this command needs the form password for authentication
135          * and to prevent abuse.
136          *
137          * @return {@code true} if the form password (given as “formPassword”) is
138          *         required, {@code false} otherwise
139          */
140         protected boolean needsFormPassword() {
141                 return true;
142         }
143
144         /**
145          * Returns whether this page requires the user to be logged in.
146          *
147          * @return {@code true} if the user needs to be logged in to use this page,
148          *         {@code false} otherwise
149          */
150         protected boolean requiresLogin() {
151                 return true;
152         }
153
154         //
155         // PROTECTED METHODS
156         //
157
158         /**
159          * Creates a success reply.
160          *
161          * @return A reply signaling success
162          */
163         protected JsonObject createSuccessJsonObject() {
164                 return new JsonObject().put("success", true);
165         }
166
167         /**
168          * Creates an error reply.
169          *
170          * @param error
171          *            The error that has occured
172          * @return The JSON object, signalling failure and the error code
173          */
174         protected JsonObject createErrorJsonObject(String error) {
175                 return new JsonObject().put("success", false).put("error", error);
176         }
177
178         //
179         // PAGE METHODS
180         //
181
182         /**
183          * {@inheritDoc}
184          */
185         @Override
186         public String getPath() {
187                 return path;
188         }
189
190         /**
191          * {@inheritDoc}
192          */
193         @Override
194         public boolean isPrefixPage() {
195                 return false;
196         }
197
198         /**
199          * {@inheritDoc}
200          */
201         @Override
202         public Response handleRequest(FreenetRequest request, Response response) throws IOException {
203                 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !request.getToadletContext().isAllowedFullAccess()) {
204                         return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
205                 }
206                 if (needsFormPassword()) {
207                         String formPassword = request.getHttpRequest().getParam("formPassword");
208                         if (!webInterface.getFormPassword().equals(formPassword)) {
209                                 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
210                         }
211                 }
212                 if (requiresLogin()) {
213                         if (getCurrentSone(request.getToadletContext(), false) == null) {
214                                 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
215                         }
216                 }
217                 JsonObject jsonObject = createJsonObject(request);
218                 return response.setStatusCode(200).setStatusText("OK").setContentType("application/json").write(JsonUtils.format(jsonObject));
219         }
220
221 }