001    /**
002     * Copyright (c) 2000-present 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.servlet.filters.dynamiccss;
016    
017    import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
018    import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
022    import com.liferay.portal.kernel.servlet.HttpHeaders;
023    import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
024    import com.liferay.portal.kernel.servlet.PortletResourcesUtil;
025    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
026    import com.liferay.portal.kernel.util.CharPool;
027    import com.liferay.portal.kernel.util.ContentTypes;
028    import com.liferay.portal.kernel.util.FileUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.HttpUtil;
031    import com.liferay.portal.kernel.util.JavaConstants;
032    import com.liferay.portal.kernel.util.PortalUtil;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.URLUtil;
036    import com.liferay.portal.servlet.filters.IgnoreModuleRequestFilter;
037    import com.liferay.portal.util.PropsUtil;
038    
039    import java.io.File;
040    
041    import java.net.URL;
042    
043    import javax.servlet.FilterChain;
044    import javax.servlet.FilterConfig;
045    import javax.servlet.ServletContext;
046    import javax.servlet.http.HttpServletRequest;
047    import javax.servlet.http.HttpServletResponse;
048    
049    /**
050     * @author Eduardo Lundgren
051     * @author Raymond Aug??
052     */
053    public class DynamicCSSFilter extends IgnoreModuleRequestFilter {
054    
055            public static final boolean ENABLED = GetterUtil.getBoolean(
056                    PropsUtil.get(DynamicCSSFilter.class.getName()));
057    
058            @Override
059            public void init(FilterConfig filterConfig) {
060                    super.init(filterConfig);
061    
062                    _servletContext = filterConfig.getServletContext();
063    
064                    File tempDir = (File)_servletContext.getAttribute(
065                            JavaConstants.JAVAX_SERVLET_CONTEXT_TEMPDIR);
066    
067                    _tempDir = new File(tempDir, _TEMP_DIR);
068    
069                    _tempDir.mkdirs();
070            }
071    
072            protected String getCacheFileName(HttpServletRequest request) {
073                    CacheKeyGenerator cacheKeyGenerator =
074                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
075                                    DynamicCSSFilter.class.getName());
076    
077                    cacheKeyGenerator.append(HttpUtil.getProtocol(request.isSecure()));
078                    cacheKeyGenerator.append(StringPool.UNDERLINE);
079                    cacheKeyGenerator.append(request.getRequestURI());
080    
081                    String requestURL = String.valueOf(request.getRequestURL());
082    
083                    if (requestURL != null) {
084                            requestURL = HttpUtil.removeParameter(requestURL, "zx");
085    
086                            String queryString = HttpUtil.getQueryString(requestURL);
087    
088                            if (queryString != null) {
089                                    cacheKeyGenerator.append(sterilizeQueryString(queryString));
090                            }
091                    }
092    
093                    if (PortalUtil.isRightToLeft(request)) {
094                            cacheKeyGenerator.append("_rtl");
095                    }
096    
097                    return String.valueOf(cacheKeyGenerator.finish());
098            }
099    
100            protected Object getDynamicContent(
101                            HttpServletRequest request, HttpServletResponse response,
102                            FilterChain filterChain)
103                    throws Exception {
104    
105                    ServletContext servletContext = _servletContext;
106    
107                    String requestPath = getRequestPath(request);
108    
109                    if (requestPath.endsWith(_CSS_EXTENSION) &&
110                            PortalUtil.isRightToLeft(request)) {
111    
112                            int pos = requestPath.lastIndexOf(StringPool.PERIOD);
113    
114                            requestPath =
115                                    requestPath.substring(0, pos) + "_rtl" +
116                                            requestPath.substring(pos);
117                    }
118    
119                    URL resourceURL = _servletContext.getResource(requestPath);
120    
121                    if (resourceURL == null) {
122                            ServletContext resourceServletContext =
123                                    PortalWebResourcesUtil.getPathServletContext(requestPath);
124    
125                            if (resourceServletContext != null) {
126                                    resourceURL = PortalWebResourcesUtil.getResource(
127                                            resourceServletContext, requestPath);
128                            }
129    
130                            if (resourceURL == null) {
131                                    resourceServletContext =
132                                            PortletResourcesUtil.getPathServletContext(requestPath);
133    
134                                    if (resourceServletContext != null) {
135                                            resourceURL = PortletResourcesUtil.getResource(
136                                                    resourceServletContext, requestPath);
137                                    }
138                            }
139    
140                            if (resourceURL == null) {
141                                    return null;
142                            }
143    
144                            servletContext = resourceServletContext;
145                    }
146    
147                    String cacheCommonFileName = getCacheFileName(request);
148    
149                    File cacheContentTypeFile = new File(
150                            _tempDir, cacheCommonFileName + "_E_CONTENT_TYPE");
151                    File cacheDataFile = new File(
152                            _tempDir, cacheCommonFileName + "_E_DATA");
153    
154                    if (cacheDataFile.exists() &&
155                            (cacheDataFile.lastModified() >=
156                                    URLUtil.getLastModifiedTime(resourceURL))) {
157    
158                            if (cacheContentTypeFile.exists()) {
159                                    String contentType = FileUtil.read(cacheContentTypeFile);
160    
161                                    response.setContentType(contentType);
162                            }
163    
164                            return cacheDataFile;
165                    }
166    
167                    String dynamicContent = null;
168    
169                    String content = null;
170    
171                    try {
172                            if (requestPath.endsWith(_CSS_EXTENSION)) {
173                                    if (_log.isInfoEnabled()) {
174                                            _log.info("Replacing tokens on CSS " + requestPath);
175                                    }
176    
177                                    content = StringUtil.read(resourceURL.openStream());
178    
179                                    dynamicContent = DynamicCSSUtil.replaceToken(
180                                            servletContext, request, content);
181    
182                                    response.setContentType(ContentTypes.TEXT_CSS);
183    
184                                    FileUtil.write(cacheContentTypeFile, ContentTypes.TEXT_CSS);
185                            }
186                            else if (requestPath.endsWith(_JSP_EXTENSION)) {
187                                    if (_log.isInfoEnabled()) {
188                                            _log.info(
189                                                    "Replacing tokens on JSP or servlet " + requestPath);
190                                    }
191    
192                                    BufferCacheServletResponse bufferCacheServletResponse =
193                                            new BufferCacheServletResponse(response);
194    
195                                    processFilter(
196                                            DynamicCSSFilter.class.getName(), request,
197                                            bufferCacheServletResponse, filterChain);
198    
199                                    bufferCacheServletResponse.finishResponse(false);
200    
201                                    content = bufferCacheServletResponse.getString();
202    
203                                    dynamicContent = DynamicCSSUtil.replaceToken(
204                                            servletContext, request, content);
205    
206                                    FileUtil.write(
207                                            cacheContentTypeFile,
208                                            bufferCacheServletResponse.getContentType());
209                            }
210                            else {
211                                    return null;
212                            }
213                    }
214                    catch (Exception e) {
215                            _log.error("Unable to replace tokens in CSS " + requestPath, e);
216    
217                            if (_log.isDebugEnabled()) {
218                                    _log.debug(content);
219                            }
220    
221                            response.setHeader(
222                                    HttpHeaders.CACHE_CONTROL,
223                                    HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
224                    }
225    
226                    if (dynamicContent != null) {
227                            FileUtil.write(cacheDataFile, dynamicContent);
228                    }
229                    else {
230                            dynamicContent = content;
231                    }
232    
233                    return dynamicContent;
234            }
235    
236            protected String getRequestPath(HttpServletRequest request) {
237                    String requestPath = request.getRequestURI();
238    
239                    String contextPath = request.getContextPath();
240    
241                    if (!contextPath.equals(StringPool.SLASH)) {
242                            requestPath = requestPath.substring(contextPath.length());
243                    }
244    
245                    return requestPath;
246            }
247    
248            @Override
249            protected boolean isModuleRequest(HttpServletRequest request) {
250                    String requestURI = request.getRequestURI();
251    
252                    if (PortalWebResourcesUtil.hasContextPath(requestURI)) {
253                            return false;
254                    }
255    
256                    return super.isModuleRequest(request);
257            }
258    
259            @Override
260            protected void processFilter(
261                            HttpServletRequest request, HttpServletResponse response,
262                            FilterChain filterChain)
263                    throws Exception {
264    
265                    Object parsedContent = getDynamicContent(
266                            request, response, filterChain);
267    
268                    if (parsedContent == null) {
269                            processFilter(
270                                    DynamicCSSFilter.class.getName(), request, response,
271                                    filterChain);
272                    }
273                    else {
274                            if (parsedContent instanceof File) {
275                                    ServletResponseUtil.write(response, (File)parsedContent);
276                            }
277                            else if (parsedContent instanceof String) {
278                                    ServletResponseUtil.write(response, (String)parsedContent);
279                            }
280                    }
281            }
282    
283            protected String sterilizeQueryString(String queryString) {
284                    return StringUtil.replace(
285                            queryString, new char[] {CharPool.SLASH, CharPool.BACK_SLASH},
286                            new char[] {CharPool.UNDERLINE, CharPool.UNDERLINE});
287            }
288    
289            private static final String _CSS_EXTENSION = ".css";
290    
291            private static final String _JSP_EXTENSION = ".jsp";
292    
293            private static final String _TEMP_DIR = "css";
294    
295            private static final Log _log = LogFactoryUtil.getLog(
296                    DynamicCSSFilter.class);
297    
298            private ServletContext _servletContext;
299            private File _tempDir;
300    
301    }