1 23 24 package org.infoglue.cms.applications.common.actions; 25 26 import java.lang.reflect.Method ; 27 import java.net.URLEncoder ; 28 import java.util.Locale ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpSession ; 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 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 ACCESS_DENIED = "accessDenied"; 63 64 private Error error; 65 private Errors errors = new Errors(); 66 67 private HttpServletRequest request; 68 private HttpServletResponse response; 69 private String commandName; 70 71 72 75 public Error getError() 76 { 77 logger.info("Fetching error from error-template:" + this.error); 78 return this.error; 79 } 80 81 84 public Errors getErrors() 85 { 86 logger.info("Errors:" + this.errors); 87 return errors; 88 } 89 90 93 public String doDefault() throws Exception 94 { 95 return INPUT; 96 } 97 98 99 100 103 public String execute() throws Exception 104 { 105 String 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 e) 147 { 148 logger.error("Throwable " + e, new Exception (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 e) 159 { 160 e.printStackTrace(); 161 } 162 163 return result; 164 } 165 166 170 171 public String getCurrentUrl() throws Exception 172 { 173 String urlBase = getRequest().getRequestURL().toString(); 174 String urlParameters = getRequest().getQueryString(); 175 176 return URLEncoder.encode(urlBase + "?" + urlParameters, "UTF-8"); 177 } 178 179 182 public void setCommand(String commandName) 183 { 184 this.commandName = commandName; 185 } 186 187 190 public void setServletRequest(HttpServletRequest request) 191 { 192 this.request = request; 193 } 194 195 196 197 200 public void setServletResponse(HttpServletResponse response) 201 { 202 this.response = response; 203 } 204 205 206 207 210 public void setError(Throwable throwable, Throwable cause) 211 { 212 this.error = new Error (throwable, cause); 213 } 214 215 216 219 private void setErrors(ConstraintException exception) 220 { 221 final Locale locale = getSession().getLocale(); 222 for (ConstraintException ce = exception; 223 ce != null; 224 ce = ce.getChainedException()) 225 { 226 final String fieldName = ce.getFieldName(); 227 final String errorCode = ce.getErrorCode(); 228 final String localizedErrorMessage = getLocalizedErrorMessage(locale, errorCode); 229 getErrors().addError(fieldName, localizedErrorMessage); 230 } 231 logger.debug(getErrors().toString()); 232 } 233 234 237 238 private String getLocalizedErrorMessage(Locale locale, String errorCode) 239 { 240 StringManager stringManager = StringManagerFactory.getPresentationStringManager("org.infoglue.cms.entities", locale); 242 243 return stringManager.getString(errorCode); 246 } 247 248 249 public String getLocalizedString(Locale locale, String key) 250 { 251 StringManager stringManager = StringManagerFactory.getPresentationStringManager("org.infoglue.cms.applications", locale); 252 253 return stringManager.getString(key); 254 } 255 256 259 260 private boolean isCommand() 261 { 262 return this.commandName != null && commandName.trim().length() > 0 && (this instanceof CommandDriven); 263 } 264 265 268 269 private String invokeCommand() throws Exception 270 { 271 final StringBuffer methodName = new StringBuffer ("do" + this.commandName); 272 methodName.setCharAt(2, Character.toUpperCase(methodName.charAt(2))); 273 274 String result = ""; 275 276 try 277 { 278 final Method method = getClass().getMethod(methodName.toString(), new Class [0]); 279 result = (String ) method.invoke(this, new Object [0]); 280 } 281 catch(Exception ie) 282 { 283 logger.error("Exception " + ie, ie); 285 286 try 287 { 288 throw ie.getCause(); 289 } 290 catch(ResultException e) 291 { 292 logger.error("ResultException " + e, e); 294 result = e.getResult(); 295 } 296 catch(AccessConstraintException e) 297 { 298 logger.warn("AccessConstraintException " + e, e); 300 setErrors(e); 301 result = ACCESS_DENIED; 302 } 303 catch(ConstraintException e) 304 { 305 logger.warn("ConstraintException " + e, e); 307 setErrors(e); 308 result = INPUT; 309 } 310 catch(Bug e) 311 { 312 logger.error("Bug " + e); 314 setError(e, e.getCause()); 315 result = ERROR; 316 } 317 catch(ConfigurationError e) 318 { 319 logger.error("ConfigurationError " + e); 321 setError(e, e.getCause()); 322 result = ERROR; 323 } 324 catch(SystemException e) 325 { 326 logger.error("SystemException " + e, e); 328 setError(e, e.getCause()); 329 result = ERROR; 330 } 331 catch(Throwable e) 332 { 333 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 e) 347 { 348 e.printStackTrace(); 349 } 350 351 return result; 352 353 } 354 355 public final String getRoot() 356 { 357 return request.getContextPath(); 358 } 359 360 363 364 public final InfoGluePrincipal getInfoGluePrincipal() 365 { 366 return getSession().getInfoGluePrincipal(); 367 } 368 369 372 373 protected abstract String doExecute() throws Exception ; 374 375 378 379 public final BrowserBean getBrowserBean() 380 { 381 BrowserBean browserBean = new BrowserBean(); 382 browserBean.setRequest(this.request); 383 return browserBean; 384 } 385 386 390 protected final HttpServletRequest getRequest() 391 { 392 return this.request; 393 } 394 395 399 400 protected final HttpServletResponse getResponse() 401 { 402 return this.response; 403 } 404 405 409 protected final HttpSession getHttpSession() 410 { 411 return getRequest().getSession(); 412 } 413 414 418 419 public final Session getSession() 420 { 421 return new Session (getHttpSession()); 422 } 423 424 425 } | Popular Tags |