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;
}
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;
}
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;
}
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;
}
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());
}
Aggregations