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