1 23 24 package org.infoglue.cms.applications.common.actions; 25 26 import java.security.Principal ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 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 56 57 public abstract class InfoGlueAbstractAction extends WebworkAbstractAction 58 { 59 private final static Logger logger = Logger.getLogger(InfoGlueAbstractAction.class.getName()); 60 61 protected String colorScheme = null; 62 63 68 69 public InfoGlueAbstractAction getThis() 70 { 71 return this; 72 } 73 74 78 79 public String getLogoutURL() throws Exception 80 { 81 AuthenticationModule authenticationModule = AuthenticationModule.getAuthenticationModule(null, null); 82 return authenticationModule.getLogoutUrl(); 83 } 84 85 86 90 91 public String getURLBase() 92 { 93 return this.getRequest().getContextPath(); 94 } 95 96 100 101 public String getCurrentURL() 102 { 103 return this.getRequest().getRequestURL() + (this.getRequest().getQueryString() == null ? "" : "?" + this.getRequest().getQueryString()); 104 } 105 106 public String getOriginalFullURL() 107 { 108 String originalRequestURL = this.getRequest().getParameter("originalRequestURL"); 109 if(originalRequestURL == null || originalRequestURL.length() == 0) 110 originalRequestURL = this.getRequest().getRequestURL().toString(); 111 112 String 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 122 123 public int getSessionTimeout() 124 { 125 return this.getHttpSession().getMaxInactiveInterval(); 126 } 127 128 131 132 public List getToolLocales() 133 { 134 List toolLocales = new ArrayList (); 135 136 int index = 0; 137 String languageCode = CmsPropertyHandler.getProperty(index + ".toolLanguageCode"); 138 while(languageCode != null) 139 { 140 Locale locale = new java.util.Locale (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 157 158 public String getPrincipalPropertyValue(String propertyName, boolean escapeSpecialCharacters) 159 { 160 return getPrincipalPropertyValue(propertyName, escapeSpecialCharacters, false); 161 } 162 163 168 169 public String getPrincipalPropertyValue(String propertyName, boolean escapeSpecialCharacters, boolean findLargestValue) 170 { 171 logger.info("propertyName: " + propertyName); 172 logger.info("escapeSpecialCharacters: " + escapeSpecialCharacters); 173 logger.info("findLargestValue: " + findLargestValue); 174 175 String 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 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 196 197 public Map getPrincipalPropertyHashValues(String propertyName, boolean escapeSpecialCharacters) 198 { 199 Map value = new HashMap (); 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 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 getAnonymousPrincipal() throws SystemException 216 { 217 Principal principal = null; 218 try 219 { 220 principal = (Principal )CacheController.getCachedObject("userCache", "anonymous"); 221 if(principal == null) 222 { 223 Map arguments = new HashMap (); 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 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 250 public boolean hasAccessTo(String 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 272 public boolean hasAccessTo(String interceptionPointName, String 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 290 public String getUserName() 291 { 292 return getInfoGluePrincipal().getName(); 293 } 294 295 298 protected final String getSingleParameter(String parameterName) 299 { 300 return (String ) ActionContext.getSingleValueParameters().get(parameterName); 301 } 302 303 306 protected final String getParameter(String parameterName) 307 { 308 return (String ) ActionContext.getParameters().get(parameterName); 309 } 310 311 public String getColorScheme() 312 { 313 return colorScheme; 314 } 315 316 public void setColorScheme(String colorScheme) 317 { 318 this.colorScheme = colorScheme; 319 } 320 321 public String encode(String value) 322 { 323 return this.getResponse().encodeUrl(value); 324 } 325 326 public String getComponentRendererUrl() 327 { 328 return CmsPropertyHandler.getComponentRendererUrl(); 329 } 330 331 public String getComponentRendererAction() 332 { 333 return CmsPropertyHandler.getComponentRendererAction(); 334 } 335 336 public String getCMSBaseUrl() 337 { 338 return CmsPropertyHandler.getCmsBaseUrl(); 339 } 340 341 public Locale getLocale() 342 { 343 return this.getSession().getLocale(); 344 } 345 346 public Integer getToolId() 347 { 348 return this.getSession().getToolId(); 349 } 350 351 public String getLanguageCode() 352 { 353 return this.getSession().getLocale().getLanguage(); 354 } 355 356 public void setLanguageCode(String languageCode) 357 { 358 this.getSession().setLocale(new java.util.Locale (languageCode)); 359 } 360 361 public void setToolId(Integer toolId) 362 { 363 this.getSession().setToolId(toolId); 364 } 365 366 370 373 374 public void beginTransaction(Database db) throws SystemException 375 { 376 try 377 { 378 db.begin(); 379 } 380 catch(Exception 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 390 391 public void closeTransaction(Database db) throws SystemException 392 { 393 rollbackTransaction(db); 396 } 397 398 399 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 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 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 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 449 450 public void closeDatabase(Database db) throws SystemException 451 { 452 try 453 { 454 db.close(); 455 } 456 catch(Exception 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 |