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 return url;
046 }
047
048 String servletContextName = name.substring(0, pos);
049
050 String servletContextPath = ContextPathUtil.getContextPath(
051 StringPool.SLASH + servletContextName);
052
053 String contextPath = PortalUtil.getPathContext();
054
055 String proxyPath = PortalUtil.getPathProxy();
056
057 if (Validator.isNotNull(proxyPath)) {
058 contextPath = contextPath.substring(proxyPath.length());
059 }
060
061 if (Validator.isNull(servletContextName) ||
062 servletContextPath.equals(contextPath)) {
063
064 servletContextName = contextPath;
065 }
066
067 ServletContext servletContext = ServletContextPool.get(
068 servletContextName);
069
070 if (servletContext != null) {
071 String templateName = name.substring(
072 pos + TemplateConstants.SERVLET_SEPARATOR.length());
073
074 if (_log.isDebugEnabled()) {
075 _log.debug(
076 name + " is associated with the servlet context " +
077 servletContextName + " " + servletContext);
078 }
079
080 url = servletContext.getResource(templateName);
081
082 if ((url == null) && templateName.endsWith("/init_custom.ftl")) {
083 if (_log.isWarnEnabled()) {
084 _log.warn("The template " + name + " should be created");
085 }
086
087 String portalServletContextName = PortalUtil.getPathContext();
088
089 ServletContext portalServletContext = ServletContextPool.get(
090 portalServletContextName);
091
092 url = portalServletContext.getResource(
093 "/html/themes/_unstyled/template/init_custom.ftl");
094 }
095 }
096 else {
097 _log.error(
098 name + " is not valid because " + servletContextName +
099 " does not map to a servlet context");
100 }
101
102 return url;
103 }
104
105 private static Log _log = LogFactoryUtil.getLog(
106 FreeMarkerServletResourceParser.class);
107
108 }