X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fpages%2FPage.java;h=054a1e52020bf1172a095ea26a82ce5c3b4595dd;hp=afe96dc76f4dddba3820e0b05da08f91851f3763;hb=17a15662212d973d12ed4cea3f5eaa9c0d1169ed;hpb=6eca3763ffd17eaa7651db4dfdff48f259135865 diff --git a/src/org/cacert/gigi/pages/Page.java b/src/org/cacert/gigi/pages/Page.java index afe96dc7..054a1e52 100644 --- a/src/org/cacert/gigi/pages/Page.java +++ b/src/org/cacert/gigi/pages/Page.java @@ -1,30 +1,168 @@ package org.cacert.gigi.pages; import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import java.util.Locale; import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; -public abstract class Page { - private String title; +import org.cacert.gigi.PermissionCheckable; +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.template.Template; +import org.cacert.gigi.util.AuthorizationContext; - public Page(String title) { - this.title = title; - } +/** + * This class encapsulates a sub page of Gigi. A template residing nearby this + * class with name <className>.templ will be loaded automatically. + */ +public abstract class Page implements PermissionCheckable { - public abstract void doGet(ServletRequest req, ServletResponse resp) - throws IOException; + private String title; - public void doPost(ServletRequest req, ServletResponse resp) - throws IOException { - doGet(req, resp); - } + private Template defaultTemplate; - public String getTitle() { - return title; - } + public Page(String title) { + this.title = title; + URL resource = getClass().getResource(getClass().getSimpleName() + ".templ"); + if (resource != null) { + defaultTemplate = new Template(resource); + } + } + + /** + * Retrieves the default template (<className>.templ) which has + * already been loaded. + * + * @return the default template. + */ + public Template getDefaultTemplate() { + return defaultTemplate; + } + + /** + * This method can be overridden to execute code and do stuff before the + * default template is applied. + * + * @param req + * the request to handle. + * @param resp + * the response to write to + * @return true, if the request is consumed and the default template should + * not be applied. + * @throws IOException + * if output goes wrong. + */ + public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException { + if (req.getMethod().equals("POST")) { + return beforePost(req, resp); + } + return false; + } + + /** + * This method can be overridden to execute code and do stuff before the + * default template is applied when the request is a post request and the + * default implementation of + * {@link #beforeTemplate(HttpServletRequest, HttpServletResponse)} is + * called. + * + * @param req + * the request to handle. + * @param resp + * the response to write to + * @return true, if the request is consumed and the default template should + * not be applied. + * @throws IOException + * if output goes wrong. + */ + public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + return false; + } + + /** + * This method is called to generate the content inside the default + * template. + * + * @param req + * the request to handle. + * @param resp + * the response to write to + * @throws IOException + * if output goes wrong. + */ + public abstract void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException; + + /** + * Same as {@link #doGet(HttpServletRequest, HttpServletResponse)} but for + * POST requests. By default they are redirected to + * {@link #doGet(HttpServletRequest, HttpServletResponse)}; + * + * @param req + * the request to handle. + * @param resp + * the response to write to + * @throws IOException + * if output goes wrong. + */ + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + doGet(req, resp); + } + + /** + * Returns true, if this page requires login. Default is true + * + * @return if the page needs login. + */ + public boolean needsLogin() { + return true; + } + + public String getTitle() { + return title; + } + + public static Language getLanguage(ServletRequest req) { + HttpSession session = ((HttpServletRequest) req).getSession(); + synchronized (session) { + + Locale sessval = (Locale) session.getAttribute(Language.SESSION_ATTRIB_NAME); + if (sessval != null) { + Language l = Language.getInstance(sessval); + if (l != null) { + return l; + } + } + Enumeration langs = req.getLocales(); + while (langs.hasMoreElements()) { + Locale c = langs.nextElement(); + Language l = Language.getInstance(c); + if (l != null) { + session.setAttribute(Language.SESSION_ATTRIB_NAME, l.getLocale()); + return l; + } + } + session.setAttribute(Language.SESSION_ATTRIB_NAME, Locale.ENGLISH); + return Language.getInstance(Locale.ENGLISH); + } + } + + public static String translate(ServletRequest req, String string) { + Language l = getLanguage(req); + return l.getTranslation(string); + } + + public static User getUser(HttpServletRequest req) { + return LoginPage.getUser(req); + } + + @Override + public boolean isPermitted(AuthorizationContext ac) { + return !needsLogin() || ac != null; + } - public void setTitle(String title) { - this.title = title; - } }