javax.faces.application.Resource Java Examples

The following examples show how to use javax.faces.application.Resource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CombinedResourceInputStream.java    From BootsFaces-Examples with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an instance of {@link CombinedResourceInputStream} based on the given resources. For each resource, the {@link InputStream}
 * will be obtained and hold in an iterable collection.
 * 
 * @param resources
 *            The resources to be read.
 * @throws IOException
 *             If something fails at I/O level.
 */
public CombinedResourceInputStream(Set<Resource> resources) throws IOException {
    prepareStreaming(resources);

    /* 16.02.2015 Caching added by Stephan Rauh, http://www.beyondjava.net */
    if ("true".equals(Faces.getInitParameter(PARAM_NAME_ACTIVATE_RESOURCE_CACHING))) {
        combinedResource = prepareStreamingFromCache(streamIterator, resources);
        pointer = 0;
        currentStream = null;
    }
    else
    {
        streamIterator.hasNext(); // We assume it to be always true, see also CombinedResource#getInputStream().
        currentStream = streamIterator.next();
    }
    /* 16.02.2015 end of pull request */
}
 
Example #2
Source File: CombinedResourceInputStream.java    From BootsFaces-Examples with Apache License 2.0 6 votes vote down vote up
/**
 * Collects the list of Stream that have to be read.
 * @param resources The resources to be read.
 * @throws IOException If something fails at I/O level.
 */
private void prepareStreaming(Set<Resource> resources) throws IOException {
    streams = new ArrayList<>();
    String domainURL = getRequestDomainURL();

    for (Resource resource : resources) {
        InputStream stream;

        try {
            stream = resource.getInputStream();
        } catch (Exception richFacesDoesNotSupportThis) {
            stream = new URL(domainURL + resource.getRequestPath()).openStream();
        }

        streams.add(stream);
        streams.add(new ByteArrayInputStream(CRLF));
    }

    streamIterator = streams.iterator();
}
 
Example #3
Source File: InternalIE8CompatiblityLinks.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
@Override
  public void encodeBegin(FacesContext context) throws IOException {
      Application app = context.getApplication();
      ResourceHandler rh = app.getResourceHandler();
  	ResponseWriter responseWriter = context.getResponseWriter();
Resource h5s = rh.createResource("js/html5shiv.js", C.BSF_LIBRARY);
Resource rjs = rh.createResource("js/respond.js", C.BSF_LIBRARY);

responseWriter.write("<!--[if lt IE 9]>");
responseWriter.startElement("script", null);
responseWriter.writeAttribute("src", h5s.getRequestPath(), null);
responseWriter.endElement("script");
responseWriter.startElement("script", null);
responseWriter.writeAttribute("src", rjs.getRequestPath(), null);
responseWriter.endElement("script");
responseWriter.write("<![endif]-->");
  }
 
Example #4
Source File: DeltaSpikeResourceHandler.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public Resource createResource(String resourceName, String libraryName)
{
    Resource resource = wrapped.createResource(resourceName, libraryName);

    if (activated && resource != null && libraryName != null && LIBRARY.equals(libraryName))
    {
        if (ProjectStageProducer.getInstance().getProjectStage() == ProjectStage.Development)
        {
            resource = wrapped.createResource(resourceName, LIBRARY_UNCOMPRESSED);
        }
        
        resource = new DeltaSpikeResource(resource, version);
    }

    return resource;
}
 
Example #5
Source File: UnmappedResourceHandler.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
@Override
public Resource createResource(final String resourceName, final String libraryName) {
    final Resource resource = super.createResource(resourceName, libraryName);

    if (resource == null) {
        return null;
    }
    return new BsfResWrapper(resource);
    
}
 
Example #6
Source File: Datepicker.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
public Datepicker() {
	setRendererType(null); // this component renders itself

	AddResourcesListener.addThemedCSSResource("core.css");
	AddResourcesListener.addExtCSSResource("jq.ui.core.css");
	AddResourcesListener.addExtCSSResource("jq.ui.theme.css");
	AddResourcesListener.addExtCSSResource("jq.ui.datepicker.css");

	AddResourcesListener.addResourceToHeadButAfterJQuery(C.BSF_LIBRARY, "jq/ui/core.js");
	AddResourcesListener.addResourceToHeadButAfterJQuery(C.BSF_LIBRARY, "jq/ui/datepicker.js");
	FacesContext context = FacesContext.getCurrentInstance();
	Application app = context.getApplication();
	ResourceHandler rh = app.getResourceHandler();
	Resource rdp;
	Iterator<Locale> preferredLanguages = context.getExternalContext().getRequestLocales();
	while (preferredLanguages.hasNext()) {
		String language = preferredLanguages.next().getLanguage();
		if ("en".equals(language)) {
			break;
		}
		final String jsl = "jq/ui/i18n/datepicker-" + language + ".js";
		rdp = rh.createResource(jsl, C.BSF_LIBRARY);
		if (rdp != null) { // rdp is null if the language .js is not present
							// in jar
			AddResourcesListener.addResourceToHeadButAfterJQuery(C.BSF_LIBRARY, jsl);
			break;
		}

	}
	Tooltip.addResourceFiles();
}
 
Example #7
Source File: ImageRenderer.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Determine the path value of an image value.
 * </p>
 *
 * @param context
 *            the {@link FacesContext} for the current request.
 * @param component
 *            the component to obtain the image information from
 * @return the encoded path to the image source
 */
public static String getImageSource(FacesContext context, UIComponent component) {
	Image image = (Image) component;
	ResourceHandler handler = context.getApplication().getResourceHandler();
	String resourceName = image.getName();
	String value = image.getValue();
	if (value != null && value.length() > 0) {
		if (resourceName != null && image.getLibrary() != null) {
			if (FacesContext.getCurrentInstance().isProjectStage(ProjectStage.Development)) {
				LOGGER.warning(
						"Please use either the 'value' attribute of b:image, or the 'name' and 'library' attribute pair. If all three attributes are provided, BootsFaces uses the 'value' attributes, ignoring both 'name' and 'library'.");
			}
		}
		if (handler.isResourceURL(value)) {
			return value;
		} else {
			value = context.getApplication().getViewHandler().getResourceURL(context, value);
			return (context.getExternalContext().encodeResourceURL(value));
		}
	}

	String library = image.getLibrary();
	Resource res = handler.createResource(resourceName, library);
	if (res == null) {
		if (context.isProjectStage(ProjectStage.Development)) {
			String msg = "Unable to find resource " + resourceName;
			FacesMessages.error(component.getClientId(context), msg, msg);
		}
		return "RES_NOT_FOUND";
	} else {
		return (context.getExternalContext().encodeResourceURL(res.getRequestPath()));
	}
}
 
Example #8
Source File: CombinedResourceInputStream.java    From BootsFaces-Examples with Apache License 2.0 4 votes vote down vote up
/**
 * This method collects the resources eagerly and combines them into a byte array. The byte array is cached.
 * @param streamIterator The stream iterator iterates over the resources to be read.
 * @param resources The resources to be read.
 * @return a byte array containing the combined resources. Can't be null.
 * @throws IOException
        If something fails at I/O level.
 */
@SuppressWarnings("resource")
private static byte[] prepareStreamingFromCache(Iterator<InputStream> streamIterator, Set<Resource> resources)
        throws IOException {
    String key = "";

    for (Resource resource : resources) {
        key += resource.getLibraryName() + "/" + resource.getResourceName() + " ";
    }

    org.omnifaces.component.output.cache.Cache scopedCache = CacheFactory.getCache(FacesContext.getCurrentInstance(), DEFAULT_SCOPE);

    byte[] _combinedResource;
    synchronized(CombinedResourceHandler.class){
        _combinedResource = (byte[]) scopedCache.getObject(key);
    }
    
    if (null != _combinedResource) {
        return _combinedResource;
    }

    streamIterator.hasNext(); // We assume it to be always true, see also CombinedResource#getInputStream().
    InputStream currentStream = streamIterator.next();
    // Caching added by Stephan Rauh, www.beyondjava.net, Feb 02, 2015
    if (null == _combinedResource) {
        ByteArrayOutputStream collector = new ByteArrayOutputStream();
        int read = -1;

        while (true) {
            read = currentStream.read();
            if (read == -1) {
                if (streamIterator.hasNext()) {
                    currentStream = streamIterator.next();
                } else {
                    break;
                }
            } else
                collector.write(read);
        }
        _combinedResource = collector.toByteArray();
        synchronized(CombinedResourceHandler.class){
            if (null==scopedCache.getObject(key))
                scopedCache.putObject(key, _combinedResource, getTimeToLiveOfCacheEntries());
        }
    }
    return _combinedResource;
}
 
Example #9
Source File: BsfResWrapper.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
public BsfResWrapper(Resource r) {
	this.resource=r;
}
 
Example #10
Source File: BsfResWrapper.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
@Override
public Resource getWrapped() {
	return resource;
}
 
Example #11
Source File: DeltaSpikeResource.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
public DeltaSpikeResource(Resource resource, String version)
{
    super();
    this.wrapped = resource;
    this.version = version;
}
 
Example #12
Source File: DeltaSpikeResource.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public Resource getWrapped()
{
    return wrapped;
}