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