View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared.resource;
20  
21  import java.io.IOException;
22  import java.net.JarURLConnection;
23  import java.net.URL;
24  import java.net.URLConnection;
25  import java.text.ParseException;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Locale;
29  import java.util.TimeZone;
30  import org.apache.myfaces.shared.util.StringUtils;
31  
32  public class ResourceLoaderUtils
33  {
34      // TODO: In tomcat and jetty it is implemented a Flyweight pattern when converting
35      // date headers. For now it is better keep this stuff simple.
36      private static final String HTTP_RESPONSE_DATE_HEADER =
37          "EEE, dd MMM yyyy HH:mm:ss zzz";
38      
39      private static final String[] HTTP_REQUEST_DATE_HEADER = {
40              "EEE, dd MMM yyyy HH:mm:ss zzz", "EEEEEE, dd-MMM-yy HH:mm:ss zzz",
41              "EEE MMMM d HH:mm:ss yyyy" };
42      
43      private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
44  
45      public static String formatDateHeader(long value)
46      {
47          SimpleDateFormat format = new SimpleDateFormat(
48                  HTTP_RESPONSE_DATE_HEADER,
49                  Locale.US);
50          format.setTimeZone(GMT);
51          return format.format(new Date(value));
52      }
53      
54      public static Long parseDateHeader(String value)
55      {
56          Date date = null;
57          for (int i = 0; (date == null) && (i < HTTP_REQUEST_DATE_HEADER.length); i++)
58          {
59              try
60              {
61                  SimpleDateFormat format = new SimpleDateFormat(
62                          HTTP_REQUEST_DATE_HEADER[i], Locale.US);
63                  format.setTimeZone(GMT);
64                  date = format.parse(value);
65              }
66              catch (ParseException e)
67              {
68                  // all fine
69              }
70          }
71          if (date == null)
72          {
73              return null;
74          }
75          return Long.valueOf(date.getTime());
76      }
77      
78      //Taken from trinidad URLUtils
79      public static long getResourceLastModified(URL url) throws IOException
80      {
81          return getResourceLastModified(url.openConnection());
82      }
83  
84      //Taken from trinidad URLUtils
85      public static long getResourceLastModified(URLConnection connection) throws IOException
86      {
87          long modified;
88          if (connection instanceof JarURLConnection)
89          {
90              // The following hack is required to work-around a JDK bug.
91              // getLastModified() on a JAR entry URL delegates to the actual JAR file
92              // rather than the JAR entry.
93              // This opens internally, and does not close, an input stream to the JAR
94              // file.
95              // In turn, you cannot close it by yourself, because it's internal.
96              // The work-around is to get the modification date of the JAR file
97              // manually,
98              // and then close that connection again.
99  
100             URL jarFileUrl = ((JarURLConnection) connection).getJarFileURL();
101             URLConnection jarFileConnection = jarFileUrl.openConnection();
102 
103             try
104             {
105                 modified = jarFileConnection.getLastModified();
106             }
107             finally
108             {
109                 try
110                 {
111                     jarFileConnection.getInputStream().close();
112                 }
113                 catch (Exception exception)
114                 {
115                     // Ignored
116                 }
117             }
118         }
119         else
120         {
121             modified = connection.getLastModified();
122         }
123 
124         return modified;
125     }
126     
127     public static int getDepth(String path)
128     {
129         int depth = 0;
130         String [] paths = StringUtils.splitShortString(path, '/');
131         if (paths == null)
132         {
133             return 0;
134         }
135         for (String p : paths)
136         {
137             if (p != null && p.length() > 0)
138             {
139                 depth++;
140             }
141         }
142         return depth;
143     }
144 
145     public static boolean isDirectory(String path)
146     {
147         return path.startsWith("/") && path.endsWith("/");
148     }
149     
150 }