1 package org.cacert.gigi.pages;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.UnsupportedEncodingException;
8 import javax.servlet.ServletRequest;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
12 import org.cacert.gigi.Language;
13 import org.cacert.gigi.output.Template;
16 * This class encapsulates a sub page of Gigi. A template residing nearby this
17 * class with name <className>.templ will be loaded automatically.
19 public abstract class Page {
21 private Template defaultTemplate;
23 public Page(String title) {
26 InputStream resource = getClass().getResourceAsStream(
27 getClass().getSimpleName() + ".templ");
28 if (resource != null) {
29 defaultTemplate = new Template(new InputStreamReader(resource,
32 } catch (UnsupportedEncodingException e) {
38 * Retrives the default template (<className>.templ) which has already
41 * @return the default template.
43 public Template getDefaultTemplate() {
44 return defaultTemplate;
48 * This method can be overridden to execute code and do stuff before the
49 * default template is applied.
52 * the request to handle.
54 * the response to write to
55 * @return true, iff the request is consumed and the default template should
58 * if output goes wrong.
60 public boolean beforeTemplate(HttpServletRequest req,
61 HttpServletResponse resp) throws IOException {
66 * This method is called to generate the content inside the default
70 * the request to handle.
72 * the response to write to
74 * if output goes wrong.
76 public abstract void doGet(HttpServletRequest req, HttpServletResponse resp)
80 * Same as {@link #doGet(HttpServletRequest, HttpServletResponse)} but for
81 * POST requests. By default they are redirected to
82 * {@link #doGet(HttpServletRequest, HttpServletResponse)};
85 * the request to handle.
87 * the response to write to
89 * if output goes wrong.
91 public void doPost(HttpServletRequest req, HttpServletResponse resp)
97 * Returns true, iff this page requires login. Default is <code>true</code>
99 * @return iff the page needs login.
101 public boolean needsLogin() {
105 public String getTitle() {
109 public void setTitle(String title) {
112 public static Language getLanguage(ServletRequest req) {
113 return Language.getInstance("de");
116 public static String translate(ServletRequest req, String string) {
117 Language l = getLanguage(req);
118 return l.getTranslation(string);