001    /**
002     * Copyright (c) 2000-2012 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.TemplateResource;
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(TemplateResource.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 + TemplateResource.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    }