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