1 16 package com.blandware.atleap.webapp.taglib.core.menu; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.model.core.Page; 20 import com.blandware.atleap.service.core.PageManager; 21 import com.blandware.atleap.webapp.exception.MenuUtilException; 22 import com.blandware.atleap.webapp.menu.MenuComponent; 23 import com.blandware.atleap.webapp.struts.ContentTilesRequestProcessor; 24 import com.blandware.atleap.webapp.util.core.CacheUtil; 25 import com.blandware.atleap.webapp.util.core.MenuUtil; 26 import com.blandware.atleap.webapp.util.core.WebappUtil; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.struts.Globals; 30 import org.apache.struts.taglib.TagUtils; 31 import org.apache.struts.tiles.ComponentDefinition; 32 import org.apache.struts.tiles.TilesUtil; 33 import org.springframework.context.ApplicationContext; 34 import org.springframework.web.context.support.WebApplicationContextUtils; 35 36 import javax.servlet.ServletContext ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import javax.servlet.jsp.JspException ; 40 import javax.servlet.jsp.JspTagException ; 41 import javax.servlet.jsp.PageContext ; 42 import javax.servlet.jsp.tagext.SimpleTagSupport ; 43 import java.net.MalformedURLException ; 44 import java.util.Locale ; 45 46 87 public class PrepareMenuTag extends SimpleTagSupport { 88 89 protected transient final Log log = LogFactory.getLog(PrepareMenuTag.class); 90 91 protected ApplicationContext applicationCtx = null; 92 93 96 protected String definition = null; 97 98 101 protected String uri = null; 102 103 106 protected String locale = null; 107 108 111 protected String name; 112 113 116 protected String var; 117 118 121 protected String scope; 122 123 126 protected Boolean addLocaleSuffix = Boolean.TRUE; 127 128 137 public String getName() { 138 return this.name; 139 } 140 141 146 public void setName(String name) { 147 this.name = name; 148 } 149 150 159 public String getUri() { 160 return uri; 161 } 162 163 168 public void setUri(String uri) { 169 this.uri = uri; 170 } 171 172 181 public String getDefinition() { 182 return definition; 183 } 184 185 190 public void setDefinition(String definition) { 191 this.definition = definition; 192 } 193 194 203 public String getLocale() { 204 return locale; 205 } 206 207 212 public void setLocale(String locale) { 213 this.locale = locale; 214 } 215 216 225 public String getVar() { 226 return var; 227 } 228 229 234 public void setVar(String var) { 235 this.var = var; 236 } 237 238 247 public String getScope() { 248 return scope; 249 } 250 251 256 public void setScope(String scope) { 257 this.scope = scope; 258 } 259 260 269 public Boolean getAddLocaleSuffix() { 270 return addLocaleSuffix; 271 } 272 273 278 public void setAddLocaleSuffix(Boolean addLocaleSuffix) { 279 this.addLocaleSuffix = addLocaleSuffix; 280 } 281 282 287 public void doTag() throws JspException { 288 289 PageContext pageContext = (PageContext ) getJspContext(); 290 291 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 292 ServletContext servletContext = request.getSession().getServletContext(); 293 294 applicationCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()); 295 296 if ( addLocaleSuffix == null ) { 297 addLocaleSuffix = Boolean.TRUE; 298 } 299 300 if ( locale == null ) { 302 Locale l = (Locale ) pageContext.getAttribute(Globals.LOCALE_KEY, PageContext.SESSION_SCOPE); 303 if ( l != null ) { 304 locale = l.getLanguage(); 305 } 306 } 307 if ( locale == null ) { 308 locale = Locale.getDefault().getLanguage(); 309 } 310 311 TagUtils tagUtils = TagUtils.getInstance(); 313 if ( definition != null ) { 314 ComponentDefinition tmpDef = null; 315 try { 316 tmpDef = TilesUtil.getDefinition(definition, request, servletContext); 317 } catch ( Exception ex ) { 318 } 320 if ( tmpDef == null ) { 321 String errorMessage = "Specified definition '" + definition + "' was not found"; 322 if ( log.isErrorEnabled() ) { 323 log.error(errorMessage); 324 } 325 JspTagException e = new JspTagException (errorMessage); 326 tagUtils.saveException(pageContext, e); 327 throw e; 328 } 329 } else { 330 definition = (String ) pageContext.getAttribute(ContentTilesRequestProcessor.DEFINITION_NAME, PageContext.REQUEST_SCOPE); 331 } 332 if ( definition == null ) { 333 String errorMessage = "DEFINITION_NAME attribute was not found in request. This can be used only on pages processed by action using tile definition."; 334 if ( log.isErrorEnabled() ) { 335 log.error(errorMessage); 336 } 337 JspTagException e = new JspTagException (errorMessage); 338 tagUtils.saveException(pageContext, e); 339 throw e; 340 } 341 342 if ( uri != null ) { 344 PageManager pageManager = (PageManager) applicationCtx.getBean(Constants.PAGE_MANAGER_BEAN); 345 Page page = pageManager.findPageByUri(uri); 346 if ( page == null ) { 347 String errorMessage = "Specified page uri '" + uri + "' was not found"; 348 if ( log.isErrorEnabled() ) { 349 log.error(errorMessage); 350 } 351 JspTagException e = new JspTagException (errorMessage); 352 tagUtils.saveException(pageContext, e); 353 throw e; 354 } 355 } else { 356 uri = (String ) pageContext.getAttribute(ContentTilesRequestProcessor.PROCESSED_URI, PageContext.REQUEST_SCOPE); 357 } 358 if ( uri == null ) { 359 String errorMessage = "PROCESSED_URI attribute was not found in request. This can be used only on pages processed by action using tile definition."; 360 if ( log.isErrorEnabled() ) { 361 log.error(errorMessage); 362 } 363 JspTagException e = new JspTagException (errorMessage); 364 tagUtils.saveException(pageContext, e); 365 throw e; 366 } 367 368 CacheUtil cacheUtil = CacheUtil.getInstance(request); 369 370 MenuComponent menu = null; 371 372 if ( log.isDebugEnabled() ) { 373 log.debug("Try to get from cache: menuName=" + getName() + " locale=" + locale + " definition=" + definition + " uri=" + uri); 374 } 375 menu = cacheUtil.getMenuFromCache(getName(), locale, definition, uri); 376 if ( menu == null ) { 377 if ( log.isDebugEnabled() ) { 378 log.debug("Trying to get from database"); 379 } 380 MenuUtil menuUtil = new MenuUtil((HttpServletRequest ) pageContext.getRequest()); 381 try { 382 menu = menuUtil.getMenuComponent(getName(), locale, definition, uri, true); 383 if ( menu != null ) { 384 cacheUtil.putMenuInCache(menu, getName(), locale, definition, uri); 385 } 386 } catch ( MenuUtilException e ) { 387 throw new JspTagException (e); 388 } 389 } 390 391 if ( menu != null ) { 392 try { 393 394 try { 398 setPageLocation(menu); 399 } catch ( MalformedURLException m ) { 400 if ( log.isErrorEnabled() ) { 401 log.error("Incorrect action or forward: " + m.getMessage()); 402 log.error("Menu '" + menu.getName() + "' location set to #"); 403 } 404 menu.setLocation("#"); 405 } 406 407 String attr = new String (name); 408 409 if ( var != null ) { 410 attr = new String (var); 411 } 412 413 int varScope = PageContext.PAGE_SCOPE; 414 if ( scope != null ) { 415 varScope = tagUtils.getScope(scope); 416 } 417 418 pageContext.setAttribute(attr, menu, varScope); 419 420 } catch ( Exception ex ) { 421 if ( log.isErrorEnabled() ) { 422 log.error(ex); 423 } 424 tagUtils.saveException(pageContext, ex); 425 throw new JspException (ex); 426 } 427 } else { 428 429 432 if ( log.isInfoEnabled() ) { 433 log.info("Menu '" + getName() + "' either does not exist, or it is invisible on current layer. Skipping..."); 434 } 435 } 436 } 437 438 450 protected void setPageLocation(MenuComponent menu) 451 throws MalformedURLException , JspException { 452 453 PageContext pageContext = (PageContext ) getJspContext(); 454 HttpServletRequest request = 455 (HttpServletRequest ) pageContext.getRequest(); 456 setLocation(menu); 457 String url = menu.getLocation(); 458 459 if ( (url != null) && (url.indexOf("${") > -1) ) { 461 String queryString = null; 462 463 if ( url.indexOf("?") > -1 ) { 464 queryString = url.substring(url.indexOf("?") + 1); 465 url = url.substring(0, url.indexOf(queryString)); 466 } 467 468 StringBuffer sb = new StringBuffer (); 469 470 if ( queryString != null ) { 472 sb = parseString(queryString, request); 473 menu.setUrl(url + sb.toString()); 474 } else { 475 sb = parseString(url, request); 477 menu.setUrl(sb.toString()); 478 } 479 } else { 480 menu.setUrl(url); 481 } 482 483 MenuComponent[] subMenus = menu.getMenuComponents(); 485 486 if ( subMenus.length > 0 ) { 487 for ( int i = 0; i < subMenus.length; i++ ) { 488 setPageLocation(subMenus[i]); 489 } 490 } 491 } 492 493 protected void setLocation(MenuComponent menu) throws MalformedURLException { 494 495 PageContext pageContext = (PageContext ) getJspContext(); 496 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 497 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 498 499 if ( menu.getLocation() == null ) { 503 try { 504 if ( menu.getAnchor() != null ) { 505 menu.setLocation(request.getContextPath() + 507 getAnchor(menu.getAnchor())); 508 } else if ( menu.getForward() != null ) { 509 String forward = WebappUtil.computeURL(null, menu.getForward(), null, null, null, request, response, true, false, addLocaleSuffix.booleanValue()); 510 menu.setLocation(forward); 511 } else if ( menu.getAction() != null ) { 512 String action = WebappUtil.computeURL(menu.getAction(), null, null, null, null, request, response, true, false, addLocaleSuffix.booleanValue()); 517 menu.setLocation(action); 518 } 519 } catch ( NoClassDefFoundError e ) { 520 if ( menu.getForward() != null ) { 521 throw new MalformedURLException ("forward '" + menu.getForward() + "' invalid - no struts.jar"); 522 } else if ( menu.getAction() != null ) { 523 throw new MalformedURLException ("action '" + menu.getAction() + "' invalid - no struts.jar"); 524 } 525 } 526 } else if ( addLocaleSuffix.booleanValue() && !menu.getExternalLocation().booleanValue() ) { 527 String href = menu.getLocation(); 529 if ( !WebappUtil.isAbsoluteURL(href) && !href.startsWith("/") ) { 530 href = WebappUtil.computeURL(null, null, "/" + href, null, null, request, response, true, false); 531 menu.setLocation(href); 532 } 533 } 534 } 535 536 542 protected String getAnchor(String page) { 543 if ( page.startsWith("/") ) { 544 return page; 545 } else { 546 page = "/" + page; 547 } 548 549 return page; 550 } 551 552 private StringBuffer parseString(String str, HttpServletRequest request) { 553 554 PageContext pageContext = (PageContext ) getJspContext(); 555 556 StringBuffer sb = new StringBuffer (); 557 558 while ( str.indexOf("${") >= 0 ) { 559 sb.append(str.substring(0, str.indexOf("${"))); 560 561 String variable = 562 str.substring(str.indexOf("${") + 2, str.indexOf("}")); 563 String value = (String ) pageContext.findAttribute(variable); 564 565 if ( value == null ) { 566 value = request.getParameter(variable); 568 } 569 570 if ( value == null ) { 572 log.warn("Value for '" + variable + 573 "' not found in pageContext or as a request parameter"); 574 } 575 576 sb.append(value); 577 str = str.substring(str.indexOf("}") + 1, str.length()); 578 } 579 580 return sb.append(str); 581 } 582 } 583 | Popular Tags |