1

I have my resourceHandler, the resources are CSS files in Jar ziped as resource.

    public class MyVersionResourceHandler extends ResourceHandlerWrapper {

        private ResourceHandler wrapped;

        public MyVersionResourceHandler(ResourceHandler wrapped) {
            this.wrapped = wrapped;
        }

        @Override
        public Resource createResource(String resourceName) {
            return createResource(resourceName, null, null);
        }

        @Override
        public Resource createResource(String resourceName, String libraryName) {
            return createResource(resourceName, libraryName, null);
        }

        @Override
        public Resource createResource(String resourceName, String libraryName, String contentType) {
//////TODO:
            if (RESOURCE_NAME exists)
              resource1 = super.createResource(resourceName, libraryName, contentType);
            else {
              resource1 = super.createResource ("default-file", ...)
            }
//////
            final Resource resource = super.createResource(resourceName, libraryName, contentType);

            if (resource == null) {
                return null;
            }

            return new ResourceWrapper() {

                @Override
                public String getRequestPath() {
                    return super.getRequestPath();
                }

                @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
                public String getResourceName() {
                    return resource.getResourceName();
                }

                @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
                public String getLibraryName() {
                    return resource.getLibraryName();
                }

                @Override // Necessary because this is missing in ResourceWrapper (will be fixed in JSF 2.2).
                public String getContentType() {
                    return resource.getContentType();
                }

                @Override
                public Resource getWrapped() {
                    return resource;
                }
            };
        }

        @Override
        public ResourceHandler getWrapped() {
            return wrapped;
        }

    }

The modified example is from https://stackoverflow.com/a/18146091/2023524

How is it possible to check if the specified file in specified library exists ? And what should I return as default file?

Community
  • 1
  • 1
Tony
  • 2,266
  • 4
  • 33
  • 54

1 Answers1

1

The super.createResource() will return null if resource doesn't exist. Make use of it.

resource1 = super.createResource(resourceName, libraryName, contentType);

if (resource1 == null) {
    resource1 = super.createResource ("default-file", ...);
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555