/
CDNResourceHandler.java
286 lines (254 loc) · 12.3 KB
/
CDNResourceHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright OmniFaces
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.omnifaces.resourcehandler;
import static java.lang.Boolean.parseBoolean;
import static org.omnifaces.util.Faces.evaluateExpressionGet;
import static org.omnifaces.util.Faces.getInitParameter;
import static org.omnifaces.util.Utils.isEmpty;
import java.util.HashMap;
import java.util.Map;
import jakarta.faces.application.Resource;
import jakarta.faces.application.ResourceDependency;
import jakarta.faces.application.ResourceHandler;
/**
* <p>
* This {@link ResourceHandler} implementation allows the developer to provide external (CDN) URLs instead of the
* default local URLs for Faces resources. This also works on auto-included resources provided as
* {@link ResourceDependency} by the Faces implementation and/or component libraries. For example, Faces's own
* <code>jakarta.faces:jsf.js</code> resource or PrimeFaces' <code>primefaces:jquery/jquery.js</code> resource could be
* pointed to a CDN.
*
* <h2>Installation</h2>
* <p>
* To get it to run, this handler needs be registered as follows in <code>faces-config.xml</code>:
* <pre>
* <application>
* <resource-handler>org.omnifaces.resourcehandler.CDNResourceHandler</resource-handler>
* </application>
* </pre>
*
* <h2>Standard configuration</h2>
* <p>
* To configure the CDN URLs, a {@value org.omnifaces.resourcehandler.CDNResourceHandler#PARAM_NAME_CDN_RESOURCES}
* context parameter has to be provided wherein the CDN resources are been specified as a comma separated string of
* <code>libraryName:resourceName=https://cdn.example.com/url</code> key=value pairs. The key represents the default
* Faces resource identifier and the value represents the full CDN URL, including the scheme. The CDN URL is not validated
* by this resource handler, so you need to make absolutely sure yourself that it is valid.
* <p>
* Here is an example configuration:
* <pre>
* <context-param>
* <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
* <param-value>
* js/script1.js=https://cdn.example.com/js/script1.js,
* somelib:js/script2.js=https://cdn.example.com/somelib/js/script2.js,
* otherlib:style.css=https://cdn.example.com/otherlib/style.css,
* somelib:images/logo.png=https://cdn.example.com/somelib/logo.png
* </param-value>
* </context-param>
* </pre>
* <p>
* With the above configuration, the following resources:
* <pre>
* <h:outputScript name="js/script1.js" />
* <h:outputScript library="somelib" name="js/script2.js" />
* <h:outputStylesheet library="otherlib" name="style.css" />
* <h:graphicImage library="somelib" name="images/logo.png" />
* </pre>
* <p>
* Will be rendered as:
* <pre>
* <script type="text/javascript" src="https://cdn.example.com/js/script1.js"></script>
* <script type="text/javascript" src="https://cdn.example.com/somelib/js/script2.js"></script>
* <link type="text/css" rel="stylesheet" href="https://cdn.example.com/otherlib/style.css" />
* <img src="https://cdn.example.com/logo.png" />
* </pre>
* <p>
* Here is a real world example with Bootstrap:
* <pre>
* <context-param>
* <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
* <param-value>
* cdn:bootstrap.css=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css,
* cdn:bootstrap.js=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
* </param-value>
* </context-param>
* </pre>
* <p>
* With the above configuration, the following resources:
* <pre>
* <h:outputStylesheet library="cdn" name="bootstrap.css" />
* <h:outputScript library="cdn" name="bootstrap.js" />
* </pre>
* <p>
* Will be rendered as:
* <pre>
* <link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
* <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
* </pre>
*
* <h2>Wildcard configuration</h2>
* <p>
* You can also use the wildcard syntax to map every single resource of a specific library to a common CDN URL. To
* achieve that, just use <code>*</code> as the sole resource name and make sure that the CDN URL ends with
* <code>/*</code>. Here's an example:
* <pre>
* <context-param>
* <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
* <param-value>jquery-cdn:*=https://code.jquery.com/*</param-value>
* </context-param>
* </pre>
* With the above configuration, the following resources:
* <pre>
* <h:outputScript library="jquery-cdn" name="jquery-1.9.1.js" />
* <h:outputScript library="jquery-cdn" name="ui/1.10.3/jquery-ui.js" />
* </pre>
* <p>
* Will be rendered as:
* <pre>
* <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>
* <script type="text/javascript" src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
* </pre>
*
* <h2>EL expressions</h2>
* <p>The CDN resource handler supports evaluating EL expessions in the CDN URL. Here's an example:</p>
* <pre>
* <context-param>
* <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
* <param-value>jquery-cdn:*=https://#{settings.jqueryCDN}/*</param-value>
* </context-param>
* </pre>
* <p>The EL expression is resolved on a per-request basis.</p>
*
* <h2>Conditionally disable CDN resource handler</h2>
* <p>
* If you'd like to supply a context parameter which conditionally disables the CDN resource handler, then set the
* context parameter {@value org.omnifaces.resourcehandler.CDNResourceHandler#PARAM_NAME_CDN_DISABLED} accordingly.
* <pre>
* <context-param>
* <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name>
* <param-value>true</param-value>
* </context-param>
* <!-- or -->
* <context-param>
* <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name>
* <param-value>#{facesContext.application.projectStage eq 'Development'}</param-value>
* </context-param>
* <!-- or -->
* <context-param>
* <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name>
* <param-value>#{someBean.someBooleanProperty}</param-value>
* </context-param>
* </pre>
* <p>The EL expression is resolved on a per-request basis.</p>
*
* <h2>CombinedResourceHandler</h2>
* <p>
* If you're also using the {@link CombinedResourceHandler}, then you need to understand that CDN resources can
* simply not be combined, as that would defeat the CDN purpose. The {@link CombinedResourceHandler} will therefore
* automatically exclude all CDN resources from combining.
*
* @author Bauke Scholtz
* @since 1.2
* @see RemappedResource
* @see DefaultResourceHandler
*/
public class CDNResourceHandler extends DefaultResourceHandler {
// Constants ------------------------------------------------------------------------------------------------------
/** The context parameter name to specify CDN URLs for the given resource identifiers. */
public static final String PARAM_NAME_CDN_RESOURCES = "org.omnifaces.CDN_RESOURCE_HANDLER_URLS";
/** The context parameter name to conditionally disable CDN resource handler. @since 2.0 */
public static final String PARAM_NAME_CDN_DISABLED = "org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED";
private static final String ERROR_MISSING_INIT_PARAM =
"Context parameter '" + PARAM_NAME_CDN_RESOURCES + "' is missing in web.xml or web-fragment.xml.";
private static final String ERROR_INVALID_INIT_PARAM =
"Context parameter '" + PARAM_NAME_CDN_RESOURCES + "' is in invalid syntax."
+ " It must follow 'resourceId=URL,resourceId=URL,resourceId=URL' syntax.";
private static final String ERROR_INVALID_WILDCARD =
"Context parameter '" + PARAM_NAME_CDN_RESOURCES + "' is in invalid syntax."
+ " Wildcard can only represent entire resource name '*' and URL suffix '/*' as in"
+ " 'libraryName:*=https://cdn.example.com/*'.";
// Properties -----------------------------------------------------------------------------------------------------
private String disabledParam;
private Map<ResourceIdentifier, String> cdnResources;
// Constructors ---------------------------------------------------------------------------------------------------
/**
* Creates a new instance of this CDN resource handler which wraps the given resource handler. The CDN resources
* will be initialized based on the {@value org.omnifaces.resourcehandler.CDNResourceHandler#PARAM_NAME_CDN_RESOURCES}
* context parameter.
* @param wrapped The resource handler to be wrapped.
* @throws IllegalArgumentException When the context parameter is missing or is in invalid format.
*/
public CDNResourceHandler(ResourceHandler wrapped) {
super(wrapped);
disabledParam = getInitParameter(PARAM_NAME_CDN_DISABLED);
cdnResources = initCDNResources();
if (cdnResources == null) {
throw new IllegalArgumentException(ERROR_MISSING_INIT_PARAM);
}
}
// Actions --------------------------------------------------------------------------------------------------------
/**
* If the given resource is not <code>null</code> and the CDN resource handler is not (conditionally) disabled for
* the current request, then the CDN resources will be consulted if any CDN URL is available for the given resource.
* If there is none, then just return the Faces default resource, otherwise return a wrapped resource whose
* {@link Resource#getRequestPath()} returns the CDN URL as is been set in the
* {@value org.omnifaces.resourcehandler.CDNResourceHandler#PARAM_NAME_CDN_RESOURCES} context parameter.
*/
@Override
public Resource decorateResource(Resource resource, String resourceName, String libraryName) {
if (disabledParam != null && parseBoolean(String.valueOf((Object) evaluateExpressionGet(disabledParam)))) {
return resource;
}
String requestPath = null;
if (cdnResources != null) {
requestPath = cdnResources.get(new ResourceIdentifier(libraryName, resourceName));
if (requestPath == null) {
requestPath = cdnResources.get(new ResourceIdentifier(libraryName, "*"));
if (requestPath != null) {
requestPath = requestPath.substring(0, requestPath.length() - 1) + resourceName;
}
}
}
if (requestPath == null) {
return resource;
}
String evaluatedRequestPath = evaluateExpressionGet(requestPath);
return new RemappedResource(resourceName, libraryName, evaluatedRequestPath);
}
// Helpers --------------------------------------------------------------------------------------------------------
/**
* Initialize the CDN resources.
* @return The CDN resources, or <code>null</code> if the context parameter has not been set.
* @throws IllegalArgumentException When the context parameter value is in invalid format.
*/
static Map<ResourceIdentifier, String> initCDNResources() {
String cdnResourcesParam = getInitParameter(PARAM_NAME_CDN_RESOURCES);
if (isEmpty(cdnResourcesParam)) {
return null;
}
Map<ResourceIdentifier, String> cdnResources = new HashMap<>();
for (String cdnResource : cdnResourcesParam.split("\\s*,\\s*")) {
String[] cdnResourceIdAndURL = cdnResource.split("\\s*=\\s*", 2);
if (cdnResourceIdAndURL.length != 2) {
throw new IllegalArgumentException(ERROR_INVALID_INIT_PARAM);
}
ResourceIdentifier id = new ResourceIdentifier(cdnResourceIdAndURL[0]);
if (id.getName().contains("*") && (!"*".equals(id.getName()) || !cdnResourceIdAndURL[1].endsWith("/*"))) {
throw new IllegalArgumentException(ERROR_INVALID_WILDCARD);
}
cdnResources.put(id, cdnResourceIdAndURL[1]);
}
return cdnResources;
}
}