Search in sources :

Example 1 with ResourceMeta

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

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

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

the class ResourceHandlerImpl method deriveViewResourceMeta.

protected ResourceMeta deriveViewResourceMeta(FacesContext context, ResourceLoader resourceLoader, String resourceName, String localePrefix) {
    ResourceMeta resourceMeta = null;
    String resourceVersion = null;
    // 1. Try to locate resource in a localized path
    if (localePrefix != null) {
        resourceVersion = resourceLoader.getResourceVersion(localePrefix + '/' + resourceName);
        if (!(resourceVersion != null && ResourceLoader.VERSION_INVALID.equals(resourceVersion))) {
            resourceMeta = resourceLoader.createResourceMeta(localePrefix, null, null, resourceName, resourceVersion);
        }
        if (resourceMeta != null && !resourceLoader.resourceExists(resourceMeta)) {
            resourceMeta = null;
        }
    }
    // 2. Try to localize resource in a non localized path
    if (resourceMeta == null) {
        resourceVersion = resourceLoader.getResourceVersion(resourceName);
        if (!(resourceVersion != null && ResourceLoader.VERSION_INVALID.equals(resourceVersion))) {
            resourceMeta = resourceLoader.createResourceMeta(null, null, null, resourceName, resourceVersion);
        }
        if (resourceMeta != null && !resourceLoader.resourceExists(resourceMeta)) {
            resourceMeta = null;
        }
    }
    return resourceMeta;
}
Also used : ResourceMeta(org.apache.myfaces.resource.ResourceMeta)

Example 4 with ResourceMeta

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

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

the class ResourceHandlerImplTest method testDeriveResourceMeta1.

@Test
public void testDeriveResourceMeta1() throws Exception {
    application.setMessageBundle("org/apache/myfaces/application/resourcehandler/messages");
    ResourceLoader loader = new ResourceLoader("/resources") {

        @Override
        public String getResourceVersion(String path) {
            return null;
        }

        @Override
        public String getLibraryVersion(String path) {
            return null;
        }

        public URL getResourceURL(String resourceId) {
            try {
                return new URL("file://" + resourceId);
            } catch (MalformedURLException ex) {
                Logger.getLogger(ResourceHandlerImplTest.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }

        @Override
        public URL getResourceURL(ResourceMeta resourceMeta) {
            try {
                return new URL("file://" + resourceMeta.getResourceIdentifier());
            } catch (MalformedURLException ex) {
                Logger.getLogger(ResourceHandlerImplTest.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }

        @Override
        public InputStream getResourceInputStream(ResourceMeta resourceMeta) {
            return null;
        }

        @Override
        public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion, String resourceName, String resourceVersion) {
            return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
        }

        @Override
        public boolean libraryExists(String libraryName) {
            return true;
        }
    };
    application.setDefaultLocale(Locale.ENGLISH);
    ResourceMeta resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/mylib/1_0_2/myres.js/1_3.js");
    Assert.assertNotNull(resource);
    Assert.assertEquals("en", resource.getLocalePrefix());
    Assert.assertEquals("mylib", resource.getLibraryName());
    Assert.assertEquals("1_0_2", resource.getLibraryVersion());
    Assert.assertEquals("myres.js", resource.getResourceName());
    Assert.assertEquals("1_3", resource.getResourceVersion());
    resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/mylib/1_0_2/myres.js");
    Assert.assertNotNull(resource);
    Assert.assertEquals("en", resource.getLocalePrefix());
    Assert.assertEquals("mylib", resource.getLibraryName());
    Assert.assertEquals("1_0_2", resource.getLibraryVersion());
    Assert.assertEquals("myres.js", resource.getResourceName());
    Assert.assertNull(resource.getResourceVersion());
    resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/mylib/myres.js/1_3.js");
    Assert.assertNotNull(resource);
    Assert.assertEquals("en", resource.getLocalePrefix());
    Assert.assertEquals("mylib", resource.getLibraryName());
    Assert.assertNull(resource.getLibraryVersion());
    Assert.assertEquals("myres.js", resource.getResourceName());
    Assert.assertEquals("1_3", resource.getResourceVersion());
    resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/mylib/myres.js");
    Assert.assertNotNull(resource);
    Assert.assertEquals("en", resource.getLocalePrefix());
    Assert.assertEquals("mylib", resource.getLibraryName());
    Assert.assertEquals("myres.js", resource.getResourceName());
    Assert.assertNull(resource.getLibraryVersion());
    Assert.assertNull(resource.getResourceVersion());
    resource = resourceHandler.deriveResourceMeta(facesContext, loader, "en/myres.js");
    Assert.assertNotNull(resource);
    Assert.assertNull(resource.getLibraryName());
    Assert.assertNull(resource.getLibraryVersion());
    Assert.assertNull(resource.getResourceVersion());
    Assert.assertEquals("en", resource.getLocalePrefix());
    Assert.assertEquals("myres.js", resource.getResourceName());
    resource = resourceHandler.deriveResourceMeta(facesContext, loader, "mylib/myres.js");
    Assert.assertNotNull(resource);
    Assert.assertNull(resource.getLocalePrefix());
    Assert.assertNull(resource.getLibraryVersion());
    Assert.assertNull(resource.getResourceVersion());
    Assert.assertEquals("mylib", resource.getLibraryName());
    Assert.assertEquals("myres.js", resource.getResourceName());
    resource = resourceHandler.deriveResourceMeta(facesContext, loader, "myres.js");
    Assert.assertNotNull(resource);
    Assert.assertNull(resource.getLocalePrefix());
    Assert.assertNull(resource.getLibraryName());
    Assert.assertNull(resource.getLibraryVersion());
    Assert.assertNull(resource.getResourceVersion());
    Assert.assertEquals("myres.js", resource.getResourceName());
}
Also used : ClassLoaderResourceLoader(org.apache.myfaces.resource.ClassLoaderResourceLoader) ResourceLoader(org.apache.myfaces.resource.ResourceLoader) MalformedURLException(java.net.MalformedURLException) ResourceMetaImpl(org.apache.myfaces.resource.ResourceMetaImpl) ResourceMeta(org.apache.myfaces.resource.ResourceMeta) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ResourceMeta (org.apache.myfaces.resource.ResourceMeta)10 ResourceLoader (org.apache.myfaces.resource.ResourceLoader)4 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 FacesContext (jakarta.faces.context.FacesContext)2 Locale (java.util.Locale)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ClassLoaderResourceLoader (org.apache.myfaces.resource.ClassLoaderResourceLoader)1 ResourceMetaImpl (org.apache.myfaces.resource.ResourceMetaImpl)1 Test (org.junit.Test)1