]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/Page.java
[Doc] Typos
[gigi.git] / src / org / cacert / gigi / pages / Page.java
1 package org.cacert.gigi.pages;
2
3 import java.io.IOException;
4 import java.net.URL;
5
6 import javax.servlet.ServletRequest;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.cacert.gigi.Language;
11 import org.cacert.gigi.output.Template;
12
13 /**
14  * This class encapsulates a sub page of Gigi. A template residing nearby this
15  * class with name <className>.templ will be loaded automatically.
16  */
17 public abstract class Page {
18         private String title;
19         private Template defaultTemplate;
20
21         public Page(String title) {
22                 this.title = title;
23                 URL resource = getClass().getResource(getClass().getSimpleName() + ".templ");
24                 if (resource != null) {
25                         defaultTemplate = new Template(resource);
26                 }
27         }
28
29         /**
30          * Retrieves the default template (<className>.templ) which has
31          * already been loaded.
32          * 
33          * @return the default template.
34          */
35         public Template getDefaultTemplate() {
36                 return defaultTemplate;
37         }
38
39         /**
40          * This method can be overridden to execute code and do stuff before the
41          * default template is applied.
42          * 
43          * @param req
44          *            the request to handle.
45          * @param resp
46          *            the response to write to
47          * @return true, if the request is consumed and the default template should
48          *         not be applied.
49          * @throws IOException
50          *             if output goes wrong.
51          */
52         public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
53                 return false;
54         }
55
56         /**
57          * This method is called to generate the content inside the default
58          * template.
59          * 
60          * @param req
61          *            the request to handle.
62          * @param resp
63          *            the response to write to
64          * @throws IOException
65          *             if output goes wrong.
66          */
67         public abstract void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException;
68
69         /**
70          * Same as {@link #doGet(HttpServletRequest, HttpServletResponse)} but for
71          * POST requests. By default they are redirected to
72          * {@link #doGet(HttpServletRequest, HttpServletResponse)};
73          * 
74          * @param req
75          *            the request to handle.
76          * @param resp
77          *            the response to write to
78          * @throws IOException
79          *             if output goes wrong.
80          */
81         public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
82                 doGet(req, resp);
83         }
84
85         /**
86          * Returns true, if this page requires login. Default is <code>true</code>
87          * 
88          * @return if the page needs login.
89          */
90         public boolean needsLogin() {
91                 return true;
92         }
93
94         public String getTitle() {
95                 return title;
96         }
97
98         public void setTitle(String title) {
99                 this.title = title;
100         }
101
102         public static Language getLanguage(ServletRequest req) {
103                 return Language.getInstance("de");
104         }
105
106         public static String translate(ServletRequest req, String string) {
107                 Language l = getLanguage(req);
108                 return l.getTranslation(string);
109         }
110
111 }