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