Organize imports.
[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 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         protected boolean needsFormPassword() {
143                 return true;
144         }
145
146         /**
147          * Returns whether this page requires the user to be logged in.
148          *
149          * @return {@code true} if the user needs to be logged in to use this page,
150          *         {@code false} otherwise
151          */
152         protected boolean requiresLogin() {
153                 return true;
154         }
155
156         //
157         // PROTECTED METHODS
158         //
159
160         /**
161          * Creates a success reply.
162          *
163          * @return A reply signaling success
164          */
165         protected JsonObject createSuccessJsonObject() {
166                 return new JsonObject().put("success", true);
167         }
168
169         /**
170          * Creates an error reply.
171          *
172          * @param error
173          *            The error that has occured
174          * @return The JSON object, signalling failure and the error code
175          */
176         protected JsonObject createErrorJsonObject(String error) {
177                 return new JsonObject().put("success", false).put("error", error);
178         }
179
180         //
181         // PAGE METHODS
182         //
183
184         /**
185          * {@inheritDoc}
186          */
187         @Override
188         public String getPath() {
189                 return path;
190         }
191
192         /**
193          * {@inheritDoc}
194          */
195         @Override
196         public boolean isPrefixPage() {
197                 return false;
198         }
199
200         /**
201          * {@inheritDoc}
202          */
203         @Override
204         public Response handleRequest(FreenetRequest request, Response response) throws IOException {
205                 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !request.getToadletContext().isAllowedFullAccess()) {
206                         return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
207                 }
208                 if (needsFormPassword()) {
209                         String formPassword = request.getHttpRequest().getParam("formPassword");
210                         if (!webInterface.getFormPassword().equals(formPassword)) {
211                                 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
212                         }
213                 }
214                 if (requiresLogin()) {
215                         if (getCurrentSone(request.getToadletContext(), false) == null) {
216                                 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
217                         }
218                 }
219                 JsonObject jsonObject = createJsonObject(request);
220                 return response.setStatusCode(200).setStatusText("OK").setContentType("application/json").write(JsonUtils.format(jsonObject));
221         }
222
223         /**
224          * {@inheritDoc}
225          */
226         @Override
227         public boolean isLinkExcepted(URI link) {
228                 return false;
229         }
230
231 }