1 31 32 package org.opencms.loader; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.flex.CmsFlexCache; 39 import org.opencms.flex.CmsFlexController; 40 import org.opencms.flex.CmsFlexRequest; 41 import org.opencms.flex.CmsFlexResponse; 42 import org.opencms.i18n.CmsEncoder; 43 import org.opencms.i18n.CmsMessageContainer; 44 import org.opencms.main.CmsException; 45 import org.opencms.main.CmsLog; 46 import org.opencms.main.OpenCms; 47 import org.opencms.staticexport.CmsLinkManager; 48 import org.opencms.util.CmsFileUtil; 49 import org.opencms.util.CmsRequestUtil; 50 import org.opencms.workplace.CmsWorkplaceManager; 51 52 import java.io.File ; 53 import java.io.FileNotFoundException ; 54 import java.io.FileOutputStream ; 55 import java.io.IOException ; 56 import java.io.UnsupportedEncodingException ; 57 import java.net.SocketException ; 58 import java.util.Collections ; 59 import java.util.HashSet ; 60 import java.util.Locale ; 61 import java.util.Map ; 62 import java.util.Set ; 63 import java.util.TreeMap ; 64 65 import javax.servlet.ServletException ; 66 import javax.servlet.ServletRequest ; 67 import javax.servlet.ServletResponse ; 68 import javax.servlet.http.HttpServletRequest ; 69 import javax.servlet.http.HttpServletResponse ; 70 71 import org.apache.commons.collections.ExtendedProperties; 72 import org.apache.commons.logging.Log; 73 74 114 public class CmsJspLoader implements I_CmsResourceLoader, I_CmsFlexCacheEnabledLoader { 115 116 117 public static final String CACHE_PROPERTY_BYPASS = "bypass"; 118 119 120 public static final String CACHE_PROPERTY_STREAM = "stream"; 121 122 123 public static final String DEFAULT_JSP_FOLDER = "/WEB-INF/jsp/"; 124 125 126 public static final String DIRECTIVE_END = "%>"; 127 128 129 public static final String DIRECTIVE_START = "<%@"; 130 131 132 public static final String JSP_EXTENSION = ".jsp"; 133 134 135 public static final String PARAM_CLIENT_CACHE_MAXAGE = "client.cache.maxage"; 136 137 138 public static final String PARAM_JSP_ERRORPAGE_COMMITTED = "jsp.errorpage.committed"; 139 140 141 public static final String PARAM_JSP_FOLDER = "jsp.folder"; 142 143 144 public static final String PARAM_JSP_REPOSITORY = "jsp.repository"; 145 146 147 public static final int RESOURCE_LOADER_ID = 6; 148 149 150 private static final Log LOG = CmsLog.getLog(CmsJspLoader.class); 151 152 153 private static long m_clientCacheMaxAge; 154 155 156 private static String m_jspRepository; 157 158 159 private static String m_jspWebAppRepository; 160 161 162 private CmsFlexCache m_cache; 163 164 165 private Map m_configuration; 166 167 168 private boolean m_errorPagesAreNotCommited; 171 177 public CmsJspLoader() { 178 179 m_configuration = new TreeMap (); 180 } 181 182 188 public static String getJspRepository() { 189 190 return m_jspRepository; 191 } 192 193 196 public void addConfigurationParameter(String paramName, String paramValue) { 197 198 m_configuration.put(paramName, paramValue); 199 } 200 201 202 public void destroy() { 203 204 } 206 207 210 public byte[] dump( 211 CmsObject cms, 212 CmsResource file, 213 String element, 214 Locale locale, 215 HttpServletRequest req, 216 HttpServletResponse res) throws ServletException , IOException { 217 218 CmsFlexController controller = CmsFlexController.getController(req); 220 CmsFlexController oldController = null; 221 222 if (controller != null) { 223 oldController = controller; 225 } 226 227 byte[] result = null; 228 try { 229 controller = getController(cms, file, req, res, false, false); 231 if (element != null) { 232 String [] value = new String [] {element}; 234 Map parameters = Collections.singletonMap(I_CmsResourceLoader.PARAMETER_ELEMENT, value); 235 controller.getCurrentRequest().addParameterMap(parameters); 236 } 237 result = dispatchJsp(controller); 239 CmsFlexController.removeController(req); 241 } finally { 242 if (oldController != null) { 243 oldController.updateDates(controller.getDateLastModified(), controller.getDateExpires()); 245 CmsFlexController.setController(req, oldController); 247 } 248 } 249 250 return result; 251 } 252 253 256 public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 257 throws ServletException , IOException { 258 259 CmsFlexController controller = getController(cms, resource, req, res, false, true); 261 262 byte[] result = dispatchJsp(controller); 264 265 CmsFlexController.removeController(req); 267 268 return result; 270 } 271 272 275 public Map getConfiguration() { 276 277 return Collections.unmodifiableMap(m_configuration); 279 } 280 281 284 public int getLoaderId() { 285 286 return RESOURCE_LOADER_ID; 287 } 288 289 296 public String getResourceLoaderInfo() { 297 298 return Messages.get().getBundle().key(Messages.GUI_LOADER_JSP_DEFAULT_DESC_0); 299 } 300 301 304 public void initConfiguration() { 305 306 ExtendedProperties config = new ExtendedProperties(); 307 config.putAll(m_configuration); 308 309 m_jspRepository = config.getString(PARAM_JSP_REPOSITORY); 310 if (m_jspRepository == null) { 311 m_jspRepository = OpenCms.getSystemInfo().getWebApplicationRfsPath(); 312 } 313 m_jspWebAppRepository = config.getString(PARAM_JSP_FOLDER, DEFAULT_JSP_FOLDER); 314 if (!m_jspWebAppRepository.endsWith("/")) { 315 m_jspWebAppRepository += "/"; 316 } 317 m_jspRepository = CmsFileUtil.normalizePath(m_jspRepository + m_jspWebAppRepository); 318 319 String maxAge = config.getString(PARAM_CLIENT_CACHE_MAXAGE); 320 if (maxAge == null) { 321 m_clientCacheMaxAge = -1; 322 } else { 323 m_clientCacheMaxAge = Long.parseLong(maxAge); 324 } 325 326 m_errorPagesAreNotCommited = config.getBoolean(PARAM_JSP_ERRORPAGE_COMMITTED, true); 328 329 if (CmsLog.INIT.isInfoEnabled()) { 331 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_JSP_REPOSITORY_ABS_PATH_1, m_jspRepository)); 332 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WEBAPP_PATH_1, m_jspWebAppRepository)); 333 CmsLog.INIT.info(Messages.get().getBundle().key( 334 Messages.INIT_JSP_REPOSITORY_ERR_PAGE_COMMOTED_1, 335 new Boolean (m_errorPagesAreNotCommited))); 336 if (maxAge != null) { 337 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_CLIENT_CACHE_MAX_AGE_1, maxAge)); 338 } 339 340 CmsLog.INIT.info(Messages.get().getBundle().key( 341 Messages.INIT_LOADER_INITIALIZED_1, 342 this.getClass().getName())); 343 } 344 } 345 346 349 public boolean isStaticExportEnabled() { 350 351 return true; 352 } 353 354 357 public boolean isStaticExportProcessable() { 358 359 return true; 360 } 361 362 365 public boolean isUsableForTemplates() { 366 367 return true; 368 } 369 370 373 public boolean isUsingUriWhenLoadingTemplate() { 374 375 return false; 376 } 377 378 381 public void load(CmsObject cms, CmsResource file, HttpServletRequest req, HttpServletResponse res) 382 throws ServletException , IOException , CmsException { 383 384 boolean streaming = false; 386 boolean bypass = false; 387 388 String cacheProperty = cms.readPropertyObject(file, CmsPropertyDefinition.PROPERTY_CACHE, true).getValue(); 390 if (cacheProperty != null) { 391 cacheProperty = cacheProperty.trim(); 392 if (CACHE_PROPERTY_STREAM.equals(cacheProperty)) { 393 streaming = true; 394 } else if (CACHE_PROPERTY_BYPASS.equals(cacheProperty)) { 395 streaming = true; 396 bypass = true; 397 } 398 } 399 400 CmsFlexController controller = getController(cms, file, req, res, streaming, true); 402 403 if (bypass || controller.isForwardMode()) { 404 controller.setForwardMode(true); 406 String target = updateJsp(file, controller, new HashSet ()); 408 req.getRequestDispatcher(target).forward(controller.getCurrentRequest(), res); 410 } else { 411 dispatchJsp(controller); 413 } 414 415 if (!controller.isForwardMode()) { 417 CmsFlexController.removeController(req); 418 } 419 } 420 421 424 public void service(CmsObject cms, CmsResource resource, ServletRequest req, ServletResponse res) 425 throws ServletException , IOException , CmsLoaderException { 426 427 CmsFlexController controller = CmsFlexController.getController(req); 428 String target = updateJsp(resource, controller, new HashSet (8)); 430 controller.getCurrentResponse().setOnlyBuffering(true); 432 controller.getCurrentRequest().getRequestDispatcherToExternal(cms.getSitePath(resource), target).include( 434 req, 435 res); 436 } 437 438 441 public void setFlexCache(CmsFlexCache cache) { 442 443 m_cache = cache; 444 if (CmsLog.INIT.isInfoEnabled()) { 446 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_ADD_FLEX_CACHE_0)); 447 } 448 } 449 450 460 private byte[] dispatchJsp(CmsFlexController controller) throws ServletException , IOException { 461 462 CmsFlexRequest f_req = controller.getCurrentRequest(); 464 CmsFlexResponse f_res = controller.getCurrentResponse(); 465 466 try { 467 f_req.getRequestDispatcher(controller.getCmsObject().getSitePath(controller.getCmsResource())).include( 468 f_req, 469 f_res); 470 } catch (SocketException e) { 471 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IGNORING_EXC_1, e.getClass().getName()), e); 473 } 474 475 byte[] result = null; 476 HttpServletResponse res = controller.getTopResponse(); 477 478 if (!controller.isStreaming() && !f_res.isSuspended()) { 479 try { 480 if (!res.isCommitted() || m_errorPagesAreNotCommited) { 482 483 boolean isWorkplaceUser = CmsWorkplaceManager.isWorkplaceUser(f_req); 485 486 if (controller.isTop() 488 && !isWorkplaceUser 489 && CmsFlexController.isNotModifiedSince(f_req, controller.getDateLastModified())) { 490 if (f_req.getParameterMap().size() == 0) { 491 CmsFlexController.setDateExpiresHeader( 495 res, 496 controller.getDateExpires(), 497 m_clientCacheMaxAge); 498 } 499 res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 500 return null; 501 } 502 503 result = f_res.getWriterBytes(); 505 HttpServletRequest req = controller.getTopRequest(); 506 if (req.getHeader(CmsRequestUtil.HEADER_OPENCMS_EXPORT) != null) { 507 req.setAttribute(CmsRequestUtil.HEADER_OPENCMS_EXPORT, new Long ( 509 controller.getDateLastModified())); 510 } else if (controller.isTop()) { 511 res.setContentLength(result.length); 513 Integer errorCode = (Integer )req.getAttribute(CmsRequestUtil.ATTRIBUTE_ERRORCODE); 515 if (errorCode == null) { 516 if (isWorkplaceUser) { 518 res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, System.currentTimeMillis()); 519 CmsRequestUtil.setNoCacheHeaders(res); 520 } else { 521 CmsFlexController.setDateLastModifiedHeader(res, controller.getDateLastModified()); 523 if ((f_req.getParameterMap().size() == 0) && (controller.getDateLastModified() > -1)) { 524 CmsFlexController.setDateExpiresHeader( 529 res, 530 controller.getDateExpires(), 531 m_clientCacheMaxAge); 532 } 533 } 534 res.setStatus(HttpServletResponse.SC_OK); 536 } else { 537 res.setStatus(errorCode.intValue()); 539 } 540 CmsFlexResponse.processHeaders(f_res.getHeaders(), res); 542 res.getOutputStream().write(result); 543 res.getOutputStream().flush(); 544 } 545 } 546 } catch (IllegalStateException e) { 547 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IGNORING_EXC_1, e.getClass().getName()), e); 549 } catch (SocketException e) { 550 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IGNORING_EXC_1, e.getClass().getName()), e); 552 } 553 } 554 555 return result; 556 } 557 558 570 private CmsFlexController getController( 571 CmsObject cms, 572 CmsResource resource, 573 HttpServletRequest req, 574 HttpServletResponse res, 575 boolean streaming, 576 boolean top) { 577 578 CmsFlexController controller = null; 579 if (top) { 580 controller = CmsFlexController.getController(req); 582 } 583 if (controller == null) { 584 controller = new CmsFlexController(cms, resource, m_cache, req, res, streaming, top); 586 CmsFlexController.setController(req, controller); 587 CmsFlexRequest f_req = new CmsFlexRequest(req, controller); 588 CmsFlexResponse f_res = new CmsFlexResponse(res, controller, streaming, true); 589 controller.push(f_req, f_res); 590 } else if (controller.isForwardMode()) { 591 controller = new CmsFlexController(cms, controller); 593 CmsFlexController.setController(req, controller); 594 } 595 return controller; 596 } 597 598 609 private byte[] parseJsp( 610 byte[] byteContent, 611 String encoding, 612 CmsFlexController controller, 613 Set includes, 614 boolean isHardInclude) { 615 616 String content; 617 try { 619 content = new String (byteContent, encoding); 620 } catch (UnsupportedEncodingException e) { 621 LOG.error(Messages.get().getBundle().key( 623 Messages.LOG_UNSUPPORTED_ENC_1, 624 controller.getCurrentRequest().getElementUri()), e); 625 try { 626 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 627 content = new String (byteContent, encoding); 628 } catch (UnsupportedEncodingException e2) { 629 content = new String (byteContent); 631 } 632 } 633 634 content = parseJspCmsTag(content, controller, includes); 636 content = parseJspIncludes(content, controller, includes); 638 content = parseJspEncoding(content, encoding, isHardInclude); 640 try { 642 return content.getBytes(encoding); 643 } catch (UnsupportedEncodingException e) { 644 return content.getBytes(); 646 } 647 } 648 649 657 private String parseJspCmsTag(String content, CmsFlexController controller, Set includes) { 658 659 int i1 = content.indexOf(DIRECTIVE_START); 661 if (i1 < 0) { 662 return content; 664 } 665 666 StringBuffer buf = new StringBuffer (content.length()); 667 int p0 = 0, i2 = 0, slen = DIRECTIVE_START.length(), elen = DIRECTIVE_END.length(); 668 669 while (i1 >= 0) { 670 i2 = content.indexOf(DIRECTIVE_END, i1 + slen); 672 if (i2 < 0) { 673 return content; 675 } else if (i2 > i1) { 676 String directive = content.substring(i1 + slen, i2); 677 if (LOG.isDebugEnabled()) { 678 LOG.debug(Messages.get().getBundle().key( 679 Messages.LOG_DIRECTIVE_DETECTED_3, 680 DIRECTIVE_START, 681 directive, 682 DIRECTIVE_END)); 683 } 684 685 int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0; 686 while (directive.charAt(t1) == ' ') { 687 t1++; 688 } 689 String argument = null; 690 if (directive.startsWith("cms", t1)) { 691 if (LOG.isDebugEnabled()) { 692 LOG.debug(Messages.get().getBundle().key(Messages.LOG_X_DIRECTIVE_DETECTED_1, "cms")); 693 } 694 t2 = directive.indexOf("file", t1 + 3); 695 t5 = 4; 696 } 697 698 if (t2 > 0) { 699 String sub = directive.substring(t2 + t5); 700 char c1 = sub.charAt(t3); 701 while ((c1 == ' ') || (c1 == '=') || (c1 == '"')) { 702 c1 = sub.charAt(++t3); 703 } 704 t4 = t3; 705 while (c1 != '"') { 706 c1 = sub.charAt(++t4); 707 } 708 if (t4 > t3) { 709 argument = sub.substring(t3, t4); 710 } 711 if (LOG.isDebugEnabled()) { 712 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DIRECTIVE_ARG_1, argument)); 713 } 714 } 715 716 if (argument != null) { 717 String jspname = updateJsp(argument, controller, includes); 719 if (jspname != null) { 720 directive = jspname; 721 if (LOG.isDebugEnabled()) { 722 LOG.debug(Messages.get().getBundle().key( 723 Messages.LOG_DIRECTIVE_CHANGED_3, 724 DIRECTIVE_START, 725 directive, 726 DIRECTIVE_END)); 727 } 728 } 729 buf.append(content.substring(p0, i1)); 731 buf.append(directive); 732 p0 = i2 + elen; 733 i1 = content.indexOf(DIRECTIVE_START, p0); 734 } else { 735 buf.append(content.substring(p0, i1 + slen)); 737 buf.append(directive); 738 p0 = i2; 739 i1 = content.indexOf(DIRECTIVE_START, p0); 740 } 741 } 742 } 743 if (i2 > 0) { 744 buf.append(content.substring(p0, content.length())); 746 content = buf.toString(); 747 } 748 return content; 749 } 750 751 761 private String parseJspEncoding(String content, String encoding, boolean isHardInclude) { 762 763 int i1 = content.indexOf(DIRECTIVE_START); 765 if (i1 < 0) { 766 if (isHardInclude) { 768 return content; 769 } 770 } 771 772 StringBuffer buf = new StringBuffer (content.length() + 64); 773 int p0 = 0, i2 = 0, slen = DIRECTIVE_START.length(); 774 boolean found = false; 775 776 if (i1 < 0) { 777 buf.append(content); 779 } 780 781 while (i1 >= 0) { 782 i2 = content.indexOf(DIRECTIVE_END, i1 + slen); 784 if (i2 < 0) { 785 return content; 787 } else if (i2 > i1) { 788 String directive = content.substring(i1 + slen, i2); 789 if (LOG.isDebugEnabled()) { 790 LOG.debug(Messages.get().getBundle().key( 791 Messages.LOG_DIRECTIVE_DETECTED_3, 792 DIRECTIVE_START, 793 directive, 794 DIRECTIVE_END)); 795 } 796 797 int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0; 798 while (directive.charAt(t1) == ' ') { 799 t1++; 800 } 801 String argument = null; 802 if (directive.startsWith("page", t1)) { 803 if (LOG.isDebugEnabled()) { 804 LOG.debug(Messages.get().getBundle().key(Messages.LOG_X_DIRECTIVE_DETECTED_1, "page")); 805 } 806 t2 = directive.indexOf("pageEncoding", t1 + 4); 807 t5 = 12; 808 if (t2 > 0) { 809 found = true; 810 } 811 } 812 813 if (t2 > 0) { 814 String sub = directive.substring(t2 + t5); 815 char c1 = sub.charAt(t3); 816 while ((c1 == ' ') || (c1 == '=') || (c1 == '"')) { 817 c1 = sub.charAt(++t3); 818 } 819 t4 = t3; 820 while (c1 != '"') { 821 c1 = sub.charAt(++t4); 822 } 823 if (t4 > t3) { 824 argument = sub.substring(t3, t4); 825 } 826 if (LOG.isDebugEnabled()) { 827 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DIRECTIVE_ARG_1, argument)); 828 } 829 } 830 831 if (argument != null) { 832 String pre = directive.substring(0, t2 + t3 + t5); 834 String suf = directive.substring(t2 + t3 + t5 + argument.length()); 835 directive = pre + encoding + suf; 837 if (LOG.isDebugEnabled()) { 838 LOG.debug(Messages.get().getBundle().key( 839 Messages.LOG_DIRECTIVE_CHANGED_3, 840 DIRECTIVE_START, 841 directive, 842 DIRECTIVE_END)); 843 } 844 } 845 846 buf.append(content.substring(p0, i1 + slen)); 847 buf.append(directive); 848 p0 = i2; 849 i1 = content.indexOf(DIRECTIVE_START, p0); 850 } 851 } 852 if (i2 > 0) { 853 buf.append(content.substring(p0, content.length())); 855 } 856 if (found) { 857 content = buf.toString(); 858 } else if (!isHardInclude) { 859 StringBuffer buf2 = new StringBuffer (buf.length() + 32); 865 buf2.append("<%@ page pageEncoding=\""); 866 buf2.append(encoding); 867 buf2.append("\" %>"); 868 buf2.append(buf); 869 content = buf2.toString(); 870 } 871 return content; 872 } 873 874 883 private String parseJspIncludes(String content, CmsFlexController controller, Set includes) { 884 885 int i1 = content.indexOf(DIRECTIVE_START); 887 if (i1 < 0) { 888 return content; 890 } 891 892 StringBuffer buf = new StringBuffer (content.length()); 893 int p0 = 0, i2 = 0, slen = DIRECTIVE_START.length(); 894 895 while (i1 >= 0) { 896 i2 = content.indexOf(DIRECTIVE_END, i1 + slen); 898 if (i2 < 0) { 899 return content; 901 } else if (i2 > i1) { 902 String directive = content.substring(i1 + slen, i2); 903 if (LOG.isDebugEnabled()) { 904 LOG.debug(Messages.get().getBundle().key( 905 Messages.LOG_DIRECTIVE_DETECTED_3, 906 DIRECTIVE_START, 907 directive, 908 DIRECTIVE_END)); 909 } 910 911 int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0; 912 while (directive.charAt(t1) == ' ') { 913 t1++; 914 } 915 String argument = null; 916 if (directive.startsWith("include", t1)) { 917 if (LOG.isDebugEnabled()) { 918 LOG.debug(Messages.get().getBundle().key(Messages.LOG_X_DIRECTIVE_DETECTED_1, "include")); 919 } 920 t2 = directive.indexOf("file", t1 + 7); 921 t5 = 6; 922 } else if (directive.startsWith("page", t1)) { 923 if (LOG.isDebugEnabled()) { 924 LOG.debug(Messages.get().getBundle().key(Messages.LOG_X_DIRECTIVE_DETECTED_1, "page")); 925 } 926 t2 = directive.indexOf("errorPage", t1 + 4); 927 t5 = 11; 928 } 929 930 if (t2 > 0) { 931 String sub = directive.substring(t2 + t5); 932 char c1 = sub.charAt(t3); 933 while ((c1 == ' ') || (c1 == '=') || (c1 == '"')) { 934 c1 = sub.charAt(++t3); 935 } 936 t4 = t3; 937 while (c1 != '"') { 938 c1 = sub.charAt(++t4); 939 } 940 if (t4 > t3) { 941 argument = sub.substring(t3, t4); 942 } 943 if (LOG.isDebugEnabled()) { 944 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DIRECTIVE_ARG_1, argument)); 945 } 946 } 947 948 if (argument != null) { 949 String pre = directive.substring(0, t2 + t3 + t5); 951 String suf = directive.substring(t2 + t3 + t5 + argument.length()); 952 String jspname = updateJsp(argument, controller, includes); 954 if (jspname != null) { 955 directive = pre + jspname + suf; 957 if (LOG.isDebugEnabled()) { 958 LOG.debug(Messages.get().getBundle().key( 959 Messages.LOG_DIRECTIVE_CHANGED_3, 960 DIRECTIVE_START, 961 directive, 962 DIRECTIVE_END)); 963 } 964 } 965 } 966 967 buf.append(content.substring(p0, i1 + slen)); 968 buf.append(directive); 969 p0 = i2; 970 i1 = content.indexOf(DIRECTIVE_START, p0); 971 } 972 } 973 if (i2 > 0) { 974 buf.append(content.substring(p0, content.length())); 976 content = buf.toString(); 977 } 978 return content; 979 } 980 981 1000 private String updateJsp(CmsResource resource, CmsFlexController controller, Set updates) 1001 throws IOException , ServletException , CmsLoaderException { 1002 1003 String jspVfsName = resource.getRootPath(); 1004 String extension; 1005 boolean isHardInclude; 1006 int loaderId = OpenCms.getResourceManager().getResourceType(resource.getTypeId()).getLoaderId(); 1007 if ((loaderId == CmsJspLoader.RESOURCE_LOADER_ID) && (!jspVfsName.endsWith(JSP_EXTENSION))) { 1008 extension = JSP_EXTENSION; 1010 isHardInclude = false; 1011 } else { 1012 extension = ""; 1014 isHardInclude = (loaderId != CmsJspLoader.RESOURCE_LOADER_ID); 1016 } 1017 1018 String jspTargetName = CmsFileUtil.getRepositoryName( 1019 m_jspWebAppRepository, 1020 jspVfsName + extension, 1021 controller.getCurrentRequest().isOnline()); 1022 1023 if (updates.contains(jspTargetName)) { 1025 return jspTargetName; 1027 } 1028 updates.add(jspTargetName); 1029 1030 String jspPath = CmsFileUtil.getRepositoryName( 1031 m_jspRepository, 1032 jspVfsName + extension, 1033 controller.getCurrentRequest().isOnline()); 1034 1035 File d = new File(jspPath).getParentFile(); 1036 if ((d == null) || (d.exists() && !(d.isDirectory() && d.canRead()))) { 1037 CmsMessageContainer message = Messages.get().container(Messages.LOG_ACCESS_DENIED_1, jspPath); 1038 LOG.error(message.key()); 1039 throw new ServletException (message.key()); 1041 } 1042 1043 if (!d.exists()) { 1044 d.mkdirs(); 1046 } 1047 1048 boolean mustUpdate = false; 1050 File f = new File(jspPath); 1051 if (!f.exists()) { 1052 mustUpdate = true; 1054 1055 File folder = f.getParentFile(); 1057 if (!folder.exists()) { 1058 boolean success = folder.mkdirs(); 1059 if (!success) { 1060 LOG.error(org.opencms.db.Messages.get().getBundle().key( 1061 org.opencms.db.Messages.LOG_CREATE_FOLDER_FAILED_1, 1062 folder.getAbsolutePath())); 1063 } 1064 } 1065 } else if (f.lastModified() <= resource.getDateLastModified()) { 1066 mustUpdate = true; 1068 } else if (controller.getCurrentRequest().isDoRecompile()) { 1069 mustUpdate = true; 1071 } 1072 1073 if (mustUpdate) { 1074 if (LOG.isDebugEnabled()) { 1075 LOG.debug(Messages.get().getBundle().key(Messages.LOG_WRITING_JSP_1, jspTargetName)); 1076 } 1077 byte[] contents; 1078 String encoding; 1079 try { 1080 CmsObject cms = controller.getCmsObject(); 1081 contents = CmsFile.upgrade(resource, cms).getContents(); 1082 encoding = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue(); 1084 if (encoding == null) { 1085 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 1086 } else { 1087 encoding = CmsEncoder.lookupEncoding(encoding.trim(), encoding); 1088 } 1089 } catch (CmsException e) { 1090 controller.setThrowable(e, jspVfsName); 1091 throw new ServletException ( 1092 Messages.get().getBundle().key(Messages.ERR_LOADER_JSP_ACCESS_1, jspVfsName), 1093 e); 1094 } 1095 1096 try { 1097 contents = parseJsp(contents, encoding, controller, updates, isHardInclude); 1099 synchronized (this) { 1101 FileOutputStream fs = new FileOutputStream (f); 1103 fs.write(contents); 1104 fs.close(); 1105 } 1106 contents = null; 1107 1108 if (LOG.isInfoEnabled()) { 1109 LOG.info(Messages.get().getBundle().key(Messages.LOG_UPDATED_JSP_2, jspTargetName, jspVfsName)); 1110 } 1111 } catch (FileNotFoundException e) { 1112 throw new ServletException ( 1113 Messages.get().getBundle().key(Messages.ERR_LOADER_JSP_WRITE_1, f.getName()), 1114 e); 1115 } 1116 } 1117 1118 controller.updateDates(f.lastModified(), CmsResource.DATE_EXPIRED_DEFAULT); 1120 1121 return jspTargetName; 1122 } 1123 1124 1134 private String updateJsp(String vfsName, CmsFlexController controller, Set includes) { 1135 1136 String jspVfsName = CmsLinkManager.getAbsoluteUri(vfsName, controller.getCurrentRequest().getElementRootPath()); 1137 if (LOG.isDebugEnabled()) { 1138 LOG.debug(Messages.get().getBundle().key(Messages.LOG_UPDATE_JSP_1, jspVfsName)); 1139 } 1140 String jspRfsName = null; 1141 try { 1142 CmsObject cms = OpenCms.initCmsObject(controller.getCmsObject()); 1144 cms.getRequestContext().setSiteRoot(""); 1145 CmsResource includeResource = cms.readResource(jspVfsName); 1146 jspRfsName = updateJsp(includeResource, controller, includes); 1148 if (LOG.isDebugEnabled()) { 1149 LOG.debug(Messages.get().getBundle().key(Messages.LOG_NAME_REAL_FS_1, jspRfsName)); 1150 } 1151 } catch (Exception e) { 1152 jspRfsName = null; 1153 if (LOG.isDebugEnabled()) { 1154 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ERR_UPDATE_1, jspVfsName), e); 1155 } 1156 } 1157 return jspRfsName; 1158 } 1159} | Popular Tags |