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