001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
034     * @author Mika Koivisto
035     */
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    }