1 package pl.aislib.fm;
2
3 import java.io.IOException;
4
5 import java.util.Iterator;
6 import java.util.Map;
7
8 import javax.servlet.ServletException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13
14 import org.apache.commons.logging.Log;
15
16 /***
17 * Abstract class representing one page of application.
18 *
19 * @author
20 * <table>
21 * <tr><td><a href="mailto:pikus@ais.pl">Tomasz Pik</a>, AIS.PL</td></tr>
22 * <tr><td><a href="mailto:warlock@ais.pl">Michal Jastak</a>, AIS.PL</td></tr>
23 * </table>
24 * @version $Revision: 1.6 $
25 * @since AISLIB 0.1
26 */
27 public abstract class Page {
28
29 /***
30 * HTTP Session object describing session corresponding to current request.
31 */
32 protected HttpSession session;
33
34 /***
35 * Contains HTTP request description.
36 */
37 protected HttpServletRequest request;
38
39 /***
40 * Contains HTTP response description.
41 */
42 protected HttpServletResponse response;
43
44 /***
45 * Contains Servlet description, part of which is this Page.
46 */
47 protected HttpServlet servlet;
48
49 /***
50 * Contains additional information about this Page.
51 */
52 protected PageInfo pageInfo;
53
54 /***
55 * Contains Application description to which this Page belongs.
56 */
57 protected Application application;
58
59 /***
60 * Contains Logging Component.
61 */
62 protected Log log;
63
64 /***
65 * Map of properties of the page.
66 */
67 protected Map properties;
68
69 /***
70 * <code>AttributeNameWrapper</code> object.
71 */
72 private AttributeNameWrapper nameWrapper;
73
74
75
76
77 /***
78 * Method which every subclass of <code>Page</code> must implement.
79 *
80 * @return PageResponse or null if the given Page handles request internally.
81 * @throws ServletException in case of invalid execution.
82 * @throws IOException in case of I/O errors.
83 */
84 public abstract PageResponse getPageResponse() throws IOException, ServletException;
85
86
87
88
89 /***
90 * Gets property with given name.
91 *
92 * @param propertyName name of a property.
93 * @return the property or <em>null</em> if it does not exist.
94 */
95 public Object getProperty(String propertyName) {
96 return properties.get(propertyName);
97 }
98
99 /***
100 * Gets property with given name.
101 *
102 * @param propertyName name of a property.
103 * @param defaultValue default value for the property.
104 * @return the property.
105 */
106 public Object getProperty(String propertyName, Object defaultValue) {
107 if (properties.containsKey(propertyName)) {
108 return properties.get(propertyName);
109 }
110 return defaultValue;
111 }
112
113 /***
114 * Check if a given property exists.
115 *
116 * @param propertyName name of the property.
117 * @return <code>true</code> if the property exists, <code>false</code> otherwise.
118 */
119 public boolean hasProperty(String propertyName) {
120 return properties.containsKey(propertyName);
121 }
122
123 /***
124 * Gets names of properties of the page.
125 *
126 * @return <code>Iterator</code> object.
127 */
128 public Iterator propertyNames() {
129 return properties.keySet().iterator();
130 }
131
132
133
134
135 /***
136 * Gets request parameter with a given name.
137 *
138 * @param paramName name of request parameter.
139 * @return parameter value if the parameter exists, <code>null</code> otherwise.
140 * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
141 */
142 protected String getParameter(String paramName) {
143 return request.getParameter(paramName);
144 }
145
146 /***
147 * Gets request parameters with a given name.
148 *
149 * @param paramName name of request parameter.
150 * @return array of string values of the parameter.
151 * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
152 */
153 protected String[] getParameterValues(String paramName) {
154 return request.getParameterValues(paramName);
155 }
156
157 /***
158 * Checks if there is a request parameter with given name.
159 *
160 * @param paramName name for requested parameter.
161 * @return <code>true</code> if the request contains the parameter, <code>false</code> otherwise.
162 */
163 protected boolean hasParameter(String paramName) {
164 return (null != request.getParameter(paramName));
165 }
166
167 /***
168 * Checks if there is a request parameter with given name.
169 *
170 * @param paramName name for requested parameter.
171 * @return <code>true</code> if the request contains parameter
172 * with the given name or the given name with <code>.x</code>
173 * or <code>.y</code> suffix, <code>false</code> otherwise.
174 */
175 protected boolean hasImageParameter(String paramName) {
176 return ((null != request.getParameter (paramName)) ||
177 (null != request.getParameter(paramName + ".x")) ||
178 (null != request.getParameter(paramName + ".y")));
179 }
180
181 /***
182 * <em>Trigger</em> method is called afer the initialization of the internal
183 * strucures for page instance (request, response, application). Subclasses
184 * may owerride this method for their own use.
185 */
186 protected void onLoad() {
187 ;
188 }
189
190 /***
191 * Gets session attribute for application.
192 *
193 * @param attrName name for an attribute.
194 * @return session attribute or <code>null</code>.
195 */
196 protected Object getSessionAttribute(String attrName) {
197 return session.getAttribute(nameWrapper.wrap(attrName));
198 }
199
200 /***
201 * Checks if there is a session attribute for application.
202 *
203 * @param attrName name of the attribute.
204 * @return <code>true</code> if there is an attribute with the given key,
205 * <code>false</code> otherwise.
206 */
207 protected boolean hasSessionAttribute(String attrName) {
208 return (null != session.getAttribute(nameWrapper.wrap(attrName)));
209 }
210
211 /***
212 * Removes session attribute for the application.
213 *
214 * @param attrName attribute name to remove.
215 */
216 protected void removeSessionAttribute(String attrName) {
217 session.removeAttribute(nameWrapper.wrap(attrName));
218 }
219
220 /***
221 * Sets session attribute for the application.
222 *
223 * @param attrName name of the attribute.
224 * @param attrValue value of the attribute.
225 */
226 protected void setSessionAttribute(String attrName, Object attrValue) {
227 session.setAttribute(nameWrapper.wrap(attrName), attrValue);
228 }
229
230 /***
231 * Gets an <code>Iterator</code> of <code>String</code>
232 * objects containing the names of all the session objects of application.
233 *
234 * @return <code>Iterator</code> object.
235 */
236 protected Iterator getSessionAttributeNames() {
237 return nameWrapper.unwrap(session.getAttributeNames());
238 }
239
240 /***
241 * Cleans all session objects associated with this application.
242 */
243 protected void cleanSession() {
244 Iterator iter = nameWrapper.getMatched(session.getAttributeNames());
245 while (iter.hasNext()) {
246 session.removeAttribute((String) iter.next());
247 }
248 }
249
250 /***
251 * Gets the request attribute for application.
252 *
253 * @param attrName name of the attribute for the application.
254 * @return attribute with the name or <code>null</code>.
255 */
256 protected Object getRequestAttribute(String attrName) {
257 return request.getAttribute(nameWrapper.wrap(attrName));
258 }
259
260 /***
261 * Check if there is a session attribute for the application.
262 *
263 * @param attrName name of the attribute for the application.
264 * @return <code>true</code> if there is an attribute with the given key,
265 * <code>false</code> otherwise.
266 */
267 protected boolean hasRequestAttribute(String attrName) {
268 return (null != request.getAttribute(nameWrapper.wrap(attrName)));
269 }
270
271 /***
272 * Removes the session attribute for application.
273 *
274 * @param attrName name of the attribute to remove.
275 */
276 protected void removeRequestAttribute(String attrName) {
277 request.removeAttribute(nameWrapper.wrap(attrName));
278 }
279
280 /***
281 * Sets the request attribute for application.
282 *
283 * @param attrName name of the attribute.
284 * @param attrValue value of the attribute.
285 */
286 protected void setRequestAttribute(String attrName, Object attrValue) {
287 request.setAttribute(nameWrapper.wrap(attrName), attrValue);
288 }
289
290 /***
291 * Gets configuration parameter.
292 *
293 * @param paramName name of the parameter.
294 * @return value of the parameter.
295 */
296 protected String getConfigParameter(String paramName) {
297 return application.getConfigAdapter().getConfigParameter(paramName);
298 }
299
300 /***
301 * Loads application-specific template using {@link TemplateEngine} object.
302 *
303 * @param templateName TemplateEngine-specific template name.
304 * @return template or <code>null</code>.
305 * @throws IOException if an error occurs while loading.
306 * @throws ServletException if an error occurs while loading.
307 */
308 protected Object loadTemplate(String templateName) throws IOException, ServletException {
309 return application.loadTemplate(request, response, templateName);
310 }
311
312 /***
313 * Gets new Page object corresponding to given <code>actionKey</code>.
314 *
315 * @param actionKey <i>action key</i> describing which Page Object should be created.
316 * @return initialized {@link Page} object or <code>null</code> if <code>Page</code> cannot be found.
317 * @throws ServletException if an error occurs.
318 */
319 protected Page getPage(String actionKey) throws ServletException {
320 Page page = application.getPage(actionKey);
321 if (page != null) {
322 if (!pageInfo.checkFlow(actionKey)) {
323 if (log.isWarnEnabled()) {
324 log.warn("using non defined flow from page " + pageInfo.getActionKey() + " to page " + actionKey);
325 }
326 }
327 page.init(application, request, response, session);
328 }
329 return page;
330 }
331
332 /***
333 * Gets message for given code.
334 *
335 * @param messageCode identification code for the message.
336 * @return message with specified language content.
337 * @see Application#getMessage(int, String)
338 */
339 protected Message getMessage(int messageCode) {
340 return application.getMessage(messageCode, (String) request.getAttribute(Constants.LANG));
341 }
342
343 /***
344 * Gets form.
345 *
346 * @param formName name of the form.
347 * @return the form.
348 */
349 protected Form getForm(String formName) {
350 return application.getForm(formName, (String) request.getAttribute(Constants.LANG));
351 }
352
353
354
355
356 /***
357 * Initializes the given Page object.
358 *
359 * <p>
360 * Initializes fields:
361 * {@link #request}, {@link #response} and {@link #application} are initialized
362 * using method parameters;
363 * {@link #log} and {@link #servlet} are initialized using values contained within
364 * {@link #application} object;
365 * {@link #session} is initialized using {@link HttpServletRequest#getSession} method;
366 * </p>
367 *
368 * @param _application <code>Application</code> object.
369 * @param _request <code>HttpServletRequest</code> object.
370 * @param _response <code>HttpServletResponse</code> object.
371 * @param _session <code>HttpSession</code> object.
372 */
373 void init(
374 Application _application, HttpServletRequest _request, HttpServletResponse _response, HttpSession _session
375 ) {
376 request = _request;
377 response = _response;
378 session = _session;
379 servlet = _application.getServlet();
380 application = _application;
381 log = application.getLog();
382 nameWrapper = application.getAttributeNameWrapper();
383 onLoad();
384 }
385
386 }