1 package org.cacert.gigi.pages;
3 import java.io.IOException;
5 import java.util.Enumeration;
6 import java.util.Locale;
8 import javax.servlet.ServletRequest;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
13 import org.cacert.gigi.PermissionCheckable;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Template;
17 import org.cacert.gigi.util.AuthorizationContext;
20 * This class encapsulates a sub page of Gigi. A template residing nearby this
21 * class with name <className>.templ will be loaded automatically.
23 public abstract class Page implements PermissionCheckable {
27 private Template defaultTemplate;
29 public Page(String title) {
31 URL resource = getClass().getResource(getClass().getSimpleName() + ".templ");
32 if (resource != null) {
33 defaultTemplate = new Template(resource);
38 * Retrieves the default template (<className>.templ) which has
39 * already been loaded.
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, if the request is consumed and the default template should
58 * if output goes wrong.
60 public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
61 if (req.getMethod().equals("POST")) {
62 return beforePost(req, resp);
68 * This method can be overridden to execute code and do stuff before the
69 * default template is applied when the request is a post request and the
70 * default implementation of
71 * {@link #beforeTemplate(HttpServletRequest, HttpServletResponse)} is
75 * the request to handle.
77 * the response to write to
78 * @return true, if the request is consumed and the default template should
81 * if output goes wrong.
83 public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
88 * This method is called to generate the content inside the default
92 * the request to handle.
94 * the response to write to
96 * if output goes wrong.
98 public abstract void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException;
101 * Same as {@link #doGet(HttpServletRequest, HttpServletResponse)} but for
102 * POST requests. By default they are redirected to
103 * {@link #doGet(HttpServletRequest, HttpServletResponse)};
106 * the request to handle.
108 * the response to write to
109 * @throws IOException
110 * if output goes wrong.
112 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
117 * Returns true, if this page requires login. Default is <code>true</code>
119 * @return if the page needs login.
121 public boolean needsLogin() {
125 public String getTitle() {
129 public static Language getLanguage(ServletRequest req) {
130 HttpSession session = ((HttpServletRequest) req).getSession();
131 synchronized (session) {
133 Locale sessval = (Locale) session.getAttribute(Language.SESSION_ATTRIB_NAME);
134 if (sessval != null) {
135 Language l = Language.getInstance(sessval);
140 Enumeration<Locale> langs = req.getLocales();
141 while (langs.hasMoreElements()) {
142 Locale c = langs.nextElement();
143 Language l = Language.getInstance(c);
145 session.setAttribute(Language.SESSION_ATTRIB_NAME, l.getLocale());
149 session.setAttribute(Language.SESSION_ATTRIB_NAME, Locale.ENGLISH);
150 return Language.getInstance(Locale.ENGLISH);
154 public static String translate(ServletRequest req, String string) {
155 Language l = getLanguage(req);
156 return l.getTranslation(string);
159 public static User getUser(HttpServletRequest req) {
160 return LoginPage.getUser(req);
164 public boolean isPermitted(AuthorizationContext ac) {
165 return !needsLogin() || ac != null;