All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.resourcehandler.DefaultResourceHandler Maven / Gradle / Ivy

/*
 * Copyright 2014 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
 *
 *     http://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 javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
import javax.faces.application.ResourceHandlerWrapper;

/**
 * 

* A default {@link ResourceHandler} implementation which hooks on all three {@link #createResource(String)}, * {@link #createResource(String, String)} and {@link #createResource(String, String, String)} methods. Implementors * should only need to override either {@link #getLibraryName()} and * {@link #createResourceFromLibrary(String, String)}, or {@link #decorateResource(Resource)}. * * @author Bauke Scholtz * @since 2.0 */ public class DefaultResourceHandler extends ResourceHandlerWrapper { // Constants ------------------------------------------------------------------------------------------------------ public static final String RES_NOT_FOUND = "RES_NOT_FOUND"; // Properties ----------------------------------------------------------------------------------------------------- private ResourceHandler wrapped; // Constructors --------------------------------------------------------------------------------------------------- /** * Creates a new instance of this default resource handler which wraps the given resource handler. * @param wrapped The resource handler to be wrapped. */ public DefaultResourceHandler(ResourceHandler wrapped) { this.wrapped = wrapped; } // Actions -------------------------------------------------------------------------------------------------------- /** * Delegate to {@link #decorateResource(Resource)} with result of {@link #createResource(String)} from the wrapped * resource handler. *

* Implementors should not override this. */ @Override public Resource createResource(String resourceName) { return decorateResource(getWrapped().createResource(resourceName)); } /** * If library name is not null and it equals {@link #getLibraryName()}, then delegate to * {@link #createResourceFromLibrary(String, String)} with null as content type, else delegate to * {@link #decorateResource(Resource)} with result of {@link #createResource(String, String)} from the wrapped * resource handler. *

* Implementors should not override this. */ @Override public Resource createResource(String resourceName, String libraryName) { if (libraryName != null && libraryName.equals(getLibraryName())) { return createResourceFromLibrary(resourceName, null); } else { return decorateResource(getWrapped().createResource(resourceName, libraryName)); } } /** * If library name is not null and it equals {@link #getLibraryName()}, then delegate to * {@link #createResourceFromLibrary(String, String)}, else delegate to {@link #decorateResource(Resource)} with * result of {@link #createResource(String, String, String)} from the wrapped resource handler. *

* Implementors should not override this. */ @Override public Resource createResource(String resourceName, String libraryName, String contentType) { if (libraryName != null && libraryName.equals(getLibraryName())) { return createResourceFromLibrary(resourceName, contentType); } else { return decorateResource(getWrapped().createResource(resourceName, libraryName, contentType)); } } /** * Returns the library name on which this resource handler implementation should listen. If a resource from * specifically this library name is requested, then {@link #createResourceFromLibrary(String, String)} will be * called to create the resource, else {@link #decorateResource(Resource)} will be called with result of the call * from the wrapped resource handler. *

* The default implementation returns null. * @return The library name on which this resource handler implementation should listen. */ public String getLibraryName() { return null; } /** * Returns the library-specific resource in case a resource from specifically the library name as identified by * {@link #getLibraryName()} is requested. *

* The default implementation returns null. * @param resourceName The resource name. * @param contentType The content type. * @return The library-specific resource. */ public Resource createResourceFromLibrary(String resourceName, String contentType) { return null; } /** * Decorate the given resource. This will only be called if no library-specific resource has been requested. *

* The default implementation just returns the given resource unmodified. * @param resource The resource to be decorated. * @return The decorated resource. */ public Resource decorateResource(Resource resource) { return resource; } @Override public ResourceHandler getWrapped() { return wrapped; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy