Liferay MinifierFilter的研究

2020年2月8日10:13:55
评论

大家都知道,在Web应用程序中,为了节省网络开销,往往吧多个小的js文件整合成一个大的js文件,吧多个小的css文件整合成一个大的js文件,这样原本N次小文件的请求就可以合并成单次的网络请求。最典型的做这件事情的工具是大名鼎鼎的yui-compressor.

其实在Liferay中,我们为了达到合并css,js的目的,用了不同于yui-compressor的方法,这就是我们的主角 MinifierFilter.

既然是Filter,那么它肯定有filter-mapping,我们轻易的在liferay-web.xml中找到了Filter的定义和Filter的mapping.

 
 
  1. ... 
  2. <filter> 
  3.         <filter-name>Minifier Filter</filter-name> 
  4.         <filter-class>com.liferay.portal.servlet.filters.minifier.MinifierFilter</filter-class> 
  5.     </filter> 
  6. <filter> <filter-name>Minifier Filter - JSP</filter-name> <filter-class>com.liferay.portal.servlet.filters.minifier.MinifierFilter</filter-class> <init-param> <param-name>url-regex-pattern</param-name> <param-value>.+/(aui_lang|barebone|css|everything|main)\.jsp</param-value> </init-param> </filter>
  7.  
  8. ... 
  9. <filter-mapping> 
  10.         <filter-name>Minifier Filter</filter-name> 
  11.         <url-pattern>*.css</url-pattern> 
  12.     </filter-mapping> 
  13.     <filter-mapping> 
  14.         <filter-name>Minifier Filter</filter-name> 
  15.         <url-pattern>*.js</url-pattern> 
  16.     </filter-mapping> 
  17. <filter-mapping> <filter-name>Minifier Filter - JSP</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
  18. ... 

所以,我们可以看到,当客户端对Liferay服务器上请求任意css或者javascript资源时候,都会被这个MinifierFilter所过滤,我们现在就来看下庐山真面目。

 

因为MinifierFilter最终实现了Filter接口,而doFilter方法在父类的父类BaseFilter中定义,这个doFilter仅仅是调用processFilter()方法,

 
 
  1. public void doFilter( 
  2.             ServletRequest servletRequest, ServletResponse servletResponse, 
  3.             FilterChain filterChain) 
  4.         throws IOException, ServletException { 
  5.  
  6.         try { 
  7.             HttpServletRequest request = (HttpServletRequest)servletRequest; 
  8.             HttpServletResponse response = (HttpServletResponse)servletResponse; 
  9.  
  10.             processFilter(request, response, filterChain); 
  11.         } 
  12.         catch (IOException ioe) { 
  13.             throw ioe; 
  14.         } 
  15.         catch (ServletException se) { 
  16.             throw se; 
  17.         } 

所以这就是我们的入口:

 
 
  1. protected void processFilter( 
  2.             HttpServletRequest request, HttpServletResponse response, 
  3.             FilterChain filterChain) 
  4.         throws Exception { 
  5.  
  6.         Object minifiedContent = getMinifiedContent( 
  7.             request, response, filterChain); 
  8.  
  9.         if (minifiedContent == null) { 
  10.             minifiedContent = getMinifiedBundleContent(request, response); 
  11.         } 
  12.  
  13.         if (minifiedContent == null) { 
  14.             processFilter(MinifierFilter.class, request, response, filterChain); 
  15.         } 
  16.         else { 
  17.             if (minifiedContent instanceof File) { 
  18.                 ServletResponseUtil.write(response, (File)minifiedContent); 
  19.             } 
  20.             else if (minifiedContent instanceof String) { 
  21.                 ServletResponseUtil.write(response, (String)minifiedContent); 
  22.             } 
  23.         } 
  24.     } 


首先,它会去执行06-07行的getMinifiedContent()方法,它会调用以下代码:

 
 
  1. protected Object getMinifiedContent( 
  2.             HttpServletRequest request, HttpServletResponse response, 
  3.             FilterChain filterChain) 
  4.         throws Exception { 
  5.  
  6.         .. 
  7.  
  8.        ..
  9.  
  10.         String minifiedContent = null
  11.  
  12.         if (realPath.endsWith(_CSS_EXTENSION)) { 
  13.             if (_log.isInfoEnabled()) { 
  14.                 _log.info("Minifying CSS " + file); 
  15.             } 
  16.  
  17.             minifiedContent = minifyCss(request, response, file); 
  18.  
  19.             response.setContentType(ContentTypes.TEXT_CSS); 
  20.  
  21.             FileUtil.write(cacheContentTypeFile, ContentTypes.TEXT_CSS); 
  22.         } 
  23.         else if (realPath.endsWith(_JAVASCRIPT_EXTENSION)) { 
  24.             if (_log.isInfoEnabled()) { 
  25.                 _log.info("Minifying JavaScript " + file); 
  26.             } 
  27.  
  28.             minifiedContent = minifyJavaScript(file); 
  29.  
  30.             response.setContentType(ContentTypes.TEXT_JAVASCRIPT); 
  31.  
  32.             FileUtil.write(cacheContentTypeFile, ContentTypes.TEXT_JAVASCRIPT); 
  33.         } 
  34.         else if (realPath.endsWith(_JSP_EXTENSION)) { 
  35.             if (_log.isInfoEnabled()) { 
  36.                 _log.info("Minifying JSP " + file); 
  37.             } 
  38.  
  39.             StringServletResponse stringResponse = new StringServletResponse( 
  40.                 response); 
  41.  
  42.             processFilter( 
  43.                 MinifierFilter.class, request, stringResponse, filterChain); 
  44.  
  45.             CacheResponseUtil.setHeaders(response, stringResponse.getHeaders()); 
  46.  
  47.             response.setContentType(stringResponse.getContentType()); 
  48.  
  49.             minifiedContent = stringResponse.getString(); 
  50.  
  51.             if (minifierType.equals("css")) { 
  52.                 minifiedContent = minifyCss( 
  53.                     request, response, realPath, minifiedContent); 
  54.             } 
  55.             else if (minifierType.equals("js")) { 
  56.                 minifiedContent = minifyJavaScript(minifiedContent); 
  57.             } 
  58.  
  59.             FileUtil.write( 
  60.                 cacheContentTypeFile, stringResponse.getContentType()); 
  61.         } 
  62.         else { 
  63.             return null
  64.         } 
  65.  
  66.         FileUtil.write(cacheDataFile, minifiedContent); 
  67.  
  68.         return minifiedContent; 
  69.     } 

企鹅博客
  • 本文由 发表于 2020年2月8日10:13:55
  • 转载请务必保留本文链接:https://www.qieseo.com/152768.html

发表评论