1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.filters.minifier;
24  
25  import com.liferay.portal.kernel.configuration.Filter;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.servlet.BrowserSniffer;
29  import com.liferay.portal.kernel.servlet.ServletContextUtil;
30  import com.liferay.portal.kernel.util.ArrayUtil;
31  import com.liferay.portal.kernel.util.ContentTypes;
32  import com.liferay.portal.kernel.util.FileUtil;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ParamUtil;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.servlet.filters.BasePortalFilter;
39  import com.liferay.portal.util.MinifierUtil;
40  import com.liferay.portal.util.PropsKeys;
41  import com.liferay.portal.util.PropsUtil;
42  import com.liferay.portal.util.PropsValues;
43  import com.liferay.util.SystemProperties;
44  import com.liferay.util.servlet.ServletResponseUtil;
45  import com.liferay.util.servlet.filters.CacheResponse;
46  import com.liferay.util.servlet.filters.CacheResponseUtil;
47  
48  import java.io.File;
49  import java.io.IOException;
50  
51  import java.util.regex.Matcher;
52  import java.util.regex.Pattern;
53  
54  import javax.servlet.FilterChain;
55  import javax.servlet.FilterConfig;
56  import javax.servlet.ServletContext;
57  import javax.servlet.ServletException;
58  import javax.servlet.http.HttpServletRequest;
59  import javax.servlet.http.HttpServletResponse;
60  
61  /**
62   * <a href="MinifierFilter.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   *
66   */
67  public class MinifierFilter extends BasePortalFilter {
68  
69      public void init(FilterConfig filterConfig) throws ServletException {
70          super.init(filterConfig);
71  
72          _servletContext = filterConfig.getServletContext();
73          _servletContextName = GetterUtil.getString(
74              _servletContext.getServletContextName());
75  
76          if (Validator.isNull(_servletContextName)) {
77              _tempDir += "/portal";
78          }
79      }
80  
81      protected String aggregateCss(String dir, String content)
82          throws IOException {
83  
84          StringBuilder sb = new StringBuilder(content.length());
85  
86          int pos = 0;
87  
88          while (true) {
89              int x = content.indexOf(_CSS_IMPORT_BEGIN, pos);
90              int y = content.indexOf(
91                  _CSS_IMPORT_END, x + _CSS_IMPORT_BEGIN.length());
92  
93              if ((x == -1) || (y == -1)) {
94                  sb.append(content.substring(pos, content.length()));
95  
96                  break;
97              }
98              else {
99                  sb.append(content.substring(pos, x));
100 
101                 String importFile = content.substring(
102                     x + _CSS_IMPORT_BEGIN.length(), y);
103 
104                 String importContent = FileUtil.read(
105                     dir + StringPool.SLASH + importFile);
106 
107                 String importFilePath = StringPool.BLANK;
108 
109                 if (importFile.lastIndexOf(StringPool.SLASH) != -1) {
110                     importFilePath = StringPool.SLASH + importFile.substring(
111                         0, importFile.lastIndexOf(StringPool.SLASH) + 1);
112                 }
113 
114                 importContent = aggregateCss(
115                     dir + importFilePath, importContent);
116 
117                 int importDepth = StringUtil.count(
118                     importFile, StringPool.SLASH);
119 
120                 // LEP-7540
121 
122                 String relativePath = StringPool.BLANK;
123 
124                 for (int i = 0; i < importDepth; i++) {
125                     relativePath += "../";
126                 }
127 
128                 importContent = StringUtil.replace(
129                     importContent,
130                     new String[] {
131                         "url('" + relativePath,
132                         "url(\"" + relativePath,
133                         "url(" + relativePath
134                     },
135                     new String[] {
136                         "url('[$TEMP_RELATIVE_PATH$]",
137                         "url(\"[$TEMP_RELATIVE_PATH$]",
138                         "url([$TEMP_RELATIVE_PATH$]"
139                     });
140 
141                 importContent = StringUtil.replace(
142                     importContent, "[$TEMP_RELATIVE_PATH$]", StringPool.BLANK);
143 
144                 sb.append(importContent);
145 
146                 pos = y + _CSS_IMPORT_END.length();
147             }
148         }
149 
150         return sb.toString();
151     }
152 
153     protected String getMinifiedBundleContent(
154             HttpServletRequest request, HttpServletResponse response)
155         throws IOException {
156 
157         String minifierType = ParamUtil.getString(request, "minifierType");
158         String minifierBundleId = ParamUtil.getString(
159             request, "minifierBundleId");
160 
161         if (Validator.isNull(minifierType) ||
162             Validator.isNull(minifierBundleId) ||
163             !ArrayUtil.contains(
164                 PropsValues.JAVASCRIPT_BUNDLE_IDS, minifierBundleId)) {
165 
166             return null;
167         }
168 
169         String minifierBundleDir = PropsUtil.get(
170             PropsKeys.JAVASCRIPT_BUNDLE_DIR, new Filter(minifierBundleId));
171 
172         String bundleDirRealPath = ServletContextUtil.getRealPath(
173             _servletContext, minifierBundleDir);
174 
175         if (bundleDirRealPath == null) {
176             return null;
177         }
178 
179         String cacheFileName = _tempDir + request.getRequestURI();
180 
181         String queryString = request.getQueryString();
182 
183         if (queryString != null) {
184             cacheFileName += _QUESTION_SEPARATOR + queryString;
185         }
186 
187         String[] fileNames = PropsUtil.getArray(minifierBundleId);
188 
189         File cacheFile = new File(cacheFileName);
190 
191         if (cacheFile.exists()) {
192             boolean staleCache = false;
193 
194             for (String fileName : fileNames) {
195                 File file = new File(
196                     bundleDirRealPath + StringPool.SLASH + fileName);
197 
198                 if (file.lastModified() > cacheFile.lastModified()) {
199                     staleCache = true;
200 
201                     break;
202                 }
203             }
204 
205             if (!staleCache) {
206                 response.setContentType(ContentTypes.TEXT_JAVASCRIPT);
207 
208                 return FileUtil.read(cacheFile);
209             }
210         }
211 
212         if (_log.isInfoEnabled()) {
213             _log.info("Minifying JavaScript bundle " + minifierBundleId);
214         }
215 
216         StringBuilder sb = new StringBuilder();
217 
218         for (String fileName : fileNames) {
219             String content = FileUtil.read(
220                 bundleDirRealPath + StringPool.SLASH + fileName);
221 
222             sb.append(content);
223             sb.append(StringPool.NEW_LINE);
224         }
225 
226         String minifiedContent = minifyJavaScript(sb.toString());
227 
228         response.setContentType(ContentTypes.TEXT_JAVASCRIPT);
229 
230         FileUtil.write(cacheFile, minifiedContent);
231 
232         return minifiedContent;
233     }
234 
235     protected String getMinifiedContent(
236             HttpServletRequest request, HttpServletResponse response,
237             FilterChain filterChain)
238         throws IOException, ServletException {
239 
240         String minifierType = ParamUtil.getString(request, "minifierType");
241         String minifierBundleId = ParamUtil.getString(
242             request, "minifierBundleId");
243         String minifierBundleDir = ParamUtil.getString(
244             request, "minifierBundleDir");
245 
246         if (Validator.isNull(minifierType) ||
247             Validator.isNotNull(minifierBundleId) ||
248             Validator.isNotNull(minifierBundleDir)) {
249 
250             return null;
251         }
252 
253         String requestURI = request.getRequestURI();
254 
255         String realPath = ServletContextUtil.getRealPath(
256             _servletContext, requestURI);
257 
258         if (realPath == null) {
259             return null;
260         }
261 
262         realPath = StringUtil.replace(
263             realPath, StringPool.BACK_SLASH, StringPool.SLASH);
264 
265         File file = new File(realPath);
266 
267         if (!file.exists()) {
268 
269             // Tomcat incorrectly returns the a real path to a resource that
270             // exists in another web application. For example, it returns
271             // ".../webapps/abc-theme/abc-theme/css/main.css" instead of
272             // ".../webapps/abc-theme/css/main.css".
273 
274             if (Validator.isNotNull(_servletContextName)) {
275                 realPath = StringUtil.replaceFirst(
276                     realPath, StringPool.SLASH + _servletContextName,
277                     StringPool.BLANK);
278 
279                 file = new File(realPath);
280             }
281         }
282 
283         if (!file.exists()) {
284             return null;
285         }
286 
287         String minifiedContent = null;
288 
289         String cacheCommonFileName = _tempDir + requestURI;
290 
291         String queryString = request.getQueryString();
292 
293         if (queryString != null) {
294             cacheCommonFileName += _QUESTION_SEPARATOR + queryString;
295         }
296 
297         File cacheContentTypeFile = new File(
298             cacheCommonFileName + "_E_CONTENT_TYPE");
299         File cacheDataFile = new File(cacheCommonFileName + "_E_DATA");
300 
301         if ((cacheDataFile.exists()) &&
302             (cacheDataFile.lastModified() >= file.lastModified())) {
303 
304             minifiedContent = FileUtil.read(cacheDataFile);
305 
306             if (cacheContentTypeFile.exists()) {
307                 String contentType = FileUtil.read(cacheContentTypeFile);
308 
309                 response.setContentType(contentType);
310             }
311         }
312         else {
313             if (realPath.endsWith(_CSS_EXTENSION)) {
314                 if (_log.isInfoEnabled()) {
315                     _log.info("Minifying CSS " + file);
316                 }
317 
318                 minifiedContent = minifyCss(request, file);
319 
320                 response.setContentType(ContentTypes.TEXT_CSS);
321 
322                 FileUtil.write(cacheContentTypeFile, ContentTypes.TEXT_CSS);
323             }
324             else if (realPath.endsWith(_JAVASCRIPT_EXTENSION)) {
325                 if (_log.isInfoEnabled()) {
326                     _log.info("Minifying JavaScript " + file);
327                 }
328 
329                 minifiedContent = minifyJavaScript(file);
330 
331                 response.setContentType(ContentTypes.TEXT_JAVASCRIPT);
332 
333                 FileUtil.write(
334                     cacheContentTypeFile, ContentTypes.TEXT_JAVASCRIPT);
335             }
336             else if (realPath.endsWith(_JSP_EXTENSION)) {
337                 if (_log.isInfoEnabled()) {
338                     _log.info("Minifying JSP " + file);
339                 }
340 
341                 CacheResponse cacheResponse = new CacheResponse(
342                     response, StringPool.UTF8);
343 
344                 processFilter(
345                     MinifierFilter.class, request, cacheResponse, filterChain);
346 
347                 CacheResponseUtil.addHeaders(
348                     response, cacheResponse.getHeaders());
349 
350                 response.setContentType(cacheResponse.getContentType());
351 
352                 minifiedContent = new String(
353                     cacheResponse.getData(), StringPool.UTF8);
354 
355                 if (minifierType.equals("css")) {
356                     minifiedContent = minifyCss(request, minifiedContent);
357                 }
358                 else if (minifierType.equals("js")) {
359                     minifiedContent = minifyJavaScript(minifiedContent);
360                 }
361 
362                 FileUtil.write(
363                     cacheContentTypeFile, cacheResponse.getContentType());
364             }
365             else {
366                 return null;
367             }
368 
369             FileUtil.write(cacheDataFile, minifiedContent);
370         }
371 
372         return minifiedContent;
373     }
374 
375     protected String minifyCss(HttpServletRequest request, File file)
376         throws IOException {
377 
378         String content = FileUtil.read(file);
379 
380         content = aggregateCss(file.getParent(), content);
381 
382         return minifyCss(request, content);
383     }
384 
385     protected String minifyCss(HttpServletRequest request, String content) {
386         String browserId = ParamUtil.getString(request, "browserId");
387 
388         if (!browserId.equals(BrowserSniffer.BROWSER_ID_IE)) {
389             Matcher matcher = _pattern.matcher(content);
390 
391             content = matcher.replaceAll(StringPool.BLANK);
392         }
393 
394         return MinifierUtil.minifyCss(content);
395     }
396 
397     protected String minifyJavaScript(File file) throws IOException {
398         String content = FileUtil.read(file);
399 
400         return minifyJavaScript(content);
401     }
402 
403     protected String minifyJavaScript(String content) {
404         return MinifierUtil.minifyJavaScript(content);
405     }
406 
407     protected void processFilter(
408             HttpServletRequest request, HttpServletResponse response,
409             FilterChain filterChain)
410         throws IOException, ServletException {
411 
412         String minifiedContent = getMinifiedContent(
413             request, response, filterChain);
414 
415         if (Validator.isNull(minifiedContent)) {
416             minifiedContent = getMinifiedBundleContent(request, response);
417         }
418 
419         if (Validator.isNull(minifiedContent)) {
420             processFilter(MinifierFilter.class, request, response, filterChain);
421         }
422         else {
423             ServletResponseUtil.write(response, minifiedContent);
424         }
425     }
426 
427     private static final String _CSS_IMPORT_BEGIN = "@import url(";
428 
429     private static final String _CSS_IMPORT_END = ");";
430 
431     private static final String _CSS_EXTENSION = ".css";
432 
433     private static final String _JAVASCRIPT_EXTENSION = ".js";
434 
435     private static final String _JSP_EXTENSION = ".jsp";
436 
437     private static final String _QUESTION_SEPARATOR = "_Q_";
438 
439     private static final String _TEMP_DIR =
440         SystemProperties.get(SystemProperties.TMP_DIR) + "/liferay/minifier";
441 
442     private static Log _log = LogFactoryUtil.getLog(MinifierFilter.class);
443 
444     private static Pattern _pattern = Pattern.compile(
445         "^(\\.ie|\\.js\\.ie)([^}]*)}", Pattern.MULTILINE);
446 
447     private ServletContext _servletContext;
448     private String _servletContextName;
449     private String _tempDir = _TEMP_DIR;
450 
451 }