001
014
015 package com.liferay.portal.freemarker;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.servlet.ServletContextPool;
020 import com.liferay.portal.kernel.template.TemplateConstants;
021 import com.liferay.portal.kernel.util.ContextPathUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.template.URLResourceParser;
025 import com.liferay.portal.util.PortalUtil;
026
027 import java.io.IOException;
028
029 import java.net.URL;
030
031 import javax.servlet.ServletContext;
032
033
036 public class FreeMarkerServletResourceParser extends URLResourceParser {
037
038 @Override
039 public URL getURL(String name) throws IOException {
040 URL url = null;
041
042 int pos = name.indexOf(TemplateConstants.SERVLET_SEPARATOR);
043
044 if (pos != -1) {
045 String servletContextName = name.substring(0, pos);
046
047 String servletContextPath = ContextPathUtil.getContextPath(
048 StringPool.SLASH + servletContextName);
049
050 String contextPath = PortalUtil.getPathContext();
051
052 if (Validator.isNull(servletContextName) ||
053 servletContextPath.equals(contextPath)) {
054
055 servletContextName = contextPath;
056 }
057
058 ServletContext servletContext = ServletContextPool.get(
059 servletContextName);
060
061 if (servletContext != null) {
062 String templateName = name.substring(
063 pos + TemplateConstants.SERVLET_SEPARATOR.length());
064
065 if (_log.isDebugEnabled()) {
066 _log.debug(
067 name + " is associated with the servlet context " +
068 servletContextName + " " + servletContext);
069 }
070
071 url = servletContext.getResource(templateName);
072
073 if ((url == null) &&
074 templateName.endsWith("/init_custom.ftl")) {
075
076 if (_log.isWarnEnabled()) {
077 _log.warn(
078 "The template " + name + " should be created");
079 }
080
081 String portalServletContextName =
082 PortalUtil.getPathContext();
083
084 ServletContext portalServletContext =
085 ServletContextPool.get(portalServletContextName);
086
087 url = portalServletContext.getResource(
088 "/html/themes/_unstyled/template/init_custom.ftl");
089 }
090 }
091 else {
092 _log.error(
093 name + " is not valid because " + servletContextName +
094 " does not map to a servlet context");
095 }
096 }
097
098 return url;
099 }
100
101 private static Log _log = LogFactoryUtil.getLog(
102 FreeMarkerServletResourceParser.class);
103
104 }