1 23 24 25 package org.infoglue.cms.controllers.kernel.impl.simple; 26 27 import java.io.ByteArrayInputStream ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Properties ; 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 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 75 76 public InfoGluePrincipal getInfoGluePrincipal(String 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 e) 88 { 89 e.printStackTrace(); 90 } 91 92 return infoGluePrincipal; 93 } 94 95 96 101 102 public String getPrincipalPropertyValue(InfoGluePrincipal infoGluePrincipal, String propertyName, Integer languageId, Integer siteNodeId, boolean useLanguageFallback, boolean escapeSpecialCharacters, boolean findLargestValue) throws Exception 103 { 104 String 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 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 134 135 public String getPrincipalPropertyValue(Database db, InfoGluePrincipal infoGluePrincipal, String propertyName, Integer languageId, Integer siteNodeId, boolean useLanguageFallback, boolean escapeSpecialCharacters, boolean findLargestValue) throws Exception 136 { 137 String key = "" + infoGluePrincipal.getName() + "_" + propertyName + "_" + languageId + "_" + siteNodeId + "_" + useLanguageFallback + "_" + escapeSpecialCharacters + "_" + findLargestValue; 138 logger.info("key:" + key); 139 Object object = (String )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 )object; 150 } 151 152 String value = ""; 153 154 if(infoGluePrincipal == null || propertyName == null) 155 return null; 156 157 Collection userPropertiesList = UserPropertiesController.getController().getUserPropertiesList(infoGluePrincipal.getName(), languageId, db, true); 158 Iterator 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 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 roles = infoGluePrincipal.getRoles(); 184 String largestValue = "-1"; 185 Iterator rolesIterator = roles.iterator(); 186 while(rolesIterator.hasNext()) 187 { 188 InfoGlueRole role = (InfoGlueRole)rolesIterator.next(); 189 190 Collection rolePropertiesList = RolePropertiesController.getController().getRolePropertiesList(role.getName(), languageId, db, true); 191 192 Iterator 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 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 (largestValue).intValue() < new Integer (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 groups = infoGluePrincipal.getGroups(); 234 String largestValue = "-1"; 235 Iterator groupsIterator = groups.iterator(); 236 while(groupsIterator.hasNext()) 237 { 238 InfoGlueGroup group = (InfoGlueGroup)groupsIterator.next(); 239 240 Collection groupPropertiesList = GroupPropertiesController.getController().getGroupPropertiesList(group.getName(), languageId, db, true); 241 242 Iterator 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 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 (largestValue).intValue() < new Integer (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 299 300 public Map getPrincipalPropertyHashValues(InfoGluePrincipal infoGluePrincipal, String propertyName, Integer languageId, Integer siteNodeId, boolean useLanguageFallback, boolean escapeSpecialCharacters) throws Exception 301 { 302 Properties properties = new Properties (); 303 304 String attributeValue = getPrincipalPropertyValue(infoGluePrincipal, propertyName, languageId, siteNodeId, useLanguageFallback, escapeSpecialCharacters, false); 305 306 ByteArrayInputStream is = new ByteArrayInputStream (attributeValue.getBytes("UTF-8")); 307 308 properties.load(is); 309 310 return properties; 311 } 312 313 318 319 public Map getPrincipalPropertyHashValues(Database db, InfoGluePrincipal infoGluePrincipal, String propertyName, Integer languageId, Integer siteNodeId, boolean useLanguageFallback, boolean escapeSpecialCharacters) throws Exception 320 { 321 Properties properties = new Properties (); 322 323 String attributeValue = getPrincipalPropertyValue(db, infoGluePrincipal, propertyName, languageId, siteNodeId, useLanguageFallback, escapeSpecialCharacters, false); 324 325 ByteArrayInputStream is = new ByteArrayInputStream (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 |