View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.application;
23  
24  import java.util.Locale;
25  
26  import javax.faces.application.Resource;
27  import javax.faces.application.ResourceHandler;
28  import javax.faces.application.ResourceHandlerWrapper;
29  
30  /**
31   * {@link ResourceHandlerWrapper} which wraps PrimeFaces Extensions resources and appends the version of PrimeFaces Extensions in the
32   * {@link PrimeFacesExtensionsResource}.
33   *
34   * @author Thomas Andraschko
35   * @since 0.1
36   * @since 10.0 will append e=x.x to all libs starting with "primefaces"
37   */
38  public class PrimeFacesExtensionsResourceHandler extends ResourceHandlerWrapper {
39  
40      @SuppressWarnings("deprecation") // the default constructor is deprecated in JSF 2.3
41      public PrimeFacesExtensionsResourceHandler(final ResourceHandler resourceHandler) {
42          super(resourceHandler);
43      }
44  
45      @Override
46      public Resource createResource(final String resourceName, final String libraryName) {
47          Resource resource = super.createResource(resourceName, libraryName);
48          return wrapResource(resource, libraryName);
49      }
50  
51      @Override
52      public Resource createResource(String resourceName, String libraryName, String contentType) {
53          Resource resource = super.createResource(resourceName, libraryName, contentType);
54          return wrapResource(resource, libraryName);
55      }
56  
57      private static Resource wrapResource(Resource resource, String libraryName) {
58          // libs starting with "primefaces" will get "&e=9.0" extension version appended
59          if (resource != null && libraryName != null
60                      && (libraryName.toLowerCase(Locale.getDefault()).startsWith(org.primefaces.util.Constants.LIBRARY))) {
61              return new PrimeFacesExtensionsResource(resource);
62          }
63          else {
64              return resource;
65          }
66      }
67  }