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.InputStream; |
22 | |
import java.net.MalformedURLException; |
23 | |
import java.net.URL; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.Set; |
26 | |
import java.util.regex.Pattern; |
27 | |
|
28 | |
import javax.faces.context.FacesContext; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class ExternalContextResourceLoader extends ResourceLoader |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | protected static final Pattern VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*/"); |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | protected static final Pattern RESOURCE_VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*\\..*"); |
50 | |
|
51 | |
public ExternalContextResourceLoader(String prefix) |
52 | |
{ |
53 | 0 | super(prefix); |
54 | 0 | } |
55 | |
|
56 | |
protected Set<String> getResourcePaths(String path) |
57 | |
{ |
58 | 0 | return FacesContext.getCurrentInstance().getExternalContext().getResourcePaths(getPrefix() + '/' + path); |
59 | |
} |
60 | |
|
61 | |
@Override |
62 | |
public String getResourceVersion(String path) |
63 | |
{ |
64 | 0 | String resourceVersion = null; |
65 | 0 | Set<String> resourcePaths = this.getResourcePaths(path); |
66 | 0 | if (getPrefix() != null) |
67 | |
{ |
68 | 0 | path = getPrefix() + '/' + path; |
69 | |
} |
70 | |
|
71 | 0 | if (null != resourcePaths && !resourcePaths.isEmpty()) |
72 | |
{ |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 0 | for (String resourcePath : resourcePaths) |
80 | |
{ |
81 | 0 | String version = ""; |
82 | 0 | if (path.length() < resourcePath.length()) |
83 | |
{ |
84 | 0 | version = resourcePath.substring(path.length()); |
85 | |
} |
86 | |
|
87 | 0 | if (RESOURCE_VERSION_CHECKER.matcher(version).matches()) |
88 | |
{ |
89 | 0 | version = version.substring(1, version.lastIndexOf('.')); |
90 | 0 | if (resourceVersion == null) |
91 | |
{ |
92 | 0 | resourceVersion = version; |
93 | |
} |
94 | 0 | else if (getVersionComparator().compare(resourceVersion, version) < 0) |
95 | |
{ |
96 | 0 | resourceVersion = version; |
97 | |
} |
98 | |
} |
99 | 0 | } |
100 | |
|
101 | 0 | if (resourceVersion == null) |
102 | |
{ |
103 | 0 | resourceVersion = VERSION_INVALID; |
104 | |
} |
105 | |
} |
106 | 0 | return resourceVersion; |
107 | |
} |
108 | |
|
109 | |
@Override |
110 | |
public String getLibraryVersion(String path) |
111 | |
{ |
112 | 0 | String libraryVersion = null; |
113 | 0 | Set<String> libraryPaths = this.getResourcePaths(path); |
114 | 0 | path = getPrefix() + '/' + path; |
115 | 0 | if (null != libraryPaths && !libraryPaths.isEmpty()) |
116 | |
{ |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | 0 | for (Iterator<String> it = libraryPaths.iterator(); it.hasNext();) |
124 | |
{ |
125 | 0 | String libraryPath = it.next(); |
126 | 0 | String version = ""; |
127 | 0 | if (path.length() < libraryPath.length()) |
128 | |
{ |
129 | 0 | version = libraryPath.substring(path.length()); |
130 | |
} |
131 | |
|
132 | 0 | if (VERSION_CHECKER.matcher(version).matches()) |
133 | |
{ |
134 | 0 | version = version.substring(1, version.length() - 1); |
135 | 0 | if (libraryVersion == null) |
136 | |
{ |
137 | 0 | libraryVersion = version; |
138 | |
} |
139 | 0 | else if (getVersionComparator().compare(libraryVersion, version) < 0) |
140 | |
{ |
141 | 0 | libraryVersion = version; |
142 | |
} |
143 | |
} |
144 | 0 | } |
145 | |
} |
146 | 0 | return libraryVersion; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
public URL getResourceURL(String resourceId) |
151 | |
{ |
152 | |
try |
153 | |
{ |
154 | 0 | return FacesContext.getCurrentInstance().getExternalContext().getResource( |
155 | |
getPrefix() + '/' + resourceId); |
156 | |
} |
157 | 0 | catch (MalformedURLException e) |
158 | |
{ |
159 | 0 | return null; |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
@Override |
164 | |
public URL getResourceURL(ResourceMeta resourceMeta) |
165 | |
{ |
166 | 0 | return getResourceURL(resourceMeta.getResourceIdentifier()); |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public InputStream getResourceInputStream(ResourceMeta resourceMeta) |
171 | |
{ |
172 | 0 | return FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream( |
173 | |
getPrefix() + '/' + resourceMeta.getResourceIdentifier()); |
174 | |
} |
175 | |
|
176 | |
@Override |
177 | |
public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion, |
178 | |
String resourceName, String resourceVersion) |
179 | |
{ |
180 | 0 | return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, resourceVersion); |
181 | |
} |
182 | |
|
183 | |
@Override |
184 | |
public boolean libraryExists(String libraryName) |
185 | |
{ |
186 | 0 | if (getPrefix() != null && !"".equals(getPrefix())) |
187 | |
{ |
188 | |
try |
189 | |
{ |
190 | 0 | URL url = |
191 | |
FacesContext.getCurrentInstance().getExternalContext().getResource( |
192 | |
getPrefix() + '/' + libraryName); |
193 | 0 | if (url != null) |
194 | |
{ |
195 | 0 | return true; |
196 | |
} |
197 | |
} |
198 | 0 | catch (MalformedURLException e) |
199 | |
{ |
200 | 0 | return false; |
201 | 0 | } |
202 | |
} |
203 | |
else |
204 | |
{ |
205 | |
try |
206 | |
{ |
207 | |
|
208 | 0 | URL url = FacesContext.getCurrentInstance().getExternalContext().getResource(libraryName); |
209 | |
|
210 | 0 | if (url != null) |
211 | |
{ |
212 | 0 | return true; |
213 | |
} |
214 | |
} |
215 | 0 | catch (MalformedURLException e) |
216 | |
{ |
217 | 0 | return false; |
218 | 0 | } |
219 | |
} |
220 | 0 | return false; |
221 | |
} |
222 | |
} |