Let a template page have an optional icon.
[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.LinkEnabledCallback;
26 import freenet.clients.http.PageMaker;
27 import freenet.clients.http.PageNode;
28 import freenet.clients.http.ToadletContext;
29 import freenet.l10n.BaseL10n;
30
31 /**
32  * Base class for all {@link Page}s that are rendered with {@link Template}s.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class TemplatePage implements Page, LinkEnabledCallback {
37
38         /** The path of the page. */
39         private final String path;
40
41         /** The template to render. */
42         protected final Template template;
43
44         /** The L10n handler. */
45         private final BaseL10n l10n;
46
47         /** The l10n key for the page title. */
48         private final String pageTitleKey;
49
50         /**
51          * Creates a new template page.
52          *
53          * @param path
54          *            The path of the page
55          * @param template
56          *            The template to render
57          * @param l10n
58          *            The L10n handler
59          * @param pageTitleKey
60          *            The l10n key of the title page
61          */
62         public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey) {
63                 this.path = path;
64                 this.template = template;
65                 this.l10n = l10n;
66                 this.pageTitleKey = pageTitleKey;
67         }
68
69         /**
70          * {@inheritDoc}
71          */
72         @Override
73         public String getPath() {
74                 return path;
75         }
76
77         /**
78          * {@inheritDoc}
79          */
80         @Override
81         public Response handleRequest(Request request) {
82                 String redirectTarget = getRedirectTarget(request);
83                 if (redirectTarget != null) {
84                         return new RedirectResponse(redirectTarget);
85                 }
86
87                 ToadletContext toadletContext = request.getToadletContext();
88                 PageMaker pageMaker = toadletContext.getPageMaker();
89                 PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext);
90                 for (String styleSheet : getStyleSheets()) {
91                         pageNode.addCustomStyleSheet(styleSheet);
92                 }
93                 String shortcutIcon = getShortcutIcon();
94                 if (shortcutIcon != null) {
95                         pageNode.addForwardLink("icon", shortcutIcon);
96                 }
97
98                 try {
99                         processTemplate(request, template);
100                 } catch (RedirectException re1) {
101                         return new RedirectResponse(re1.getTarget());
102                 }
103
104                 StringWriter stringWriter = new StringWriter();
105                 template.render(stringWriter);
106                 pageNode.content.addChild("%", stringWriter.toString());
107
108                 return new Response(200, "OK", "text/html", pageNode.outer.generate());
109         }
110
111         /**
112          * Can be overridden to return a custom set of style sheets that are to be
113          * included in the page’s header.
114          *
115          * @return Additional style sheets to load
116          */
117         protected Collection<String> getStyleSheets() {
118                 return Collections.emptySet();
119         }
120
121         /**
122          * Returns the name of the shortcut icon to include in the page’s header.
123          *
124          * @return The URL of the shortcut icon, or {@code null} for no icon
125          */
126         protected String getShortcutIcon() {
127                 return null;
128         }
129
130         /**
131          * Can be overridden when extending classes need to set variables in the
132          * template before it is rendered.
133          *
134          * @param request
135          *            The request that is rendered
136          * @param template
137          *            The template to set variables in
138          * @throws RedirectException
139          *             if the processing page wants to redirect after processing
140          */
141         protected void processTemplate(Request request, Template template) throws RedirectException {
142                 /* do nothing. */
143         }
144
145         /**
146          * Can be overridden to redirect the user to a different page, in case a log
147          * in is required, or something else is wrong.
148          *
149          * @param request
150          *            The request that is processed
151          * @return The URL to redirect to, or {@code null} to not redirect
152          */
153         protected String getRedirectTarget(Page.Request request) {
154                 return null;
155         }
156
157         //
158         // INTERFACE LinkEnabledCallback
159         //
160
161         /**
162          * {@inheritDoc}
163          */
164         @Override
165         public boolean isEnabled(ToadletContext toadletContext) {
166                 return true;
167         }
168
169         /**
170          * Exception that can be thrown to signal that a subclassed {@link Page}
171          * wants to redirect the user during the
172          * {@link TemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
173          * method call.
174          *
175          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
176          */
177         public class RedirectException extends Exception {
178
179                 /** The target to redirect to. */
180                 private final String target;
181
182                 /**
183                  * Creates a new redirect exception.
184                  *
185                  * @param target
186                  *            The target of the redirect
187                  */
188                 public RedirectException(String target) {
189                         this.target = target;
190                 }
191
192                 /**
193                  * Returns the target to redirect to.
194                  *
195                  * @return The target to redirect to
196                  */
197                 public String getTarget() {
198                         return target;
199                 }
200
201         }
202
203 }