]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/pages/Page.java
[Doc] Typos
[gigi.git] / src / org / cacert / gigi / pages / Page.java
index afe96dc76f4dddba3820e0b05da08f91851f3763..1125db966066e35b1c0b7049e7450b33974e724f 100644 (file)
@@ -1,25 +1,96 @@
 package org.cacert.gigi.pages;
 
 import java.io.IOException;
+import java.net.URL;
 
 import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
+import org.cacert.gigi.Language;
+import org.cacert.gigi.output.Template;
+
+/**
+ * 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 {
        private String title;
+       private Template defaultTemplate;
 
        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;
        }
 
-       public abstract void doGet(ServletRequest req, ServletResponse resp)
-                       throws IOException;
+       /**
+        * 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 {
+               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;
 
-       public void doPost(ServletRequest req, ServletResponse 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 <code>true</code>
+        * 
+        * @return if the page needs login.
+        */
+       public boolean needsLogin() {
+               return true;
+       }
+
        public String getTitle() {
                return title;
        }
@@ -27,4 +98,14 @@ public abstract class Page {
        public void setTitle(String title) {
                this.title = title;
        }
+
+       public static Language getLanguage(ServletRequest req) {
+               return Language.getInstance("de");
+       }
+
+       public static String translate(ServletRequest req, String string) {
+               Language l = getLanguage(req);
+               return l.getTranslation(string);
+       }
+
 }