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 net.pterodactylus.sone.web.page.Page;
21 import net.pterodactylus.util.json.JsonObject;
22 import net.pterodactylus.util.json.JsonUtils;
25 * A JSON page is a specialized {@link Page} that will always return a JSON
26 * object to the browser, e.g. for use with AJAX or other scripting frameworks.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 public abstract class JsonPage implements Page {
32 /** The path of the page. */
33 private final String path;
36 * Creates a new JSON page at the given path.
39 * The path of the page
41 public JsonPage(String path) {
46 // METHODS FOR SUBCLASSES TO OVERRIDE
50 * This method is called to create the JSON object that is returned back to
54 * The request to handle
55 * @return The created JSON object
57 protected abstract JsonObject createJsonObject(Request request);
67 public String getPath() {
75 public Response handleRequest(Request request) {
76 JsonObject jsonObject = createJsonObject(request);
77 return new Response(200, "OK", "application/json", JsonUtils.format(jsonObject));