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.kernel.servlet;
24  
25  import com.liferay.portal.kernel.util.Http;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.io.File;
29  
30  import java.util.Set;
31  
32  import javax.servlet.ServletContext;
33  
34  /**
35   * <a href="ServletContextUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class ServletContextUtil {
41  
42      public static long getLastModified(ServletContext servletContext) {
43          return getLastModified(servletContext, StringPool.SLASH);
44      }
45  
46      public static long getLastModified(
47          ServletContext servletContext, String resourcePath) {
48  
49          return getLastModified(servletContext, resourcePath, false);
50      }
51  
52      public static long getLastModified(
53          ServletContext servletContext, String resourcePath, boolean cache) {
54  
55          if (resourcePath.startsWith(Http.HTTP) ||
56              resourcePath.startsWith(Http.HTTPS)) {
57  
58              int pos = resourcePath.indexOf(Http.PROTOCOL_DELIMITER);
59  
60              resourcePath = resourcePath.substring(pos + 3);
61          }
62  
63          int pos = resourcePath.indexOf(StringPool.SLASH);
64  
65          if (pos != -1) {
66              resourcePath = resourcePath.substring(pos);
67          }
68  
69          if (cache) {
70              Long lastModified = (Long)servletContext.getAttribute(
71                  ServletContextUtil.class.getName() + StringPool.PERIOD +
72                      resourcePath);
73  
74              if (lastModified != null) {
75                  return lastModified.longValue();
76              }
77          }
78  
79          long lastModified = 0;
80  
81          Set<String> resourcePaths = servletContext.getResourcePaths(
82              resourcePath);
83  
84          if (resourcePaths != null) {
85              for (String curResourcePath : resourcePaths) {
86                  if (curResourcePath.endsWith(StringPool.SLASH)) {
87                      long curLastModified = getLastModified(
88                          servletContext, curResourcePath);
89  
90                      if (curLastModified > lastModified) {
91                          lastModified = curLastModified;
92                      }
93                  }
94                  else {
95                      File file = new File(
96                          servletContext.getRealPath(curResourcePath));
97  
98                      if (file.lastModified() > lastModified) {
99                          lastModified = file.lastModified();
100                     }
101                 }
102             }
103         }
104 
105         if (cache) {
106             servletContext.setAttribute(
107                 ServletContextUtil.class.getName() + StringPool.PERIOD +
108                     resourcePath,
109                 new Long(lastModified));
110         }
111 
112         return lastModified;
113     }
114 
115 }