1 package org.cacert.gigi.pages;
3 import java.io.IOException;
6 import javax.servlet.ServletRequest;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
10 import org.cacert.gigi.PermissionCheckable;
11 import org.cacert.gigi.User;
12 import org.cacert.gigi.localisation.Language;
13 import org.cacert.gigi.output.template.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 implements PermissionCheckable {
23 private Template defaultTemplate;
25 public Page(String title) {
27 URL resource = getClass().getResource(getClass().getSimpleName() + ".templ");
28 if (resource != null) {
29 defaultTemplate = new Template(resource);
34 * Retrieves the default template (<className>.templ) which has
35 * already been loaded.
37 * @return the default template.
39 public Template getDefaultTemplate() {
40 return defaultTemplate;
44 * This method can be overridden to execute code and do stuff before the
45 * default template is applied.
48 * the request to handle.
50 * the response to write to
51 * @return true, if the request is consumed and the default template should
54 * if output goes wrong.
56 public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
61 * This method is called to generate the content inside the default
65 * the request to handle.
67 * the response to write to
69 * if output goes wrong.
71 public abstract void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException;
74 * Same as {@link #doGet(HttpServletRequest, HttpServletResponse)} but for
75 * POST requests. By default they are redirected to
76 * {@link #doGet(HttpServletRequest, HttpServletResponse)};
79 * the request to handle.
81 * the response to write to
83 * if output goes wrong.
85 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
90 * Returns true, if this page requires login. Default is <code>true</code>
92 * @return if the page needs login.
94 public boolean needsLogin() {
98 public String getTitle() {
102 public void setTitle(String title) {
106 public static Language getLanguage(ServletRequest req) {
107 return Language.getInstance("de");
110 public static String translate(ServletRequest req, String string) {
111 Language l = getLanguage(req);
112 return l.getTranslation(string);
115 public static User getUser(HttpServletRequest req) {
116 return LoginPage.getUser(req);
120 public boolean isPermitted(User u) {
121 return !needsLogin() || u != null;