use of jakarta.faces.application.Resource in project myfaces by apache.
the class ResourceHandlerImpl method handleResourceRequest.
/**
* Handle the resource request, writing in the output.
*
* This method implements an algorithm semantically identical to
* the one described on the javadoc of ResourceHandler.handleResourceRequest
*/
@Override
public void handleResourceRequest(FacesContext facesContext) throws IOException {
String resourceBasePath = getResourceHandlerSupport().calculateResourceBasePath(facesContext);
if (resourceBasePath == null) {
// resource base name
return;
}
// We neet to get an instance of HttpServletResponse, but sometimes
// the response object is wrapped by several instances of
// ServletResponseWrapper (like ResponseSwitch).
// Since we are handling a resource, we can expect to get an
// HttpServletResponse.
ExternalContext extContext = facesContext.getExternalContext();
Object response = extContext.getResponse();
HttpServletResponse httpServletResponse = ExternalContextUtils.getHttpServletResponse(response);
if (httpServletResponse == null) {
throw new IllegalStateException("Could not obtain an instance of HttpServletResponse.");
}
if (isResourceIdentifierExcluded(facesContext, resourceBasePath)) {
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
String resourceName = null;
if (resourceBasePath.startsWith(ResourceHandler.RESOURCE_IDENTIFIER)) {
resourceName = resourceBasePath.substring(ResourceHandler.RESOURCE_IDENTIFIER.length() + 1);
if (resourceBasePath != null && !ResourceValidationUtils.isValidResourceName(resourceName)) {
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
} else {
// Does not have the conditions for be a resource call
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
String libraryName = facesContext.getExternalContext().getRequestParameterMap().get("ln");
if (libraryName != null && !ResourceValidationUtils.isValidLibraryName(libraryName, isAllowSlashesLibraryName())) {
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
Resource resource = null;
if (libraryName != null) {
resource = facesContext.getApplication().getResourceHandler().createResource(resourceName, libraryName);
} else {
resource = facesContext.getApplication().getResourceHandler().createResource(resourceName);
}
if (resource == null) {
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (!resource.userAgentNeedsUpdate(facesContext)) {
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
httpServletResponse.setContentType(_getContentType(resource, facesContext.getExternalContext()));
Map<String, String> headers = resource.getResponseHeaders();
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpServletResponse.setHeader(entry.getKey(), entry.getValue());
}
// Sets the preferred buffer size for the body of the response
extContext.setResponseBufferSize(this.getResourceBufferSize());
// serve up the bytes (taken from trinidad ResourceServlet)
try {
InputStream in = resource.getInputStream();
OutputStream out = httpServletResponse.getOutputStream();
byte[] buffer = new byte[this.getResourceBufferSize()];
try {
int count = pipeBytes(in, out, buffer);
// set the content lenght
if (!httpServletResponse.isCommitted()) {
httpServletResponse.setContentLength(count);
}
} finally {
try {
in.close();
} finally {
out.close();
}
}
} catch (IOException e) {
if (isConnectionAbort(e)) {
if (log.isLoggable(Level.FINE)) {
log.log(Level.FINE, "Connection was aborted while loading resource " + resourceName + " with library " + libraryName);
}
} else {
if (log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, "Error trying to load and send resource " + resourceName + " with library " + libraryName + ": " + e.getMessage(), e);
}
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
}
use of jakarta.faces.application.Resource 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 jakarta.faces.application.Resource 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 jakarta.faces.application.Resource 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 jakarta.faces.application.Resource in project myfaces by apache.
the class ApplicationImpl method createComponent.
@Override
public UIComponent createComponent(FacesContext context, Resource componentResource) {
Assert.notNull(context, "context");
Assert.notNull(componentResource, "componentResource");
UIComponent component = null;
Resource resource;
String fqcn;
Class<? extends UIComponent> componentClass = null;
/*
* Obtain a reference to the ViewDeclarationLanguage for this Application instance by calling
* ViewHandler.getViewDeclarationLanguage(jakarta.faces.context.FacesContext, java.lang.String), passing the
* viewId found by calling UIViewRoot.getViewId() on the UIViewRoot in the argument FacesContext.
*/
UIViewRoot view = context.getViewRoot();
Application application = context.getApplication();
ViewDeclarationLanguage vdl = application.getViewHandler().getViewDeclarationLanguage(context, view.getViewId());
/*
* Obtain a reference to the composite component metadata for this composite component by calling
* ViewDeclarationLanguage.getComponentMetadata(jakarta.faces.context.FacesContext,
* jakarta.faces.application.Resource), passing the facesContext and componentResource arguments to this method.
* This version of JSF specification uses JavaBeans as the API to the component metadata.
*/
BeanInfo metadata = vdl.getComponentMetadata(context, componentResource);
if (metadata == null) {
throw new FacesException("Could not get component metadata for " + componentResource.getResourceName() + ". Did you forget to specify <composite:interface>?");
}
/*
* Determine if the component author declared a component-type for this component instance by obtaining the
* BeanDescriptor from the component metadata and calling its getValue() method, passing
* UIComponent.COMPOSITE_COMPONENT_TYPE_KEY as the argument. If non-null, the result must be a ValueExpression
* whose value is the component-type of the UIComponent to be created for this Resource component. Call through
* to createComponent(java.lang.String) to create the component.
*/
BeanDescriptor descriptor = metadata.getBeanDescriptor();
ValueExpression componentType = (ValueExpression) descriptor.getValue(UIComponent.COMPOSITE_COMPONENT_TYPE_KEY);
boolean annotationsApplied = false;
if (componentType != null) {
component = application.createComponent((String) componentType.getValue(context.getELContext()));
annotationsApplied = true;
} else {
/*
* Otherwise, determine if a script based component for this Resource can be found by calling
* ViewDeclarationLanguage.getScriptComponentResource(jakarta.faces.context.FacesContext,
* jakarta.faces.application.Resource). If the result is non-null, and is a script written in one of the
* languages listed in JSF 4.3 of the specification prose document, create a UIComponent instance from the
* script resource.
*/
resource = vdl.getScriptComponentResource(context, componentResource);
if (resource != null) {
String name = resource.getResourceName();
String className = name.substring(0, name.lastIndexOf('.'));
component = (UIComponent) ClassUtils.newInstance(className);
} else {
/*
* Otherwise, let library-name be the return from calling Resource.getLibraryName() on the argument
* componentResource and resource-name be the return from calling Resource.getResourceName() on the
* argument componentResource. Create a fully qualified Java class name by removing any file extension
* from resource-name and let fqcn be library-name + "." + resource-name. If a class with the name of
* fqcn cannot be found, take no action and continue to the next step. If any of
* InstantiationException,
* IllegalAccessException, or ClassCastException are thrown, wrap the exception in a FacesException and
* re-throw it. If any other exception is thrown, log the exception and continue to the next step.
*/
boolean isProduction = FacesContext.getCurrentInstance().isProjectStage(ProjectStage.Production);
String name = componentResource.getResourceName();
String className = name.substring(0, name.lastIndexOf('.'));
fqcn = componentResource.getLibraryName() + '.' + className;
if (isProduction) {
componentClass = (Class<? extends UIComponent>) _componentClassMap.get(fqcn);
}
if (componentClass == null) {
try {
componentClass = ClassUtils.classForName(fqcn);
if (isProduction) {
_componentClassMap.put(fqcn, componentClass);
}
} catch (ClassNotFoundException e) {
// Remember here that classForName did not find Class
if (isProduction) {
_componentClassMap.put(fqcn, NOTHING.getClass());
}
}
}
if (componentClass != null && NOTHING.getClass() != componentClass) {
try {
component = componentClass.newInstance();
} catch (InstantiationException e) {
log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
throw new FacesException("Could not instantiate component class name = " + fqcn, e);
} catch (IllegalAccessException e) {
log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
throw new FacesException("Could not instantiate component class name = " + fqcn, e);
} catch (Exception e) {
log.log(Level.SEVERE, "Could not instantiate component class name = " + fqcn, e);
}
}
/*
* If none of the previous steps have yielded a UIComponent instance, call
* createComponent(java.lang.String) passing "jakarta.faces.NamingContainer" as the argument.
*/
if (component == null) {
component = application.createComponent(context, UINamingContainer.COMPONENT_TYPE, null);
annotationsApplied = true;
}
}
}
/*
* Call UIComponent.setRendererType(java.lang.String) on the UIComponent instance, passing
* "jakarta.faces.Composite" as the argument.
*/
component.setRendererType("jakarta.faces.Composite");
/*
* Store the argument Resource in the attributes Map of the UIComponent under the key,
* Resource.COMPONENT_RESOURCE_KEY.
*/
component.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
/*
* Store composite component metadata in the attributes Map of the UIComponent under the key,
* UIComponent.BEANINFO_KEY.
*/
component.getAttributes().put(UIComponent.BEANINFO_KEY, metadata);
/*
* Before the component instance is returned, it must be inspected for the presence of a
* ListenerFor annotation.
* If this annotation is present, the action listed in ListenerFor must be taken on the component,
* before it is
* returned from this method.
*/
if (!annotationsApplied) {
_handleAnnotations(context, component, component);
}
return component;
}
Aggregations