1 31 32 package org.opencms.jsp; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.flex.CmsFlexController; 38 import org.opencms.flex.CmsFlexResponse; 39 import org.opencms.loader.CmsLoaderException; 40 import org.opencms.loader.I_CmsResourceLoader; 41 import org.opencms.loader.I_CmsResourceStringDumpLoader; 42 import org.opencms.main.CmsException; 43 import org.opencms.main.OpenCms; 44 import org.opencms.staticexport.CmsLinkManager; 45 import org.opencms.util.CmsStringUtil; 46 import org.opencms.workplace.editors.directedit.CmsDirectEditParams; 47 48 import java.io.IOException ; 49 import java.util.HashMap ; 50 import java.util.Locale ; 51 import java.util.Map ; 52 53 import javax.servlet.ServletException ; 54 import javax.servlet.ServletRequest ; 55 import javax.servlet.ServletResponse ; 56 import javax.servlet.http.HttpServletRequest ; 57 import javax.servlet.http.HttpServletResponse ; 58 import javax.servlet.jsp.JspException ; 59 import javax.servlet.jsp.PageContext ; 60 import javax.servlet.jsp.tagext.BodyTagSupport ; 61 62 72 public class CmsJspTagInclude extends BodyTagSupport implements I_CmsJspTagParamParent { 73 74 75 private static final long serialVersionUID = 705978510743164951L; 76 77 78 private String m_attribute; 79 80 81 private boolean m_cacheable; 82 83 84 private boolean m_editable; 85 86 87 private String m_element; 88 89 90 private Map m_parameterMap; 91 92 93 private String m_property; 94 95 96 private String m_suffix; 97 98 99 private String m_target; 100 101 104 public CmsJspTagInclude() { 105 106 super(); 107 m_cacheable = true; 108 } 109 110 120 public static void addParameter(Map parameters, String name, String value, boolean overwrite) { 121 122 if ((parameters == null) || (name == null) || (value == null)) { 124 return; 125 } 126 127 if (parameters.containsKey(name) && (!overwrite)) { 129 String [] values = (String [])parameters.get(name); 131 String [] newValues = new String [values.length + 1]; 132 System.arraycopy(values, 0, newValues, 0, values.length); 133 newValues[values.length] = value; 134 parameters.put(name, newValues); 135 } else { 136 String [] values = new String [] {value}; 138 parameters.put(name, values); 139 } 140 } 141 142 156 public static void includeTagAction( 157 PageContext context, 158 String target, 159 String element, 160 boolean editable, 161 Map paramMap, 162 ServletRequest req, 163 ServletResponse res) throws JspException { 164 165 includeTagAction(context, target, element, null, editable, true, paramMap, req, res); 167 } 168 169 185 public static void includeTagAction( 186 PageContext context, 187 String target, 188 String element, 189 Locale locale, 190 boolean editable, 191 boolean cacheable, 192 Map paramMap, 193 ServletRequest req, 194 ServletResponse res) throws JspException { 195 196 CmsFlexController controller = CmsFlexController.getController(req); 198 199 if (target == null) { 200 target = controller.getCmsObject().getRequestContext().getUri(); 202 } 203 204 target = CmsLinkManager.getAbsoluteUri(target, controller.getCurrentRequest().getElementUri()); 206 207 try { 208 target = OpenCms.getResourceManager().resolveIncludeExtensions( 210 target, 211 element, 212 editable, 213 paramMap, 214 req, 215 res); 216 } catch (CmsException e) { 217 controller.setThrowable(e, target); 219 throw new JspException (e); 220 } 221 222 boolean directEditOpen = editable 224 && CmsJspTagEditable.startDirectEdit(context, new CmsDirectEditParams(target, element)); 225 226 Map oldParameterMap = req.getParameterMap(); 228 try { 229 Map parameterMap = (paramMap == null) ? new HashMap () : new HashMap (paramMap); 231 if (cacheable && (element != null)) { 232 addParameter(parameterMap, I_CmsResourceLoader.PARAMETER_ELEMENT, element, true); 234 } 235 controller.getCurrentRequest().addParameterMap(parameterMap); 237 if (cacheable) { 238 includeActionWithCache(controller, context, target, parameterMap, req, res); 240 } else { 241 includeActionNoCache(controller, context, target, element, locale, req, res); 243 } 244 } finally { 245 if (oldParameterMap != null) { 247 controller.getCurrentRequest().setParameterMap(oldParameterMap); 248 } 249 } 250 251 if (directEditOpen) { 253 CmsJspTagEditable.endDirectEdit(context); 254 } 255 } 256 257 270 private static void includeActionNoCache( 271 CmsFlexController controller, 272 PageContext context, 273 String target, 274 String element, 275 Locale locale, 276 ServletRequest req, 277 ServletResponse res) throws JspException { 278 279 try { 280 CmsFile file = controller.getCmsObject().readFile(target); 282 CmsObject cms = controller.getCmsObject(); 283 if (locale == null) { 284 locale = cms.getRequestContext().getLocale(); 285 } 286 I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(file); 288 String content; 289 if (loader instanceof I_CmsResourceStringDumpLoader) { 290 I_CmsResourceStringDumpLoader strLoader = (I_CmsResourceStringDumpLoader)loader; 292 content = strLoader.dumpAsString(cms, file, element, locale, req, res); 293 } else { 294 if (!(req instanceof HttpServletRequest ) || !(res instanceof HttpServletResponse )) { 295 CmsLoaderException e = new CmsLoaderException(Messages.get().container( 297 Messages.ERR_BAD_REQUEST_RESPONSE_0)); 298 throw new JspException (e); 299 } 300 byte[] result = loader.dump( 302 cms, 303 file, 304 element, 305 locale, 306 (HttpServletRequest )req, 307 (HttpServletResponse )res); 308 String encoding = cms.readPropertyObject(file, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue( 310 OpenCms.getSystemInfo().getDefaultEncoding()); 311 content = new String (result, encoding); 312 } 313 context.getOut().print(content); 315 316 } catch (ServletException e) { 317 Throwable t = (e.getRootCause() != null) ? e.getRootCause() : e; 319 t = controller.setThrowable(t, target); 320 throw new JspException (t); 321 } catch (IOException e) { 322 Throwable t = controller.setThrowable(e, target); 324 throw new JspException (t); 325 } catch (CmsException e) { 326 Throwable t = controller.setThrowable(e, target); 328 throw new JspException (t); 329 } 330 } 331 332 344 private static void includeActionWithCache( 345 CmsFlexController controller, 346 PageContext context, 347 String target, 348 Map parameterMap, 349 ServletRequest req, 350 ServletResponse res) throws JspException { 351 352 try { 353 context.getOut().print(CmsFlexResponse.FLEX_CACHE_DELIMITER); 355 controller.getCurrentResponse().addToIncludeList(target, parameterMap); 357 controller.getCurrentRequest().getRequestDispatcher(target).include(req, res); 359 } catch (ServletException e) { 360 Throwable t = (e.getRootCause() != null) ? e.getRootCause() : e; 362 t = controller.setThrowable(t, target); 363 throw new JspException (t); 364 } catch (IOException e) { 365 Throwable t = controller.setThrowable(e, target); 367 throw new JspException (t); 368 } 369 } 370 371 388 public void addParameter(String name, String value) { 389 390 if ((name == null) || (value == null)) { 392 return; 393 } 394 395 if (m_parameterMap == null) { 397 m_parameterMap = new HashMap (); 398 } 399 400 addParameter(m_parameterMap, name, value, false); 401 } 402 403 410 public int doEndTag() throws JspException { 411 412 ServletRequest req = pageContext.getRequest(); 413 ServletResponse res = pageContext.getResponse(); 414 415 if (CmsFlexController.isCmsRequest(req)) { 416 CmsObject cms = CmsFlexController.getCmsObject(req); 418 String target = null; 419 420 if (m_target != null) { 422 target = m_target + getSuffix(); 424 } else if (m_property != null) { 425 try { 427 String prop = cms.readPropertyObject(cms.getRequestContext().getUri(), m_property, true).getValue(); 428 if (prop != null) { 429 target = prop + getSuffix(); 430 } 431 } catch (Exception e) { 432 } 434 } else if (m_attribute != null) { 435 try { 437 String attr = (String )req.getAttribute(m_attribute); 438 if (attr != null) { 439 target = attr + getSuffix(); 440 } 441 } catch (Exception e) { 442 } 444 } else { 445 String body = null; 447 if (getBodyContent() != null) { 448 body = getBodyContent().getString(); 449 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(body)) { 450 target = body + getSuffix(); 452 } 453 } 455 } 456 457 includeTagAction(pageContext, target, m_element, null, m_editable, m_cacheable, m_parameterMap, req, res); 459 460 release(); 462 } 463 464 return EVAL_PAGE; 465 } 466 467 474 public int doStartTag() { 475 476 return EVAL_BODY_BUFFERED; 477 } 478 479 484 public String getAttribute() { 485 486 return m_attribute != null ? m_attribute : ""; 487 } 488 489 494 public String getCacheable() { 495 496 return String.valueOf(m_cacheable); 497 } 498 499 504 public String getEditable() { 505 506 return String.valueOf(m_editable); 507 } 508 509 514 public String getElement() { 515 516 return m_element; 517 } 518 519 525 public String getFile() { 526 527 return getPage(); 528 } 529 530 535 public String getPage() { 536 537 return m_target != null ? m_target : ""; 538 } 539 540 545 public String getProperty() { 546 547 return m_property != null ? m_property : ""; 548 } 549 550 555 public String getSuffix() { 556 557 return m_suffix != null ? m_suffix : ""; 558 } 559 560 563 public void release() { 564 565 super.release(); 566 m_target = null; 567 m_suffix = null; 568 m_property = null; 569 m_element = null; 570 m_parameterMap = null; 571 m_editable = false; 572 m_cacheable = true; 573 } 574 575 580 public void setAttribute(String attribute) { 581 582 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(attribute)) { 583 m_attribute = attribute; 584 } 585 } 586 587 594 public void setCacheable(String cacheable) { 595 596 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(cacheable)) { 597 m_cacheable = Boolean.valueOf(cacheable).booleanValue(); 598 } 599 } 600 601 608 public void setEditable(String editable) { 609 610 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(editable)) { 611 m_editable = Boolean.valueOf(editable).booleanValue(); 612 } 613 } 614 615 620 public void setElement(String element) { 621 622 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(element)) { 623 m_element = element; 624 } 625 } 626 627 633 public void setFile(String file) { 634 635 setPage(file); 636 } 637 638 643 public void setPage(String target) { 644 645 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(target)) { 646 m_target = target; 647 } 648 } 649 650 655 public void setProperty(String property) { 656 657 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(property)) { 658 m_property = property; 659 } 660 } 661 662 667 public void setSuffix(String suffix) { 668 669 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(suffix)) { 670 m_suffix = suffix.toLowerCase(); 671 } 672 } 673 }
| Popular Tags
|