Don’t create a new Sone when getting the logged in Sone.
[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.util.UUID;
21
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;
29
30 /**
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.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public abstract class JsonPage implements Page {
37
38         /** The path of the page. */
39         private final String path;
40
41         /** The Sone web interface. */
42         protected final WebInterface webInterface;
43
44         /**
45          * Creates a new JSON page at the given path.
46          *
47          * @param path
48          *            The path of the page
49          * @param webInterface
50          *            The Sone web interface
51          */
52         public JsonPage(String path, WebInterface webInterface) {
53                 this.path = path;
54                 this.webInterface = webInterface;
55         }
56
57         //
58         // ACCESSORS
59         //
60
61         /**
62          * Returns the current session, creating a new session if there is no
63          * current session.
64          *
65          * @param toadletContenxt
66          *            The toadlet context
67          * @return The current session, or {@code null} if there is no current
68          *         session
69          */
70         protected Session getCurrentSession(ToadletContext toadletContenxt) {
71                 return getCurrentSession(toadletContenxt, true);
72         }
73
74         /**
75          * Returns the current session, creating a new session if there is no
76          * current session and {@code create} is {@code true}.
77          *
78          * @param toadletContenxt
79          *            The toadlet context
80          * @param create
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
84          *         session
85          */
86         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
87                 try {
88                         Session session = webInterface.getSessionManager().useSession(toadletContenxt);
89                         if (create && (session == null)) {
90                                 session = webInterface.getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
91                         }
92                         return session;
93                 } catch (freenet.clients.http.RedirectException re1) {
94                         return null;
95                 }
96         }
97
98         /**
99          * Returns the currently logged in Sone.
100          *
101          * @param toadletContext
102          *            The toadlet context
103          * @return The currently logged in Sone, or {@code null} if no Sone is
104          *         currently logged in
105          */
106         protected Sone getCurrentSone(ToadletContext toadletContext) {
107                 Session session = getCurrentSession(toadletContext);
108                 if (session == null) {
109                         return null;
110                 }
111                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
112                 if (soneId == null) {
113                         return null;
114                 }
115                 return webInterface.getCore().getLocalSone(soneId, false);
116         }
117
118         //
119         // METHODS FOR SUBCLASSES TO OVERRIDE
120         //
121
122         /**
123          * This method is called to create the JSON object that is returned back to
124          * the browser.
125          *
126          * @param request
127          *            The request to handle
128          * @return The created JSON object
129          */
130         protected abstract JsonObject createJsonObject(Request request);
131
132         /**
133          * Returns whether this command needs the form password for authentication
134          * and to prevent abuse.
135          *
136          * @return {@code true} if the form password (given as “formPassword”) is
137          *         required, {@code false} otherwise
138          */
139         protected boolean needsFormPassword() {
140                 return true;
141         }
142
143         //
144         // PROTECTED METHODS
145         //
146
147         /**
148          * Creates an error reply.
149          *
150          * @param error
151          *            The error that has occured
152          * @return The JSON object, signalling failure and the error code
153          */
154         protected JsonObject createErrorJsonObject(String error) {
155                 return new JsonObject().put("success", false).put("error", error);
156         }
157
158         //
159         // PAGE METHODS
160         //
161
162         /**
163          * {@inheritDoc}
164          */
165         @Override
166         public String getPath() {
167                 return path;
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         @Override
174         public Response handleRequest(Request request) {
175                 if (needsFormPassword()) {
176                         String formPassword = request.getHttpRequest().getParam("formPassword");
177                         if (!webInterface.getFormPassword().equals(formPassword)) {
178                                 return new Response(401, "Not authorized", "application/json", JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
179                         }
180                 }
181                 JsonObject jsonObject = createJsonObject(request);
182                 return new Response(200, "OK", "application/json", JsonUtils.format(jsonObject));
183         }
184
185 }