KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > actions > WebworkAbstractAction


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common.actions;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.URLEncoder JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.infoglue.cms.applications.common.Session;
36 import org.infoglue.cms.exception.AccessConstraintException;
37 import org.infoglue.cms.exception.Bug;
38 import org.infoglue.cms.exception.ConfigurationError;
39 import org.infoglue.cms.exception.ConstraintException;
40 import org.infoglue.cms.exception.SystemException;
41 import org.infoglue.cms.security.InfoGluePrincipal;
42 import org.infoglue.cms.util.ChangeNotificationController;
43 import org.infoglue.cms.util.StringManager;
44 import org.infoglue.cms.util.StringManagerFactory;
45 import org.infoglue.deliver.util.BrowserBean;
46
47 import webwork.action.Action;
48 import webwork.action.CommandDriven;
49 import webwork.action.ResultException;
50 import webwork.action.ServletRequestAware;
51 import webwork.action.ServletResponseAware;
52
53 /**
54  * @author Mattias Bogeblad
55  * @author Frank Febbraro (frank@phase2technology.com)
56  */

57
58 public abstract class WebworkAbstractAction implements Action, ServletRequestAware, ServletResponseAware, CommandDriven
59 {
60     private final static Logger logger = Logger.getLogger(WebworkAbstractAction.class.getName());
61
62     private final String JavaDoc ACCESS_DENIED = "accessDenied";
63     
64     private Error JavaDoc error;
65     private Errors errors = new Errors();
66
67     private HttpServletRequest JavaDoc request;
68     private HttpServletResponse JavaDoc response;
69     private String JavaDoc commandName;
70   
71     
72     /**
73      *
74      */

75     public Error JavaDoc getError()
76     {
77         logger.info("Fetching error from error-template:" + this.error);
78         return this.error;
79     }
80
81     /**
82      *
83      */

84     public Errors getErrors()
85     {
86         logger.info("Errors:" + this.errors);
87         return errors;
88     }
89   
90     /**
91      *
92      */

93     public String JavaDoc doDefault() throws Exception JavaDoc
94     {
95         return INPUT;
96     }
97
98
99
100   /**
101    *
102    */

103     public String JavaDoc execute() throws Exception JavaDoc
104     {
105         String JavaDoc result = "";
106         
107         try
108         {
109             result = isCommand() ? invokeCommand() : doExecute();
110         }
111         catch(ResultException e)
112         {
113             logger.error("ResultException " + e, e);
114             result = e.getResult();
115         }
116         catch(AccessConstraintException e)
117         {
118             logger.info("AccessConstraintException " + e, e);
119             setErrors(e);
120             result = ACCESS_DENIED;
121         }
122         catch(ConstraintException e)
123         {
124             logger.info("ConstraintException " + e, e);
125             setErrors(e);
126             result = INPUT;
127         }
128         catch(Bug e)
129         {
130             logger.error("Bug " + e);
131             setError(e, e.getCause());
132             result = ERROR;
133         }
134         catch(ConfigurationError e)
135         {
136             logger.error("ConfigurationError " + e);
137              setError(e, e.getCause());
138              result = ERROR;
139         }
140         catch(SystemException e)
141         {
142             logger.error("SystemException " + e, e);
143             setError(e, e.getCause());
144             result = ERROR;
145         }
146         catch(Throwable JavaDoc e)
147         {
148             logger.error("Throwable " + e, new Exception JavaDoc(e));
149             final Bug bug = new Bug("Uncaught exception!", e);
150             setError(bug, bug.getCause());
151             result = ERROR;
152         }
153         
154         try
155         {
156             ChangeNotificationController.notifyListeners();
157         }
158         catch(Exception JavaDoc e)
159         {
160             e.printStackTrace();
161         }
162         
163         return result;
164     }
165
166     /**
167      * This method returns the url to the current page.
168      * Could be used in case of reload for example or for logging reasons.
169      */

170     
171     public String JavaDoc getCurrentUrl() throws Exception JavaDoc
172     {
173         String JavaDoc urlBase = getRequest().getRequestURL().toString();
174         String JavaDoc urlParameters = getRequest().getQueryString();
175         
176         return URLEncoder.encode(urlBase + "?" + urlParameters, "UTF-8");
177     }
178
179     /**
180      *
181      */

182     public void setCommand(String JavaDoc commandName)
183     {
184         this.commandName = commandName;
185     }
186
187     /**
188      *
189      */

190     public void setServletRequest(HttpServletRequest JavaDoc request)
191     {
192         this.request = request;
193     }
194   
195
196
197     /**
198      *
199      */

200     public void setServletResponse(HttpServletResponse JavaDoc response)
201     {
202         this.response = response;
203     }
204
205
206
207     /**
208      *
209      */

210     public void setError(Throwable JavaDoc throwable, Throwable JavaDoc cause)
211     {
212         this.error = new Error JavaDoc(throwable, cause);
213     }
214
215
216     /**
217      *
218      */

219     private void setErrors(ConstraintException exception)
220     {
221         final Locale JavaDoc locale = getSession().getLocale();
222         for (ConstraintException ce = exception;
223             ce != null;
224             ce = ce.getChainedException())
225         {
226             final String JavaDoc fieldName = ce.getFieldName();
227             final String JavaDoc errorCode = ce.getErrorCode();
228             final String JavaDoc localizedErrorMessage = getLocalizedErrorMessage(locale, errorCode);
229             getErrors().addError(fieldName, localizedErrorMessage);
230         }
231         logger.debug(getErrors().toString());
232     }
233
234     /**
235      * <todo>Move to a ConstraintExceptionHelper class?</todo>
236      */

237   
238     private String JavaDoc getLocalizedErrorMessage(Locale JavaDoc locale, String JavaDoc errorCode)
239     {
240         // <todo>fetch packagename from somewhere</todo>
241
StringManager stringManager = StringManagerFactory.getPresentationStringManager("org.infoglue.cms.entities", locale);
242
243         // check if a specific error message exists - <todo/>
244
// nah, use the general error message
245
return stringManager.getString(errorCode);
246     }
247   
248
249     public String JavaDoc getLocalizedString(Locale JavaDoc locale, String JavaDoc key)
250     {
251         StringManager stringManager = StringManagerFactory.getPresentationStringManager("org.infoglue.cms.applications", locale);
252
253         return stringManager.getString(key);
254     }
255   
256    /**
257     *
258     */

259     
260     private boolean isCommand()
261     {
262         return this.commandName != null && commandName.trim().length() > 0 && (this instanceof CommandDriven);
263     }
264
265     /**
266      *
267      */

268     
269     private String JavaDoc invokeCommand() throws Exception JavaDoc
270     {
271         final StringBuffer JavaDoc methodName = new StringBuffer JavaDoc("do" + this.commandName);
272         methodName.setCharAt(2, Character.toUpperCase(methodName.charAt(2)));
273
274         String JavaDoc result = "";
275         
276         try
277         {
278             final Method JavaDoc method = getClass().getMethod(methodName.toString(), new Class JavaDoc[0]);
279             result = (String JavaDoc) method.invoke(this, new Object JavaDoc[0]);
280         }
281         catch(Exception JavaDoc ie)
282         {
283             //ie.printStackTrace();
284
logger.error("Exception " + ie, ie);
285             
286             try
287             {
288                 throw ie.getCause();
289             }
290             catch(ResultException e)
291             {
292                 //e.printStackTrace();
293
logger.error("ResultException " + e, e);
294                 result = e.getResult();
295             }
296             catch(AccessConstraintException e)
297             {
298                 //e.printStackTrace();
299
logger.warn("AccessConstraintException " + e, e);
300                 setErrors(e);
301                 result = ACCESS_DENIED;
302             }
303             catch(ConstraintException e)
304             {
305                 //e.printStackTrace();
306
logger.warn("ConstraintException " + e, e);
307                 setErrors(e);
308                 result = INPUT;
309             }
310             catch(Bug e)
311             {
312                 //e.printStackTrace();
313
logger.error("Bug " + e);
314                 setError(e, e.getCause());
315                 result = ERROR;
316             }
317             catch(ConfigurationError e)
318             {
319                 //e.printStackTrace();
320
logger.error("ConfigurationError " + e);
321                 setError(e, e.getCause());
322                 result = ERROR;
323             }
324             catch(SystemException e)
325             {
326                 //e.printStackTrace();
327
logger.error("SystemException " + e, e);
328                 setError(e, e.getCause());
329                 result = ERROR;
330             }
331             catch(Throwable JavaDoc e)
332             {
333                 //e.printStackTrace();
334
logger.error("Throwable " + e);
335                 final Bug bug = new Bug("Uncaught exception!", e);
336                 setError(bug, bug.getCause());
337                 result = ERROR;
338             }
339             
340         }
341
342         try
343         {
344             ChangeNotificationController.notifyListeners();
345         }
346         catch(Exception JavaDoc e)
347         {
348             e.printStackTrace();
349         }
350         
351         return result;
352         
353     }
354
355     public final String JavaDoc getRoot()
356     {
357         return request.getContextPath();
358     }
359     
360     /**
361      * This method returns a logged in principal if existing.
362      */

363
364     public final InfoGluePrincipal getInfoGluePrincipal()
365     {
366         return getSession().getInfoGluePrincipal();
367     }
368
369     /**
370      * Subclasses implement this
371      */

372
373     protected abstract String JavaDoc doExecute() throws Exception JavaDoc;
374
375     /**
376      *
377      */

378
379     public final BrowserBean getBrowserBean()
380     {
381         BrowserBean browserBean = new BrowserBean();
382         browserBean.setRequest(this.request);
383         return browserBean;
384     }
385     
386     /**
387      * Returns the HttpServletRequest
388      * TODO: Hide the implementation behind WebWorks abstractions in ActionContext
389      */

390     protected final HttpServletRequest JavaDoc getRequest()
391     {
392         return this.request;
393     }
394
395     /**
396      * Returns the HttpServletResponse
397      * TODO: Hide the implementation behind WebWorks abstractions in ActionContext
398      */

399
400     protected final HttpServletResponse JavaDoc getResponse()
401     {
402         return this.response;
403     }
404
405     /**
406      * Returns the HttpSession
407      * TODO: Hide the implementation behind WebWorks abstractions in ActionContext
408      */

409     protected final HttpSession JavaDoc getHttpSession()
410     {
411         return getRequest().getSession();
412     }
413
414     /**
415      * Use the ActionContext to initialize the Session and remove the dependence
416      * on HTTP and the Servlet Spec. Makes it much easier for testing.
417      */

418
419     public final Session JavaDoc getSession()
420     {
421         return new Session JavaDoc(getHttpSession());
422     }
423   
424
425 }
Popular Tags