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