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