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