1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared.resource; |
20 | |
|
21 | |
import java.io.File; |
22 | |
import java.io.IOException; |
23 | |
import java.net.JarURLConnection; |
24 | |
import java.net.URL; |
25 | |
import java.net.URLConnection; |
26 | |
import java.text.ParseException; |
27 | |
import java.text.SimpleDateFormat; |
28 | |
import java.util.Date; |
29 | |
import java.util.Locale; |
30 | |
import java.util.TimeZone; |
31 | |
|
32 | 0 | public class ResourceLoaderUtils |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
private static final String HTTP_RESPONSE_DATE_HEADER = |
37 | |
"EEE, dd MMM yyyy HH:mm:ss zzz"; |
38 | |
|
39 | 0 | 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 | 0 | private static final TimeZone GMT = TimeZone.getTimeZone("GMT"); |
44 | |
|
45 | |
public static String formatDateHeader(long value) |
46 | |
{ |
47 | 0 | SimpleDateFormat format = new SimpleDateFormat( |
48 | |
HTTP_RESPONSE_DATE_HEADER, |
49 | |
Locale.US); |
50 | 0 | format.setTimeZone(GMT); |
51 | 0 | return format.format(new Date(value)); |
52 | |
} |
53 | |
|
54 | |
public static Long parseDateHeader(String value) |
55 | |
{ |
56 | 0 | Date date = null; |
57 | 0 | for (int i = 0; (date == null) && (i < HTTP_REQUEST_DATE_HEADER.length); i++) |
58 | |
{ |
59 | |
try |
60 | |
{ |
61 | 0 | SimpleDateFormat format = new SimpleDateFormat( |
62 | |
HTTP_REQUEST_DATE_HEADER[i], Locale.US); |
63 | 0 | format.setTimeZone(GMT); |
64 | 0 | date = format.parse(value); |
65 | |
} |
66 | 0 | catch (ParseException e) |
67 | |
{ |
68 | |
|
69 | 0 | } |
70 | |
} |
71 | 0 | if (date == null) |
72 | |
{ |
73 | 0 | return null; |
74 | |
} |
75 | 0 | return Long.valueOf(date.getTime()); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
public static long getResourceLastModified(URL url) throws IOException |
80 | |
{ |
81 | 0 | if ("file".equals(url.getProtocol())) |
82 | |
{ |
83 | 0 | String externalForm = url.toExternalForm(); |
84 | |
|
85 | 0 | File file = new File(externalForm.substring(5)); |
86 | |
|
87 | 0 | return file.lastModified(); |
88 | |
} |
89 | |
else |
90 | |
{ |
91 | 0 | return getResourceLastModified(url.openConnection()); |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
|
96 | |
public static long getResourceLastModified(URLConnection connection) throws IOException |
97 | |
{ |
98 | |
long modified; |
99 | 0 | if (connection instanceof JarURLConnection) |
100 | |
{ |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | 0 | URL jarFileUrl = ((JarURLConnection) connection).getJarFileURL(); |
112 | 0 | URLConnection jarFileConnection = jarFileUrl.openConnection(); |
113 | |
|
114 | |
try |
115 | |
{ |
116 | 0 | modified = jarFileConnection.getLastModified(); |
117 | |
} |
118 | |
finally |
119 | |
{ |
120 | 0 | try |
121 | |
{ |
122 | 0 | jarFileConnection.getInputStream().close(); |
123 | |
} |
124 | 0 | catch (Exception exception) |
125 | |
{ |
126 | |
|
127 | 0 | } |
128 | 0 | } |
129 | 0 | } |
130 | |
else |
131 | |
{ |
132 | 0 | modified = connection.getLastModified(); |
133 | |
} |
134 | |
|
135 | 0 | return modified; |
136 | |
} |
137 | |
} |