2 * Sone - JsonPage.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.ajax;
20 import java.util.UUID;
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.sone.web.WebInterface;
24 import net.pterodactylus.sone.web.page.Page;
25 import net.pterodactylus.util.json.JsonObject;
26 import net.pterodactylus.util.json.JsonUtils;
27 import freenet.clients.http.SessionManager.Session;
28 import freenet.clients.http.ToadletContext;
31 * A JSON page is a specialized {@link Page} that will always return a JSON
32 * object to the browser, e.g. for use with AJAX or other scripting frameworks.
34 * @author <a href="mailto:bombe@pterodactylus.net">David âBombeâ Roden</a>
36 public abstract class JsonPage implements Page {
38 /** The path of the page. */
39 private final String path;
41 /** The Sone web interface. */
42 protected final WebInterface webInterface;
45 * Creates a new JSON page at the given path.
48 * The path of the page
50 * The Sone web interface
52 public JsonPage(String path, WebInterface webInterface) {
54 this.webInterface = webInterface;
62 * Returns the current session, creating a new session if there is no
65 * @param toadletContenxt
67 * @return The current session, or {@code null} if there is no current
70 protected Session getCurrentSession(ToadletContext toadletContenxt) {
71 return getCurrentSession(toadletContenxt, true);
75 * Returns the current session, creating a new session if there is no
76 * current session and {@code create} is {@code true}.
78 * @param toadletContenxt
81 * {@code true} to create a new session if there is no current
82 * session, {@code false} otherwise
83 * @return The current session, or {@code null} if there is no current
86 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
87 Session session = webInterface.getSessionManager().useSession(toadletContenxt);
88 if (create && (session == null)) {
89 session = webInterface.getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
95 * Returns the currently logged in Sone.
97 * @param toadletContext
99 * @return The currently logged in Sone, or {@code null} if no Sone is
100 * currently logged in
102 protected Sone getCurrentSone(ToadletContext toadletContext) {
103 Session session = getCurrentSession(toadletContext);
104 if (session == null) {
107 String soneId = (String) session.getAttribute("Sone.CurrentSone");
108 if (soneId == null) {
111 return webInterface.getCore().getLocalSone(soneId, false);
115 // METHODS FOR SUBCLASSES TO OVERRIDE
119 * This method is called to create the JSON object that is returned back to
123 * The request to handle
124 * @return The created JSON object
126 protected abstract JsonObject createJsonObject(Request request);
129 * Returns whether this command needs the form password for authentication
130 * and to prevent abuse.
132 * @return {@code true} if the form password (given as âformPasswordâ) is
133 * required, {@code false} otherwise
135 protected boolean needsFormPassword() {
144 * Creates a success reply.
146 * @return A reply signaling success
148 protected JsonObject createSuccessJsonObject() {
149 return new JsonObject().put("success", true);
153 * Creates an error reply.
156 * The error that has occured
157 * @return The JSON object, signalling failure and the error code
159 protected JsonObject createErrorJsonObject(String error) {
160 return new JsonObject().put("success", false).put("error", error);
171 public String getPath() {
179 public Response handleRequest(Request request) {
180 if (needsFormPassword()) {
181 String formPassword = request.getHttpRequest().getParam("formPassword");
182 if (!webInterface.getFormPassword().equals(formPassword)) {
183 return new Response(401, "Not authorized", "application/json", JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
186 JsonObject jsonObject = createJsonObject(request);
187 return new Response(200, "OK", "application/json", JsonUtils.format(jsonObject));