1 16 package com.blandware.atleap.webapp.taglib.core.html; 17 18 import com.blandware.atleap.webapp.util.core.ApplicationResources; 19 import com.blandware.atleap.webapp.util.core.WebappConstants; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.commons.beanutils.PropertyUtils; 23 import org.apache.struts.Globals; 24 import org.apache.struts.action.ActionMessage; 25 import org.apache.struts.action.ActionMessages; 26 import org.apache.struts.taglib.TagUtils; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.PageContext ; 31 import javax.servlet.jsp.tagext.SimpleTagSupport ; 32 import java.io.PrintWriter ; 33 import java.io.StringWriter ; 34 import java.util.Iterator ; 35 36 87 public class MessagesTag extends SimpleTagSupport { 88 89 protected transient final Log log = LogFactory.getLog(MessagesTag.class); 90 91 94 protected String property; 95 96 101 protected String stackTraceVar; 102 103 106 protected String stackTraceVarScope; 107 108 118 public String getProperty() { 119 return property; 120 } 121 122 128 public void setProperty(String property) { 129 this.property = property; 130 } 131 132 142 public String getStackTraceVar() { 143 return stackTraceVar; 144 } 145 146 152 public void setStackTraceVar(String stackTraceVar) { 153 this.stackTraceVar = stackTraceVar; 154 } 155 156 166 public String getStackTraceVarScope() { 167 return stackTraceVarScope; 168 } 169 170 176 public void setStackTraceVarScope(String stackTraceVarScope) { 177 this.stackTraceVarScope = stackTraceVarScope; 178 } 179 180 185 public void doTag() throws JspException { 186 187 PageContext pageContext = (PageContext ) getJspContext(); 188 189 boolean messagesPresent = true; 190 191 ActionMessages messages = (ActionMessages) pageContext.getAttribute(WebappConstants.MESSAGE_KEY, PageContext.REQUEST_SCOPE); 193 if ( !containRequestedMessage(messages) ) { 194 messages = (ActionMessages) pageContext.getAttribute(WebappConstants.MESSAGE_KEY, PageContext.SESSION_SCOPE); 195 if ( !containRequestedMessage(messages) ) { 196 messages = (ActionMessages) pageContext.getAttribute(Globals.MESSAGE_KEY, PageContext.REQUEST_SCOPE); 197 } else { 198 pageContext.setAttribute(WebappConstants.MESSAGE_KEY, messages, PageContext.REQUEST_SCOPE); 199 pageContext.removeAttribute(WebappConstants.MESSAGE_KEY, PageContext.SESSION_SCOPE); 200 } 201 } 202 203 if ( !containRequestedMessage(messages) ) { 205 messagesPresent = false; 206 messages = (ActionMessages) pageContext.getAttribute(WebappConstants.ERROR_KEY, PageContext.REQUEST_SCOPE); 207 if ( !containRequestedMessage(messages) ) { 208 messages = (ActionMessages) pageContext.getAttribute(WebappConstants.ERROR_KEY, PageContext.SESSION_SCOPE); 209 if ( !containRequestedMessage(messages) ) { 210 messages = (ActionMessages) pageContext.getAttribute(Globals.ERROR_KEY, PageContext.REQUEST_SCOPE); 211 if ( !containRequestedMessage(messages) ) { 212 messages = new ActionMessages(); 213 } 214 } else { 215 pageContext.setAttribute(WebappConstants.ERROR_KEY, messages, PageContext.REQUEST_SCOPE); 216 pageContext.removeAttribute(WebappConstants.ERROR_KEY, PageContext.SESSION_SCOPE); 217 } 218 } 219 } 220 221 222 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 223 TagUtils tagUtils = TagUtils.getInstance(); 224 ApplicationResources applicationResources = ApplicationResources.getInstance(pageContext.getServletContext()); 225 StringBuffer exceptionBuffer = null; 226 227 if ( property == null ) { 229 Throwable t = (Throwable ) pageContext.findAttribute("javax.servlet.error.exception"); 231 if ( t == null ) { 232 t = (Throwable ) pageContext.findAttribute("javax.servlet.error.jspException"); 233 if ( t == null ) { 234 t = (Throwable ) pageContext.findAttribute(Globals.EXCEPTION_KEY); 235 } 236 } 237 238 if ( t != null ) { 239 241 try { 244 Throwable rootCause = (Throwable ) PropertyUtils.getProperty(t, "rootCause"); 245 if ( rootCause != null ) { 246 t = rootCause; 247 } 248 } catch ( Exception e ) { 249 } 251 StringWriter sw = new StringWriter (); 252 t.printStackTrace(new PrintWriter (sw)); 253 exceptionBuffer = new StringBuffer (sw.toString()); 254 255 logException(t); 256 } 257 } 258 259 if ( messages != null && !messages.isEmpty() ) { 260 261 263 String headerKey = messagesPresent ? "core.commons.messages.header" : "core.commons.errors.header"; 264 String footerKey = messagesPresent ? "core.commons.messages.footer" : "core.commons.errors.footer"; 265 String prefixKey = messagesPresent ? "core.commons.messages.prefix" : "core.commons.errors.prefix"; 266 String suffixKey = messagesPresent ? "core.commons.messages.suffix" : "core.commons.errors.suffix"; 267 268 String header = applicationResources.getMessage(request, headerKey); 269 String footer = applicationResources.getMessage(request, footerKey); 270 String prefix = applicationResources.getMessage(request, prefixKey); 271 String suffix = applicationResources.getMessage(request, suffixKey); 272 273 Iterator reports = property == null ? messages.get() : messages.get(property); 274 275 StringBuffer results = new StringBuffer (); 276 if ( header != null && property == null ) { 277 results.append(header); 278 } 279 while ( reports.hasNext() ) { 280 ActionMessage report = (ActionMessage) reports.next(); 281 if ( report == null ) { 282 continue; 283 } 284 if ( prefix != null ) { 285 results.append(prefix); 286 } 287 288 String message = applicationResources.getMessage(request, report.getKey(), report.getValues()); 289 if ( message != null ) { 290 results.append(message); 291 } 292 293 if ( suffix != null ) { 294 results.append(suffix); 295 } 296 297 } 298 299 if ( header != null && footer != null && property == null ) { 300 results.append(footer); 301 } 302 303 tagUtils.write(pageContext, results.toString()); 304 } 305 306 if ( exceptionBuffer != null ) { 307 if ( stackTraceVar != null ) { 308 int varScope = PageContext.PAGE_SCOPE; 309 if ( stackTraceVarScope != null ) { 310 varScope = tagUtils.getScope(stackTraceVarScope); 311 } 312 pageContext.setAttribute(stackTraceVar, exceptionBuffer.toString(), varScope); 313 } else { 314 tagUtils.write(pageContext, new StringBuffer ("<pre>").append(exceptionBuffer).append("</pre>").toString()); 315 } 316 } 317 } 318 319 325 protected boolean containRequestedMessage(ActionMessages messages) { 326 if ( messages == null ) { 327 return false; 328 } 329 330 Iterator reports = null; 331 if ( property == null ) { 332 reports = messages.get(); 333 } else { 334 reports = messages.get(property); 335 } 336 return reports != null && reports.hasNext(); 337 } 338 339 344 protected void logException(Throwable t) { 345 StringWriter sw = new StringWriter (); 346 t.printStackTrace(new PrintWriter (sw)); 347 if ( log.isErrorEnabled() ) { 348 log.error(sw.toString()); 349 } 350 } 351 352 } 353 | Popular Tags |