Remove unused methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SoneTemplatePage.java
1 /*
2  * Sone - SoneTemplatePage.java - Copyright © 2010–2013 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;
19
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.main.SonePlugin;
30 import net.pterodactylus.sone.notify.ListNotificationFilters;
31 import net.pterodactylus.sone.web.page.FreenetRequest;
32 import net.pterodactylus.sone.web.page.FreenetTemplatePage;
33 import net.pterodactylus.util.notify.Notification;
34 import net.pterodactylus.util.template.Template;
35 import net.pterodactylus.util.template.TemplateContext;
36
37 import com.google.common.collect.ImmutableList;
38 import com.google.common.collect.ImmutableMap;
39
40 import freenet.clients.http.ToadletContext;
41 import freenet.support.api.HTTPRequest;
42
43 /**
44  * Base page for the Sone web interface.
45  *
46  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47  */
48 public class SoneTemplatePage extends FreenetTemplatePage {
49
50         /** The Sone core. */
51         protected final WebInterface webInterface;
52
53         /** The page title l10n key. */
54         private final String pageTitleKey;
55
56         /** Whether to require a login. */
57         private final boolean requireLogin;
58
59         /**
60          * Creates a new template page for Sone that does not require the user to be
61          * logged in.
62          *
63          * @param path
64          *            The path of the page
65          * @param template
66          *            The template to render
67          * @param webInterface
68          *            The Sone web interface
69          */
70         public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
71                 this(path, template, null, webInterface, false);
72         }
73
74         /**
75          * Creates a new template page for Sone that does not require the user to be
76          * logged in.
77          *
78          * @param path
79          *            The path of the page
80          * @param template
81          *            The template to render
82          * @param pageTitleKey
83          *            The l10n key of the page title
84          * @param webInterface
85          *            The Sone web interface
86          */
87         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
88                 this(path, template, pageTitleKey, webInterface, false);
89         }
90
91         /**
92          * Creates a new template page for Sone.
93          *
94          * @param path
95          *            The path of the page
96          * @param template
97          *            The template to render
98          * @param webInterface
99          *            The Sone web interface
100          * @param requireLogin
101          *            Whether this page requires a login
102          */
103         public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
104                 this(path, template, null, webInterface, requireLogin);
105         }
106
107         /**
108          * Creates a new template page for Sone.
109          *
110          * @param path
111          *            The path of the page
112          * @param template
113          *            The template to render
114          * @param pageTitleKey
115          *            The l10n key of the page title
116          * @param webInterface
117          *            The Sone web interface
118          * @param requireLogin
119          *            Whether this page requires a login
120          */
121         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
122                 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
123                 this.pageTitleKey = pageTitleKey;
124                 this.webInterface = webInterface;
125                 this.requireLogin = requireLogin;
126         }
127
128         //
129         // PROTECTED METHODS
130         //
131
132         /**
133          * Returns the currently logged in Sone.
134          *
135          * @param toadletContext
136          *            The toadlet context
137          * @return The currently logged in Sone, or {@code null} if no Sone is
138          *         currently logged in
139          */
140         protected Sone getCurrentSone(ToadletContext toadletContext) {
141                 return webInterface.getCurrentSone(toadletContext);
142         }
143
144         /**
145          * Returns the currently logged in Sone.
146          *
147          * @param toadletContext
148          *            The toadlet context
149          * @param create
150          *            {@code true} to create a new session if no session exists,
151          *            {@code false} to not create a new session
152          * @return The currently logged in Sone, or {@code null} if no Sone is
153          *         currently logged in
154          */
155         protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
156                 return webInterface.getCurrentSone(toadletContext, create);
157         }
158
159         /**
160          * Sets the currently logged in Sone.
161          *
162          * @param toadletContext
163          *            The toadlet context
164          * @param sone
165          *            The Sone to set as currently logged in
166          */
167         protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
168                 webInterface.setCurrentSone(toadletContext, sone);
169         }
170
171         //
172         // TEMPLATEPAGE METHODS
173         //
174
175         /**
176          * {@inheritDoc}
177          */
178         @Override
179         protected String getPageTitle(FreenetRequest request) {
180                 if (pageTitleKey != null) {
181                         return webInterface.getL10n().getString(pageTitleKey);
182                 }
183                 return "";
184         }
185
186         /**
187          * {@inheritDoc}
188          */
189         @Override
190         protected List<Map<String, String>> getAdditionalLinkNodes(FreenetRequest request) {
191                 return ImmutableList.<Map<String, String>> builder().add(ImmutableMap.<String, String> builder().put("rel", "search").put("type", "application/opensearchdescription+xml").put("title", "Sone").put("href", "http://" + request.getHttpRequest().getHeader("host") + "/Sone/OpenSearch.xml").build()).build();
192         }
193
194         /**
195          * {@inheritDoc}
196          */
197         @Override
198         protected Collection<String> getStyleSheets() {
199                 return Arrays.asList("css/sone.css");
200         }
201
202         /**
203          * {@inheritDoc}
204          */
205         @Override
206         protected String getShortcutIcon() {
207                 return "images/icon.png";
208         }
209
210         /**
211          * Returns whether this page requires the user to log in.
212          *
213          * @return {@code true} if the user is required to be logged in to use this
214          *         page, {@code false} otherwise
215          */
216         protected boolean requiresLogin() {
217                 return requireLogin;
218         }
219
220         /**
221          * {@inheritDoc}
222          */
223         @Override
224         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
225                 super.processTemplate(request, templateContext);
226                 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
227                 templateContext.set("core", webInterface.getCore());
228                 templateContext.set("currentSone", currentSone);
229                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
230                 templateContext.set("request", request);
231                 templateContext.set("currentVersion", SonePlugin.VERSION);
232                 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
233                 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
234                 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
235                 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
236                 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
237                 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
238                 templateContext.set("notifications", notifications);
239                 templateContext.set("notificationHash", notifications.hashCode());
240         }
241
242         /**
243          * {@inheritDoc}
244          */
245         @Override
246         protected String getRedirectTarget(FreenetRequest request) {
247                 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
248                         HTTPRequest httpRequest = request.getHttpRequest();
249                         String originalUrl = httpRequest.getPath();
250                         if (httpRequest.hasParameters()) {
251                                 StringBuilder requestParameters = new StringBuilder();
252                                 for (String parameterName : httpRequest.getParameterNames()) {
253                                         if (requestParameters.length() > 0) {
254                                                 requestParameters.append("%26");
255                                         }
256                                         String[] parameterValues = httpRequest.getMultipleParam(parameterName);
257                                         for (String parameterValue : parameterValues) {
258                                                 try {
259                                                         requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
260                                                 } catch (UnsupportedEncodingException uee1) {
261                                                         /* A JVM without UTF-8? I don’t think so. */
262                                                 }
263                                         }
264                                 }
265                                 originalUrl += "?" + requestParameters.toString();
266                         }
267                         return "login.html?target=" + originalUrl;
268                 }
269                 return null;
270         }
271
272         /**
273          * {@inheritDoc}
274          */
275         @Override
276         protected boolean isFullAccessOnly() {
277                 return webInterface.getCore().getPreferences().isRequireFullAccess();
278         }
279
280         /**
281          * {@inheritDoc}
282          */
283         @Override
284         public boolean isEnabled(ToadletContext toadletContext) {
285                 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
286                         return false;
287                 }
288                 if (requiresLogin()) {
289                         return getCurrentSone(toadletContext, false) != null;
290                 }
291                 return true;
292         }
293
294 }