KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > InfoGluePrincipalControllerProxy


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
25 package org.infoglue.cms.controllers.kernel.impl.simple;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.dom4j.Document;
36 import org.dom4j.Node;
37 import org.exolab.castor.jdo.Database;
38 import org.infoglue.cms.applications.common.VisualFormatter;
39 import org.infoglue.cms.entities.kernel.BaseEntityVO;
40 import org.infoglue.cms.entities.management.GroupProperties;
41 import org.infoglue.cms.entities.management.LanguageVO;
42 import org.infoglue.cms.entities.management.RoleProperties;
43 import org.infoglue.cms.entities.management.UserProperties;
44 import org.infoglue.cms.exception.ConstraintException;
45 import org.infoglue.cms.exception.SystemException;
46 import org.infoglue.cms.security.AuthorizationModule;
47 import org.infoglue.cms.security.InfoGlueAuthenticationFilter;
48 import org.infoglue.cms.security.InfoGlueGroup;
49 import org.infoglue.cms.security.InfoGluePrincipal;
50 import org.infoglue.cms.security.InfoGlueRole;
51 import org.infoglue.cms.util.dom.DOMBuilder;
52 import org.infoglue.deliver.controllers.kernel.impl.simple.LanguageDeliveryController;
53 import org.infoglue.deliver.util.CacheController;
54 import org.infoglue.deliver.util.NullObject;
55
56 /**
57  * @author Mattias Bogeblad
58  *
59  * This class acts as the proxy for getting a principal from the right source. The source may vary depending
60  * on security setup.
61  */

62
63 public class InfoGluePrincipalControllerProxy extends BaseController
64 {
65     private final static Logger logger = Logger.getLogger(InfoGluePrincipalControllerProxy.class.getName());
66
67     public static InfoGluePrincipalControllerProxy getController()
68     {
69         return new InfoGluePrincipalControllerProxy();
70     }
71     
72     /**
73      * This method returns a specific content-object
74      */

75     
76     public InfoGluePrincipal getInfoGluePrincipal(String JavaDoc userName) throws ConstraintException, SystemException
77     {
78         InfoGluePrincipal infoGluePrincipal = null;
79         
80         try
81         {
82             AuthorizationModule authorizationModule = (AuthorizationModule)Class.forName(InfoGlueAuthenticationFilter.authorizerClass).newInstance();
83             authorizationModule.setExtraProperties(InfoGlueAuthenticationFilter.extraProperties);
84             
85             infoGluePrincipal = authorizationModule.getAuthorizedInfoGluePrincipal(userName);
86         }
87         catch(Exception JavaDoc e)
88         {
89             e.printStackTrace();
90         }
91         
92         return infoGluePrincipal;
93     }
94  
95     
96     /**
97      * Getting a property for a Principal - used for personalisation.
98      * This method starts with getting the property on the user and if it does not exist we check out the
99      * group-properties as well.
100      */

101     
102     public String JavaDoc getPrincipalPropertyValue(InfoGluePrincipal infoGluePrincipal, String JavaDoc propertyName, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, boolean useLanguageFallback, boolean escapeSpecialCharacters, boolean findLargestValue) throws Exception JavaDoc
103     {
104         String JavaDoc value = "";
105         
106         if(infoGluePrincipal == null || propertyName == null)
107             return null;
108         
109         Database db = CastorDatabaseService.getDatabase();
110         
111         beginTransaction(db);
112         
113         try
114         {
115             value = getPrincipalPropertyValue(db, infoGluePrincipal, propertyName, languageId, siteNodeId, useLanguageFallback, escapeSpecialCharacters, findLargestValue);
116             commitTransaction(db);
117         }
118         catch(Exception JavaDoc e)
119         {
120             logger.error("An error occurred so we should not completes the transaction:" + e, e);
121             rollbackTransaction(db);
122             throw new SystemException(e.getMessage());
123         }
124             
125         return value;
126     }
127     
128     
129     /**
130      * Getting a property for a Principal - used for personalisation.
131      * This method starts with getting the property on the user and if it does not exist we check out the
132      * group-properties as well.
133      */

134     
135     public String JavaDoc getPrincipalPropertyValue(Database db, InfoGluePrincipal infoGluePrincipal, String JavaDoc propertyName, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, boolean useLanguageFallback, boolean escapeSpecialCharacters, boolean findLargestValue) throws Exception JavaDoc
136     {
137         String JavaDoc key = "" + infoGluePrincipal.getName() + "_" + propertyName + "_" + languageId + "_" + siteNodeId + "_" + useLanguageFallback + "_" + escapeSpecialCharacters + "_" + findLargestValue;
138         logger.info("key:" + key);
139         Object JavaDoc object = (String JavaDoc)CacheController.getCachedObject("principalPropertyValueCache", key);
140
141         if(object instanceof NullObject)
142         {
143             logger.info("There was an cached property but it was null:" + object);
144             return null;
145         }
146         else if(object != null)
147         {
148             logger.info("There was an cached principalPropertyValue:" + object);
149             return (String JavaDoc)object;
150         }
151
152         String JavaDoc value = "";
153         
154         if(infoGluePrincipal == null || propertyName == null)
155             return null;
156     
157         Collection JavaDoc userPropertiesList = UserPropertiesController.getController().getUserPropertiesList(infoGluePrincipal.getName(), languageId, db, true);
158         Iterator JavaDoc userPropertiesListIterator = userPropertiesList.iterator();
159         while(userPropertiesListIterator.hasNext())
160         {
161             UserProperties userProperties = (UserProperties)userPropertiesListIterator.next();
162
163             if(userProperties != null && userProperties.getLanguage().getLanguageId().equals(languageId) && userProperties.getValue() != null && propertyName != null)
164             {
165                 String JavaDoc propertyXML = userProperties.getValue();
166                 DOMBuilder domBuilder = new DOMBuilder();
167                 Document document = domBuilder.getDocument(propertyXML);
168     
169                 Node node = document.getRootElement().selectSingleNode("attributes/" + propertyName);
170                 if(node != null)
171                 {
172                     value = node.getStringValue();
173                     logger.info("Getting value: " + value);
174                     if(value != null && escapeSpecialCharacters)
175                         value = new VisualFormatter().escapeHTML(value);
176                     break;
177                 }
178             }
179         }
180         
181         if(value.equals(""))
182         {
183             List JavaDoc roles = infoGluePrincipal.getRoles();
184             String JavaDoc largestValue = "-1";
185             Iterator JavaDoc rolesIterator = roles.iterator();
186             while(rolesIterator.hasNext())
187             {
188                 InfoGlueRole role = (InfoGlueRole)rolesIterator.next();
189                 
190                 Collection JavaDoc rolePropertiesList = RolePropertiesController.getController().getRolePropertiesList(role.getName(), languageId, db, true);
191
192                 Iterator JavaDoc rolePropertiesListIterator = rolePropertiesList.iterator();
193                 while(rolePropertiesListIterator.hasNext())
194                 {
195                     RoleProperties roleProperties = (RoleProperties)rolePropertiesListIterator.next();
196                     
197                     if(roleProperties != null && roleProperties.getLanguage().getLanguageId().equals(languageId) && roleProperties.getValue() != null && propertyName != null)
198                     {
199                         String JavaDoc propertyXML = roleProperties.getValue();
200                         DOMBuilder domBuilder = new DOMBuilder();
201                         Document document = domBuilder.getDocument(propertyXML);
202                         
203                         Node node = document.getRootElement().selectSingleNode("attributes/" + propertyName);
204                         if(node != null)
205                         {
206                             value = node.getStringValue();
207                             logger.info("Getting value: " + value);
208                             if(value != null && escapeSpecialCharacters)
209                                 value = new VisualFormatter().escapeHTML(value);
210                             
211                             if(value != null && !value.equals("") && findLargestValue && new Integer JavaDoc(largestValue).intValue() < new Integer JavaDoc(value).intValue())
212                                 largestValue = value;
213                             
214                             break;
215                         }
216                     }
217                 }
218             }
219             
220             if(findLargestValue)
221                 value = largestValue;
222             
223             if(value.equals("") && useLanguageFallback)
224             {
225                 LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(db, siteNodeId);
226                 if(!masterLanguageVO.getLanguageId().equals(languageId))
227                     value = getPrincipalPropertyValue(infoGluePrincipal, propertyName, masterLanguageVO.getLanguageId(), siteNodeId, useLanguageFallback, escapeSpecialCharacters, findLargestValue);
228             }
229         }
230         
231         if(value.equals(""))
232         {
233             List JavaDoc groups = infoGluePrincipal.getGroups();
234             String JavaDoc largestValue = "-1";
235             Iterator JavaDoc groupsIterator = groups.iterator();
236             while(groupsIterator.hasNext())
237             {
238                 InfoGlueGroup group = (InfoGlueGroup)groupsIterator.next();
239                 
240                 Collection JavaDoc groupPropertiesList = GroupPropertiesController.getController().getGroupPropertiesList(group.getName(), languageId, db, true);
241
242                 Iterator JavaDoc groupPropertiesListIterator = groupPropertiesList.iterator();
243                 while(groupPropertiesListIterator.hasNext())
244                 {
245                     GroupProperties groupProperties = (GroupProperties)groupPropertiesListIterator.next();
246                     
247                     if(groupProperties != null && groupProperties.getLanguage().getLanguageId().equals(languageId) && groupProperties.getValue() != null && propertyName != null)
248                     {
249                         String JavaDoc propertyXML = groupProperties.getValue();
250                         DOMBuilder domBuilder = new DOMBuilder();
251                         Document document = domBuilder.getDocument(propertyXML);
252                         
253                         Node node = document.getRootElement().selectSingleNode("attributes/" + propertyName);
254                         if(node != null)
255                         {
256                             value = node.getStringValue();
257                             logger.info("Getting value: " + value);
258                             if(value != null && escapeSpecialCharacters)
259                                 value = new VisualFormatter().escapeHTML(value);
260                             
261                             if(value != null && !value.equals("") && findLargestValue && new Integer JavaDoc(largestValue).intValue() < new Integer JavaDoc(value).intValue())
262                                 largestValue = value;
263                             
264                             break;
265                         }
266                     }
267                 }
268             }
269             
270             if(findLargestValue)
271                 value = largestValue;
272             
273             if(value.equals("") && useLanguageFallback)
274             {
275                 LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(db, siteNodeId);
276                 if(!masterLanguageVO.getLanguageId().equals(languageId))
277                     value = getPrincipalPropertyValue(infoGluePrincipal, propertyName, masterLanguageVO.getLanguageId(), siteNodeId, useLanguageFallback, escapeSpecialCharacters, findLargestValue);
278             }
279         }
280             
281         if(value != null)
282             CacheController.getCachedObject("principalPropertyValueCache", key);
283         
284         if(value != null)
285             CacheController.cacheObject("principalPropertyValueCache", key, value);
286         else
287             CacheController.cacheObject("principalPropertyValueCache", key, new NullObject());
288         
289         return value;
290     }
291     
292     
293     
294     /**
295      * Getting a property for a Principal - used for personalisation.
296      * This method starts with getting the property on the user and if it does not exist we check out the
297      * group-properties as well. The value in question is a map - name-value.
298      */

299     
300     public Map JavaDoc getPrincipalPropertyHashValues(InfoGluePrincipal infoGluePrincipal, String JavaDoc propertyName, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, boolean useLanguageFallback, boolean escapeSpecialCharacters) throws Exception JavaDoc
301     {
302         Properties JavaDoc properties = new Properties JavaDoc();
303         
304         String JavaDoc attributeValue = getPrincipalPropertyValue(infoGluePrincipal, propertyName, languageId, siteNodeId, useLanguageFallback, escapeSpecialCharacters, false);
305         
306         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(attributeValue.getBytes("UTF-8"));
307
308         properties.load(is);
309         
310         return properties;
311     }
312
313     /**
314      * Getting a property for a Principal - used for personalisation.
315      * This method starts with getting the property on the user and if it does not exist we check out the
316      * group-properties as well. The value in question is a map - name-value.
317      */

318     
319     public Map JavaDoc getPrincipalPropertyHashValues(Database db, InfoGluePrincipal infoGluePrincipal, String JavaDoc propertyName, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, boolean useLanguageFallback, boolean escapeSpecialCharacters) throws Exception JavaDoc
320     {
321         Properties JavaDoc properties = new Properties JavaDoc();
322         
323         String JavaDoc attributeValue = getPrincipalPropertyValue(db, infoGluePrincipal, propertyName, languageId, siteNodeId, useLanguageFallback, escapeSpecialCharacters, false);
324         
325         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(attributeValue.getBytes("UTF-8"));
326
327         properties.load(is);
328         
329         return properties;
330     }
331
332     
333     public BaseEntityVO getNewVO()
334     {
335         return null;
336     }
337 }
338
Popular Tags