Search in sources :

Example 1 with ResourceLoader

use of org.apache.myfaces.resource.ResourceLoader in project myfaces by apache.

the class ResourceHandlerImpl method createResource.

@Override
public Resource createResource(String resourceName, String libraryName, String contentType) {
    Assert.notNull(resourceName, "resourceName");
    Resource resource = null;
    if (resourceName.length() == 0) {
        return null;
    }
    if (resourceName.charAt(0) == '/') {
        // If resourceName starts with '/', remove that character because it
        // does not have any meaning (with and without should point to the
        // same resource).
        resourceName = resourceName.substring(1);
    }
    if (!ResourceValidationUtils.isValidResourceName(resourceName)) {
        return null;
    }
    if (libraryName != null && !ResourceValidationUtils.isValidLibraryName(libraryName, isAllowSlashesLibraryName())) {
        return null;
    }
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (contentType == null) {
        // Resolve contentType using ExternalContext.getMimeType
        contentType = facesContext.getExternalContext().getMimeType(resourceName);
    }
    final String localePrefix = getLocalePrefixForLocateResource(facesContext);
    final List<String> contracts = facesContext.getResourceLibraryContracts();
    String contractPreferred = getContractNameForLocateResource(facesContext);
    ResourceValue resourceValue = null;
    // contract name.
    if (contractPreferred != null) {
        resourceValue = getResourceHandlerCache().getResource(resourceName, libraryName, contentType, localePrefix, contractPreferred);
    }
    if (resourceValue == null && !contracts.isEmpty()) {
        // Try to get resource but try with a contract name
        for (String contract : contracts) {
            resourceValue = getResourceHandlerCache().getResource(resourceName, libraryName, contentType, localePrefix, contract);
            if (resourceValue != null) {
                break;
            }
        }
    }
    // Only if no contract preferred try without it.
    if (resourceValue == null) {
        // Try to get resource without contract name
        resourceValue = getResourceHandlerCache().getResource(resourceName, libraryName, contentType, localePrefix);
    }
    if (resourceValue != null) {
        resource = new ResourceImpl(resourceValue.getResourceMeta(), resourceValue.getResourceLoader(), getResourceHandlerSupport(), contentType, resourceValue.getCachedInfo() != null ? resourceValue.getCachedInfo().getURL() : null, resourceValue.getCachedInfo() != null ? resourceValue.getCachedInfo().getRequestPath() : null);
    } else {
        boolean resolved = false;
        // Try preferred contract first
        if (contractPreferred != null) {
            for (ContractResourceLoader loader : getResourceHandlerSupport().getContractResourceLoaders()) {
                ResourceMeta resourceMeta = deriveResourceMeta(loader, resourceName, libraryName, localePrefix, contractPreferred);
                if (resourceMeta != null) {
                    resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                    // cache it
                    getResourceHandlerCache().putResource(resourceName, libraryName, contentType, localePrefix, contractPreferred, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                    resolved = true;
                    break;
                }
            }
        }
        if (!resolved && !contracts.isEmpty()) {
            for (ContractResourceLoader loader : getResourceHandlerSupport().getContractResourceLoaders()) {
                for (String contract : contracts) {
                    ResourceMeta resourceMeta = deriveResourceMeta(loader, resourceName, libraryName, localePrefix, contract);
                    if (resourceMeta != null) {
                        resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                        // cache it
                        getResourceHandlerCache().putResource(resourceName, libraryName, contentType, localePrefix, contract, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                        resolved = true;
                        break;
                    }
                }
            }
        }
        if (!resolved) {
            for (ResourceLoader loader : getResourceHandlerSupport().getResourceLoaders()) {
                ResourceMeta resourceMeta = deriveResourceMeta(loader, resourceName, libraryName, localePrefix);
                if (resourceMeta != null) {
                    resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                    // cache it
                    getResourceHandlerCache().putResource(resourceName, libraryName, contentType, localePrefix, null, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                    break;
                }
            }
        }
    }
    return resource;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ResourceLoader(org.apache.myfaces.resource.ResourceLoader) ContractResourceLoader(org.apache.myfaces.resource.ContractResourceLoader) ResourceCachedInfo(org.apache.myfaces.resource.ResourceCachedInfo) ResourceImpl(org.apache.myfaces.resource.ResourceImpl) ContractResource(org.apache.myfaces.resource.ContractResource) Resource(jakarta.faces.application.Resource) ResourceValue(org.apache.myfaces.resource.ResourceHandlerCache.ResourceValue) ContractResourceLoader(org.apache.myfaces.resource.ContractResourceLoader) ResourceMeta(org.apache.myfaces.resource.ResourceMeta)

Example 2 with ResourceLoader

use of org.apache.myfaces.resource.ResourceLoader in project myfaces by apache.

the class ResourceHandlerImpl method createResourceFromId.

@Override
public Resource createResourceFromId(String resourceId) {
    Resource resource = null;
    Assert.notNull(resourceId, "resourceId");
    // its elements validated properly.
    if (!ResourceValidationUtils.isValidResourceId(resourceId)) {
        return null;
    }
    FacesContext facesContext = FacesContext.getCurrentInstance();
    final List<String> contracts = facesContext.getResourceLibraryContracts();
    String contractPreferred = getContractNameForLocateResource(facesContext);
    ResourceValue resourceValue = null;
    // a contract.
    if (contractPreferred != null) {
        resourceValue = getResourceHandlerCache().getResource(resourceId, contractPreferred);
    }
    if (resourceValue == null && !contracts.isEmpty()) {
        // Try to get resource but try with a contract name
        for (String contract : contracts) {
            resourceValue = getResourceHandlerCache().getResource(resourceId, contract);
            if (resourceValue != null) {
                break;
            }
        }
    }
    if (resourceValue == null) {
        // Try to get resource without contract name
        resourceValue = getResourceHandlerCache().getResource(resourceId);
    }
    if (resourceValue != null) {
        // Resolve contentType using ExternalContext.getMimeType
        String contentType = facesContext.getExternalContext().getMimeType(resourceValue.getResourceMeta().getResourceName());
        resource = new ResourceImpl(resourceValue.getResourceMeta(), resourceValue.getResourceLoader(), getResourceHandlerSupport(), contentType, resourceValue.getCachedInfo() != null ? resourceValue.getCachedInfo().getURL() : null, resourceValue.getCachedInfo() != null ? resourceValue.getCachedInfo().getRequestPath() : null);
    } else {
        boolean resolved = false;
        if (contractPreferred != null) {
            for (ContractResourceLoader loader : getResourceHandlerSupport().getContractResourceLoaders()) {
                ResourceMeta resourceMeta = deriveResourceMeta(facesContext, loader, resourceId, contractPreferred);
                if (resourceMeta != null) {
                    String contentType = facesContext.getExternalContext().getMimeType(resourceMeta.getResourceName());
                    resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                    // cache it
                    getResourceHandlerCache().putResource(resourceId, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                    resolved = true;
                    break;
                }
            }
        }
        if (!resolved && !contracts.isEmpty()) {
            for (ContractResourceLoader loader : getResourceHandlerSupport().getContractResourceLoaders()) {
                for (String contract : contracts) {
                    ResourceMeta resourceMeta = deriveResourceMeta(facesContext, loader, resourceId, contract);
                    if (resourceMeta != null) {
                        String contentType = facesContext.getExternalContext().getMimeType(resourceMeta.getResourceName());
                        resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                        // cache it
                        getResourceHandlerCache().putResource(resourceId, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                        resolved = true;
                        break;
                    }
                }
            }
        }
        if (!resolved) {
            for (ResourceLoader loader : getResourceHandlerSupport().getResourceLoaders()) {
                ResourceMeta resourceMeta = deriveResourceMeta(facesContext, loader, resourceId);
                if (resourceMeta != null) {
                    String contentType = facesContext.getExternalContext().getMimeType(resourceMeta.getResourceName());
                    resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                    // cache it
                    getResourceHandlerCache().putResource(resourceId, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                    break;
                }
            }
        }
    }
    return resource;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ResourceLoader(org.apache.myfaces.resource.ResourceLoader) ContractResourceLoader(org.apache.myfaces.resource.ContractResourceLoader) ResourceCachedInfo(org.apache.myfaces.resource.ResourceCachedInfo) ResourceImpl(org.apache.myfaces.resource.ResourceImpl) ContractResource(org.apache.myfaces.resource.ContractResource) Resource(jakarta.faces.application.Resource) ResourceValue(org.apache.myfaces.resource.ResourceHandlerCache.ResourceValue) ContractResourceLoader(org.apache.myfaces.resource.ContractResourceLoader) ResourceMeta(org.apache.myfaces.resource.ResourceMeta)

Example 3 with ResourceLoader

use of org.apache.myfaces.resource.ResourceLoader in project myfaces by apache.

the class ResourceHandlerImpl method libraryExists.

/**
 * Check if a library exists or not. This is done delegating
 * to each ResourceLoader used, because each one has a different
 * prefix and way to load resources.
 */
@Override
public boolean libraryExists(String libraryName) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String localePrefix = getLocalePrefixForLocateResource(facesContext);
    final List<String> contracts = facesContext.getResourceLibraryContracts();
    String pathToLib = null;
    Boolean libraryFound = null;
    if (libraryName != null && !ResourceValidationUtils.isValidLibraryName(libraryName, isAllowSlashesLibraryName())) {
        return false;
    }
    if (localePrefix != null) {
        // Check with locale
        pathToLib = localePrefix + '/' + libraryName;
        libraryFound = getResourceHandlerCache().libraryExists(pathToLib);
        if (libraryFound != null) {
            return libraryFound;
        }
    }
    libraryFound = getResourceHandlerCache().libraryExists(libraryName);
    if (libraryFound != null) {
        return libraryFound;
    }
    if (localePrefix != null) {
        if (!contracts.isEmpty()) {
            for (String contract : contracts) {
                for (ContractResourceLoader loader : getResourceHandlerSupport().getContractResourceLoaders()) {
                    if (loader.libraryExists(pathToLib, contract)) {
                        getResourceHandlerCache().confirmLibraryExists(pathToLib);
                        return true;
                    }
                }
            }
        }
        for (ResourceLoader loader : getResourceHandlerSupport().getResourceLoaders()) {
            if (loader.libraryExists(pathToLib)) {
                getResourceHandlerCache().confirmLibraryExists(pathToLib);
                return true;
            }
        }
    }
    // Check without locale
    if (!contracts.isEmpty()) {
        for (String contract : contracts) {
            for (ContractResourceLoader loader : getResourceHandlerSupport().getContractResourceLoaders()) {
                if (loader.libraryExists(libraryName, contract)) {
                    getResourceHandlerCache().confirmLibraryExists(libraryName);
                    return true;
                }
            }
        }
    }
    for (ResourceLoader loader : getResourceHandlerSupport().getResourceLoaders()) {
        if (loader.libraryExists(libraryName)) {
            getResourceHandlerCache().confirmLibraryExists(libraryName);
            return true;
        }
    }
    if (localePrefix != null) {
        // Check with locale
        getResourceHandlerCache().confirmLibraryNotExists(pathToLib);
    } else {
        getResourceHandlerCache().confirmLibraryNotExists(libraryName);
    }
    return false;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ResourceLoader(org.apache.myfaces.resource.ResourceLoader) ContractResourceLoader(org.apache.myfaces.resource.ContractResourceLoader) ContractResourceLoader(org.apache.myfaces.resource.ContractResourceLoader)

Example 4 with ResourceLoader

use of org.apache.myfaces.resource.ResourceLoader in project myfaces by apache.

the class ResourceHandlerImpl method createViewResource.

@Override
public Resource createViewResource(FacesContext facesContext, String resourceName) {
    // There are some special points to remember for a view resource in comparison
    // with a normal resource:
    // 
    // - A view resource never has an associated library name
    // (this was done to keep simplicity).
    // - A view resource can be inside a resource library contract.
    // - A view resource could be internationalized in the same way a normal resource.
    // - A view resource can be created from the webapp root folder,
    // a normal resource cannot.
    // - A view resource cannot be created from /resources or META-INF/resources.
    // 
    // For example, a valid resourceId for a view resource is like this:
    // 
    // [localePrefix/]resourceName[/resourceVersion]
    // 
    // but the resource loader can ignore localePrefix or resourceVersion, like
    // for example the webapp root folder.
    // 
    // When createViewResource() is called, the view must be used to derive
    // the localePrefix and facesContext must be used to get the available contracts.
    Resource resource = null;
    Assert.notNull(resourceName, "resourceName");
    if (resourceName.charAt(0) == '/') {
        // If resourceName starts with '/', remove that character because it
        // does not have any meaning (with and without should point to the
        // same resource).
        resourceName = resourceName.substring(1);
    }
    // its elements validated properly.
    if (!ResourceValidationUtils.isValidViewResource(resourceName)) {
        return null;
    }
    final String localePrefix = getLocalePrefixForLocateResource(facesContext);
    String contentType = facesContext.getExternalContext().getMimeType(resourceName);
    final List<String> contracts = facesContext.getResourceLibraryContracts();
    String contractPreferred = getContractNameForLocateResource(facesContext);
    ResourceValue resourceValue = null;
    // a contract.
    if (contractPreferred != null) {
        resourceValue = getResourceHandlerCache().getViewResource(resourceName, contentType, localePrefix, contractPreferred);
    }
    if (resourceValue == null && !contracts.isEmpty()) {
        // Try to get resource but try with a contract name
        for (String contract : contracts) {
            resourceValue = getResourceHandlerCache().getViewResource(resourceName, contentType, localePrefix, contract);
            if (resourceValue != null) {
                break;
            }
        }
    }
    if (resourceValue == null) {
        // Try to get resource without contract name
        resourceValue = getResourceHandlerCache().getViewResource(resourceName, contentType, localePrefix);
    }
    if (resourceValue != null) {
        resource = new ResourceImpl(resourceValue.getResourceMeta(), resourceValue.getResourceLoader(), getResourceHandlerSupport(), contentType, resourceValue.getCachedInfo() != null ? resourceValue.getCachedInfo().getURL() : null, null);
    } else {
        boolean resolved = false;
        if (contractPreferred != null) {
            for (ContractResourceLoader loader : getResourceHandlerSupport().getContractResourceLoaders()) {
                ResourceMeta resourceMeta = deriveViewResourceMeta(facesContext, loader, resourceName, localePrefix, contractPreferred);
                if (resourceMeta != null) {
                    resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                    // cache it
                    getResourceHandlerCache().putViewResource(resourceName, contentType, localePrefix, contractPreferred, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), null));
                    resolved = true;
                    break;
                }
            }
        }
        if (!resolved && !contracts.isEmpty()) {
            for (ContractResourceLoader loader : getResourceHandlerSupport().getContractResourceLoaders()) {
                for (String contract : contracts) {
                    ResourceMeta resourceMeta = deriveViewResourceMeta(facesContext, loader, resourceName, localePrefix, contract);
                    if (resourceMeta != null) {
                        resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                        // cache it
                        getResourceHandlerCache().putViewResource(resourceName, contentType, localePrefix, contract, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), null));
                        resolved = true;
                        break;
                    }
                }
            }
        }
        if (!resolved) {
            // Faces Flows in the Using JSF in Web Applications chapter) ..."
            for (ResourceLoader loader : getResourceHandlerSupport().getViewResourceLoaders()) {
                ResourceMeta resourceMeta = deriveViewResourceMeta(facesContext, loader, resourceName, localePrefix);
                if (resourceMeta != null) {
                    resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
                    // cache it
                    getResourceHandlerCache().putViewResource(resourceName, contentType, localePrefix, resourceMeta, loader, new ResourceCachedInfo(resource.getURL(), null));
                    break;
                }
            }
        }
    }
    return resource;
}
Also used : ResourceLoader(org.apache.myfaces.resource.ResourceLoader) ContractResourceLoader(org.apache.myfaces.resource.ContractResourceLoader) ResourceCachedInfo(org.apache.myfaces.resource.ResourceCachedInfo) ResourceImpl(org.apache.myfaces.resource.ResourceImpl) ContractResource(org.apache.myfaces.resource.ContractResource) Resource(jakarta.faces.application.Resource) ResourceValue(org.apache.myfaces.resource.ResourceHandlerCache.ResourceValue) ContractResourceLoader(org.apache.myfaces.resource.ContractResourceLoader) ResourceMeta(org.apache.myfaces.resource.ResourceMeta)

Example 5 with ResourceLoader

use of org.apache.myfaces.resource.ResourceLoader in project myfaces by apache.

the class ResourceHandlerImplTest method testDisabledCache.

@Test
public void testDisabledCache() {
    ResourceLoader loader = Mockito.spy(new ClassLoaderResourceLoader(null));
    Mockito.when(loader.resourceExists(Mockito.any())).thenReturn(true);
    ResourceHandlerCache cache = Mockito.spy(new ResourceHandlerCache());
    Mockito.when(cache.isResourceHandlerCacheEnabled()).thenReturn(false);
    ResourceHandlerSupport support = Mockito.spy(new DefaultResourceHandlerSupport());
    Mockito.when(support.getResourceLoaders()).thenReturn(new ResourceLoader[] { loader });
    resourceHandler = Mockito.spy(resourceHandler);
    Mockito.when(resourceHandler.getResourceHandlerCache()).thenReturn(cache);
    Mockito.when(resourceHandler.getResourceHandlerSupport()).thenReturn(support);
    resourceHandler.createResource("test.png", "test", "test");
    resourceHandler.createResource("test.png", "test", "test");
    resourceHandler.createResource("test.png", "test", "test");
    resourceHandler.createResource("test.png", "test", "test");
    Mockito.verify(cache, Mockito.times(4)).getResource(Mockito.eq("test.png"), Mockito.eq("test"), Mockito.eq("test"), Mockito.any());
    Mockito.verify(cache, Mockito.times(4)).putResource(Mockito.eq("test.png"), Mockito.eq("test"), Mockito.eq("test"), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
    Mockito.verify(loader, Mockito.times(4)).createResourceMeta(Mockito.any(), Mockito.eq("test"), Mockito.any(), Mockito.eq("test.png"), Mockito.any());
}
Also used : ClassLoaderResourceLoader(org.apache.myfaces.resource.ClassLoaderResourceLoader) ResourceLoader(org.apache.myfaces.resource.ResourceLoader) ResourceHandlerSupport(org.apache.myfaces.resource.ResourceHandlerSupport) ClassLoaderResourceLoader(org.apache.myfaces.resource.ClassLoaderResourceLoader) ResourceHandlerCache(org.apache.myfaces.resource.ResourceHandlerCache) Test(org.junit.Test)

Aggregations

ResourceLoader (org.apache.myfaces.resource.ResourceLoader)9 ContractResourceLoader (org.apache.myfaces.resource.ContractResourceLoader)5 ClassLoaderResourceLoader (org.apache.myfaces.resource.ClassLoaderResourceLoader)4 ResourceMeta (org.apache.myfaces.resource.ResourceMeta)4 Test (org.junit.Test)4 Resource (jakarta.faces.application.Resource)3 FacesContext (jakarta.faces.context.FacesContext)3 ContractResource (org.apache.myfaces.resource.ContractResource)3 ResourceCachedInfo (org.apache.myfaces.resource.ResourceCachedInfo)3 ResourceHandlerCache (org.apache.myfaces.resource.ResourceHandlerCache)3 ResourceValue (org.apache.myfaces.resource.ResourceHandlerCache.ResourceValue)3 ResourceHandlerSupport (org.apache.myfaces.resource.ResourceHandlerSupport)3 ResourceImpl (org.apache.myfaces.resource.ResourceImpl)3 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ResourceMetaImpl (org.apache.myfaces.resource.ResourceMetaImpl)1