Tony
Tony

Reputation: 2406

How to test if some file exists as JSF resource

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?

Upvotes: 1

Views: 797

Answers (1)

BalusC
BalusC

Reputation: 1108632

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", ...);
}

Upvotes: 1

Related Questions