KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.Principal JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.log4j.Logger;
37 import org.exolab.castor.jdo.Database;
38 import org.infoglue.cms.controllers.kernel.impl.simple.AccessRightController;
39 import org.infoglue.cms.controllers.kernel.impl.simple.InfoGluePrincipalControllerProxy;
40 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
41 import org.infoglue.cms.entities.management.LanguageVO;
42 import org.infoglue.cms.exception.SystemException;
43 import org.infoglue.cms.security.AuthenticationModule;
44 import org.infoglue.cms.security.InfoGluePrincipal;
45 import org.infoglue.cms.util.CmsPropertyHandler;
46 import org.infoglue.deliver.controllers.kernel.impl.simple.ExtranetController;
47 import org.infoglue.deliver.util.CacheController;
48
49 import webwork.action.ActionContext;
50
51 /**
52  * @author Mattias Bogeblad
53  *
54  * This is an abstract action used for all InfoGlue actions. Just to not have to put to much in the WebworkAbstractAction.
55  */

56
57 public abstract class InfoGlueAbstractAction extends WebworkAbstractAction
58 {
59     private final static Logger logger = Logger.getLogger(InfoGlueAbstractAction.class.getName());
60
61     protected String JavaDoc colorScheme = null;
62     
63     /**
64      * This method lets the velocity template get hold of all actions inheriting.
65      *
66      * @return The action object currently invoked
67      */

68     
69     public InfoGlueAbstractAction getThis()
70     {
71         return this;
72     }
73     
74     /**
75      * This method returns the logout url.
76      * @author Mattias Bogeblad
77      */

78     
79     public String JavaDoc getLogoutURL() throws Exception JavaDoc
80     {
81         AuthenticationModule authenticationModule = AuthenticationModule.getAuthenticationModule(null, null);
82         return authenticationModule.getLogoutUrl();
83     }
84
85
86     /**
87      * This method returns the actions url base.
88      * @author Mattias Bogeblad
89      */

90     
91     public String JavaDoc getURLBase()
92     {
93         return this.getRequest().getContextPath();
94     }
95
96     /**
97      * This method returns the current url.
98      * @author Mattias Bogeblad
99      */

100     
101     public String JavaDoc getCurrentURL()
102     {
103         return this.getRequest().getRequestURL() + (this.getRequest().getQueryString() == null ? "" : "?" + this.getRequest().getQueryString());
104     }
105
106     public String JavaDoc getOriginalFullURL()
107     {
108         String JavaDoc originalRequestURL = this.getRequest().getParameter("originalRequestURL");
109         if(originalRequestURL == null || originalRequestURL.length() == 0)
110             originalRequestURL = this.getRequest().getRequestURL().toString();
111
112         String JavaDoc originalQueryString = this.getRequest().getParameter("originalQueryString");
113         if(originalQueryString == null || originalQueryString.length() == 0)
114             originalQueryString = this.getRequest().getQueryString();
115
116         return originalRequestURL + (originalQueryString == null ? "" : "?" + originalQueryString);
117     }
118
119     /**
120      * This method returns the session timeout value.
121      */

122     
123     public int getSessionTimeout()
124     {
125         return this.getHttpSession().getMaxInactiveInterval();
126     }
127
128     /**
129      * Gets a list of tool languages
130      */

131
132     public List JavaDoc getToolLocales()
133     {
134         List JavaDoc toolLocales = new ArrayList JavaDoc();
135         
136         int index = 0;
137         String JavaDoc languageCode = CmsPropertyHandler.getProperty(index + ".toolLanguageCode");
138         while(languageCode != null)
139         {
140             Locale JavaDoc locale = new java.util.Locale JavaDoc(languageCode);
141             if(locale != null)
142                 toolLocales.add(locale);
143             
144             index++;
145             languageCode = CmsPropertyHandler.getProperty(index + ".toolLanguageCode");
146         }
147         
148         return toolLocales;
149     }
150
151     
152     /**
153      * This method returns a propertyValue for the logged in user.
154      *
155      * @author Mattias Bogeblad
156      */

157     
158     public String JavaDoc getPrincipalPropertyValue(String JavaDoc propertyName, boolean escapeSpecialCharacters)
159     {
160         return getPrincipalPropertyValue(propertyName, escapeSpecialCharacters, false);
161     }
162
163     /**
164      * This method returns a propertyValue for the logged in user.
165      *
166      * @author Mattias Bogeblad
167      */

168     
169     public String JavaDoc getPrincipalPropertyValue(String JavaDoc propertyName, boolean escapeSpecialCharacters, boolean findLargestValue)
170     {
171         logger.info("propertyName: " + propertyName);
172         logger.info("escapeSpecialCharacters: " + escapeSpecialCharacters);
173         logger.info("findLargestValue: " + findLargestValue);
174         
175         String JavaDoc value = "";
176         
177         try
178         {
179             InfoGluePrincipal infoGluePrincipal = this.getInfoGluePrincipal();
180             LanguageVO languageVO = (LanguageVO)LanguageController.getController().getLanguageVOList().get(0);
181             value = InfoGluePrincipalControllerProxy.getController().getPrincipalPropertyValue(infoGluePrincipal, propertyName, languageVO.getId(), null, false, escapeSpecialCharacters, findLargestValue);
182         }
183         catch(Exception JavaDoc e)
184         {
185             logger.warn("An error occurred trying to get property " + propertyName + " from infoGluePrincipal:" + e.getMessage(), e);
186         }
187         
188         return value;
189     }
190
191     /**
192      * Getting a property for a Principal - used for personalisation.
193      * This method starts with getting the property on the user and if it does not exist we check out the
194      * group-properties as well.
195      */

196     
197     public Map JavaDoc getPrincipalPropertyHashValues(String JavaDoc propertyName, boolean escapeSpecialCharacters)
198     {
199         Map JavaDoc value = new HashMap JavaDoc();
200         
201         try
202         {
203             InfoGluePrincipal infoGluePrincipal = this.getInfoGluePrincipal();
204             LanguageVO languageVO = (LanguageVO)LanguageController.getController().getLanguageVOList().get(0);
205             value = InfoGluePrincipalControllerProxy.getController().getPrincipalPropertyHashValues(infoGluePrincipal, propertyName, languageVO.getId(), null, false, escapeSpecialCharacters);
206         }
207         catch(Exception JavaDoc e)
208         {
209             logger.warn("An error occurred trying to get property " + propertyName + " from infoGluePrincipal:" + e.getMessage(), e);
210         }
211         
212         return value;
213     }
214
215     public Principal JavaDoc getAnonymousPrincipal() throws SystemException
216     {
217         Principal JavaDoc principal = null;
218         try
219         {
220             principal = (Principal JavaDoc)CacheController.getCachedObject("userCache", "anonymous");
221             if(principal == null)
222             {
223                 Map JavaDoc arguments = new HashMap JavaDoc();
224                 arguments.put("j_username", CmsPropertyHandler.getAnonymousUser());
225                 arguments.put("j_password", CmsPropertyHandler.getAnonymousPassword());
226                 arguments.put("ticket", this.getHttpSession().getAttribute("ticket"));
227
228                 principal = ExtranetController.getController().getAuthenticatedPrincipal(arguments);
229                 
230                 if(principal != null)
231                     CacheController.cacheObject("userCache", "anonymous", principal);
232             }
233         }
234         catch(Exception JavaDoc e)
235         {
236             logger.warn("There was no anonymous user found in the system. There must be - add the user anonymous/anonymous and try again.", e);
237             throw new SystemException("There was no anonymous user found in the system. There must be - add the user anonymous/anonymous and try again.", e);
238         }
239
240         return principal;
241     }
242     
243     /**
244      * Used by the view pages to determine if the current user has sufficient access rights
245      * to perform the action specific by the interception point name.
246      *
247      * @param interceptionPointName THe Name of the interception point to check access rights
248      * @return True is access is allowed, false otherwise
249      */

250     public boolean hasAccessTo(String JavaDoc interceptionPointName)
251     {
252         logger.info("Checking if " + getUserName() + " has access to " + interceptionPointName);
253
254         try
255         {
256             return AccessRightController.getController().getIsPrincipalAuthorized(this.getInfoGluePrincipal(), interceptionPointName);
257         }
258         catch (SystemException e)
259         {
260             logger.warn("Error checking access rights", e);
261             return false;
262         }
263     }
264
265     /**
266      * Used by the view pages to determine if the current user has sufficient access rights
267      * to perform the action specific by the interception point name.
268      *
269      * @param interceptionPointName THe Name of the interception point to check access rights
270      * @return True is access is allowed, false otherwise
271      */

272     public boolean hasAccessTo(String JavaDoc interceptionPointName, String JavaDoc extraParameter)
273     {
274         logger.info("Checking if " + getUserName() + " has access to " + interceptionPointName + " with extraParameter " + extraParameter);
275
276         try
277         {
278             return AccessRightController.getController().getIsPrincipalAuthorized(this.getInfoGluePrincipal(), interceptionPointName, extraParameter);
279         }
280         catch (SystemException e)
281         {
282             logger.warn("Error checking access rights", e);
283             return false;
284         }
285     }
286
287     /**
288      * Get the username for the currently logged in user
289      */

290     public String JavaDoc getUserName()
291     {
292         return getInfoGluePrincipal().getName();
293     }
294
295     /**
296      * Get a single parameter from the ActionContext (hides Servlet implementation)
297      */

298     protected final String JavaDoc getSingleParameter(String JavaDoc parameterName)
299     {
300         return (String JavaDoc) ActionContext.getSingleValueParameters().get(parameterName);
301     }
302
303     /**
304      * Get a parameter (could possibly be an array) from the ActionContext (hides Servlet implementation)
305      */

306     protected final String JavaDoc getParameter(String JavaDoc parameterName)
307     {
308         return (String JavaDoc) ActionContext.getParameters().get(parameterName);
309     }
310     
311     public String JavaDoc getColorScheme()
312     {
313         return colorScheme;
314     }
315     
316     public void setColorScheme(String JavaDoc colorScheme)
317     {
318         this.colorScheme = colorScheme;
319     }
320
321     public String JavaDoc encode(String JavaDoc value)
322     {
323         return this.getResponse().encodeUrl(value);
324     }
325
326     public String JavaDoc getComponentRendererUrl()
327     {
328         return CmsPropertyHandler.getComponentRendererUrl();
329     }
330     
331     public String JavaDoc getComponentRendererAction()
332     {
333         return CmsPropertyHandler.getComponentRendererAction();
334     }
335     
336     public String JavaDoc getCMSBaseUrl()
337     {
338         return CmsPropertyHandler.getCmsBaseUrl();
339     }
340     
341     public Locale JavaDoc getLocale()
342     {
343         return this.getSession().getLocale();
344     }
345     
346     public Integer JavaDoc getToolId()
347     {
348         return this.getSession().getToolId();
349     }
350
351     public String JavaDoc getLanguageCode()
352     {
353         return this.getSession().getLocale().getLanguage();
354     }
355     
356     public void setLanguageCode(String JavaDoc languageCode)
357     {
358         this.getSession().setLocale(new java.util.Locale JavaDoc(languageCode));
359     }
360
361     public void setToolId(Integer JavaDoc toolId)
362     {
363         this.getSession().setToolId(toolId);
364     }
365
366     //--------------------------------------------------------------------------
367
// Database/Transaction specific operations
368
//--------------------------------------------------------------------------
369

370     /**
371      * Begins a transaction on the supplied database
372      */

373     
374     public void beginTransaction(Database db) throws SystemException
375     {
376         try
377         {
378             db.begin();
379         }
380         catch(Exception JavaDoc e)
381         {
382             e.printStackTrace();
383             throw new SystemException("An error occurred when we tried to begin an transaction. Reason:" + e.getMessage(), e);
384         }
385     }
386        
387     /**
388      * Rollbacks a transaction on the named database
389      */

390      
391     public void closeTransaction(Database db) throws SystemException
392     {
393         //if(db != null && !db.isClosed() && db.isActive())
394
//commitTransaction(db);
395
rollbackTransaction(db);
396     }
397
398     
399     /**
400      * Ends a transaction on the named database
401      */

402     
403     public void commitTransaction(Database db) throws SystemException
404     {
405         try
406         {
407             if (db.isActive())
408             {
409                 db.commit();
410             }
411         }
412         catch(Exception JavaDoc e)
413         {
414             throw new SystemException("An error occurred when we tried to commit an transaction. Reason:" + e.getMessage(), e);
415         }
416         finally
417         {
418             closeDatabase(db);
419         }
420     }
421     
422  
423     /**
424      * Rollbacks a transaction on the named database
425      */

426      
427     public void rollbackTransaction(Database db) throws SystemException
428     {
429         try
430         {
431             if (db.isActive())
432             {
433                 db.rollback();
434             }
435         }
436         catch(Exception JavaDoc e)
437         {
438             logger.warn("An error occurred when we tried to rollback an transaction. Reason:" + e.getMessage());
439         }
440         finally
441         {
442             closeDatabase(db);
443         }
444     }
445
446     /**
447      * Close the database
448      */

449      
450     public void closeDatabase(Database db) throws SystemException
451     {
452         try
453         {
454             db.close();
455         }
456         catch(Exception JavaDoc e)
457         {
458             logger.warn("An error occurred when we close database. Reason:" + e.getMessage());
459             throw new SystemException("An error occurred when we tried to close a database. Reason:" + e.getMessage(), e);
460         }
461     }
462 }
463
464
Popular Tags