]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/Page.java
upd: rename package name and all references to it
[gigi.git] / src / club / wpia / gigi / pages / Page.java
1 package club.wpia.gigi.pages;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.Enumeration;
6 import java.util.Locale;
7
8 import javax.servlet.ServletRequest;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12
13 import club.wpia.gigi.PermissionCheckable;
14 import club.wpia.gigi.dbObjects.User;
15 import club.wpia.gigi.localisation.Language;
16 import club.wpia.gigi.output.template.Template;
17 import club.wpia.gigi.util.AuthorizationContext;
18
19 /**
20  * This class encapsulates a sub page of Gigi. A template residing nearby this
21  * class with name <className>.templ will be loaded automatically.
22  */
23 public abstract class Page implements PermissionCheckable {
24
25     private String title;
26
27     private Template defaultTemplate;
28
29     public Page(String title) {
30         this.title = title;
31         URL resource = getClass().getResource(getClass().getSimpleName() + ".templ");
32         if (resource != null) {
33             defaultTemplate = new Template(resource);
34         }
35     }
36
37     /**
38      * Retrieves the default template (<className>.templ) which has
39      * already 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, if 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, HttpServletResponse resp) throws IOException {
61         if (req.getMethod().equals("POST")) {
62             return beforePost(req, resp);
63         }
64         return false;
65     }
66
67     /**
68      * This method can be overridden to execute code and do stuff before the
69      * default template is applied when the request is a post request and the
70      * default implementation of
71      * {@link #beforeTemplate(HttpServletRequest, HttpServletResponse)} is
72      * called.
73      * 
74      * @param req
75      *            the request to handle.
76      * @param resp
77      *            the response to write to
78      * @return true, if the request is consumed and the default template should
79      *         not be applied.
80      * @throws IOException
81      *             if output goes wrong.
82      */
83     public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
84         return false;
85     }
86
87     /**
88      * This method is called to generate the content inside the default
89      * template.
90      * 
91      * @param req
92      *            the request to handle.
93      * @param resp
94      *            the response to write to
95      * @throws IOException
96      *             if output goes wrong.
97      */
98     public abstract void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException;
99
100     /**
101      * Same as {@link #doGet(HttpServletRequest, HttpServletResponse)} but for
102      * POST requests. By default they are redirected to
103      * {@link #doGet(HttpServletRequest, HttpServletResponse)};
104      * 
105      * @param req
106      *            the request to handle.
107      * @param resp
108      *            the response to write to
109      * @throws IOException
110      *             if output goes wrong.
111      */
112     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
113         doGet(req, resp);
114     }
115
116     /**
117      * Returns true, if this page requires login. Default is <code>true</code>
118      * 
119      * @return if the page needs login.
120      */
121     public boolean needsLogin() {
122         return true;
123     }
124
125     public String getTitle() {
126         return title;
127     }
128
129     public static Language getLanguage(ServletRequest req) {
130         HttpSession session = ((HttpServletRequest) req).getSession();
131         synchronized (session) {
132
133             Locale sessval = (Locale) session.getAttribute(Language.SESSION_ATTRIB_NAME);
134             if (sessval != null) {
135                 Language l = Language.getInstance(sessval);
136                 if (l != null) {
137                     return l;
138                 }
139             }
140             Enumeration<Locale> langs = req.getLocales();
141             while (langs.hasMoreElements()) {
142                 Locale c = langs.nextElement();
143                 Language l = Language.getInstance(c);
144                 if (l != null) {
145                     session.setAttribute(Language.SESSION_ATTRIB_NAME, l.getLocale());
146                     return l;
147                 }
148             }
149             session.setAttribute(Language.SESSION_ATTRIB_NAME, Locale.ENGLISH);
150             return Language.getInstance(Locale.ENGLISH);
151         }
152     }
153
154     public static String translate(ServletRequest req, String string) {
155         Language l = getLanguage(req);
156         return l.getTranslation(string);
157     }
158
159     public static User getUser(HttpServletRequest req) {
160         return LoginPage.getUser(req);
161     }
162
163     @Override
164     public boolean isPermitted(AuthorizationContext ac) {
165         return !needsLogin() || ac != null;
166     }
167
168 }