Add interface between templates and toadlets.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / page / TemplatePage.java
1 /*
2  * shortener - TemplatePage.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.page;
19
20 import java.io.StringWriter;
21 import java.util.Collection;
22 import java.util.Collections;
23
24 import net.pterodactylus.util.template.Template;
25 import freenet.clients.http.PageMaker;
26 import freenet.clients.http.PageNode;
27 import freenet.clients.http.ToadletContext;
28 import freenet.l10n.BaseL10n;
29
30 /**
31  * Base class for all {@link Page}s that are rendered with {@link Template}s.
32  * 
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class TemplatePage implements Page {
36
37         /** The path of the page. */
38         private final String path;
39
40         /** The template to render. */
41         private final Template template;
42
43         /** The L10n handler. */
44         private final BaseL10n l10n;
45
46         /** The l10n key for the page title. */
47         private final String pageTitleKey;
48
49         /**
50          * Creates a new template page.
51          * 
52          * @param path
53          *            The path of the page
54          * @param template
55          *            The template to render
56          * @param l10n
57          *            The L10n handler
58          * @param pageTitleKey
59          *            The l10n key of the title page
60          */
61         public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey) {
62                 this.path = path;
63                 this.template = template;
64                 this.l10n = l10n;
65                 this.pageTitleKey = pageTitleKey;
66         }
67
68         /**
69          * {@inheritDoc}
70          */
71         @Override
72         public String getPath() {
73                 return path;
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         @Override
80         public Response handleRequest(Request request) {
81                 String redirectTarget = getRedirectTarget(request);
82                 if (redirectTarget != null) {
83                         return new RedirectResponse(redirectTarget);
84                 }
85
86                 ToadletContext toadletContext = request.getToadletContext();
87                 PageMaker pageMaker = toadletContext.getPageMaker();
88                 PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext);
89                 for (String styleSheet : getStyleSheets()) {
90                         pageNode.addCustomStyleSheet(styleSheet);
91                 }
92
93                 processTemplate(request, template);
94                 StringWriter stringWriter = new StringWriter();
95                 template.render(stringWriter);
96                 pageNode.content.addChild("%", stringWriter.toString());
97
98                 return new Response(200, "OK", "text/html", pageNode.outer.generate());
99         }
100
101         /**
102          * Can be overridden to return a custom set of style sheets that are to be
103          * included in the page’s header.
104          * 
105          * @return Additional style sheets to load
106          */
107         protected Collection<String> getStyleSheets() {
108                 return Collections.emptySet();
109         }
110
111         /**
112          * Can be overridden when extending classes need to set variables in the
113          * template before it is rendered.
114          * 
115          * @param request
116          *            The request that is rendered
117          * @param template
118          *            The template to set variables in
119          */
120         protected void processTemplate(Request request, Template template) {
121                 /* do nothing. */
122         }
123
124         /**
125          * Can be overridden to redirect the user to a different page, in case a log
126          * in is required, or something else is wrong.
127          * 
128          * @param request
129          *            The request that is processed
130          * @return The URL to redirect to, or {@code null} to not redirect
131          */
132         protected String getRedirectTarget(Page.Request request) {
133                 return null;
134         }
135
136 }