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