Search in sources :

Example 1 with ResourceValue

use of org.apache.myfaces.resource.ResourceHandlerCache.ResourceValue 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 ResourceValue

use of org.apache.myfaces.resource.ResourceHandlerCache.ResourceValue 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 ResourceValue

use of org.apache.myfaces.resource.ResourceHandlerCache.ResourceValue 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)

Aggregations

Resource (jakarta.faces.application.Resource)3 ContractResource (org.apache.myfaces.resource.ContractResource)3 ContractResourceLoader (org.apache.myfaces.resource.ContractResourceLoader)3 ResourceCachedInfo (org.apache.myfaces.resource.ResourceCachedInfo)3 ResourceValue (org.apache.myfaces.resource.ResourceHandlerCache.ResourceValue)3 ResourceImpl (org.apache.myfaces.resource.ResourceImpl)3 ResourceLoader (org.apache.myfaces.resource.ResourceLoader)3 ResourceMeta (org.apache.myfaces.resource.ResourceMeta)3 FacesContext (jakarta.faces.context.FacesContext)2