use of javax.faces.application.Resource in project primefaces by primefaces.
the class BarcodeRenderer method encodeEnd.
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
Barcode barcode = (Barcode) component;
String clientId = barcode.getClientId(context);
String styleClass = barcode.getStyleClass();
String src = null;
Object value = barcode.getValue();
String type = barcode.getType();
DynamicContentType dynamicContentType = "qr".equals(type) ? DynamicContentType.QR_CODE : DynamicContentType.BARCODE;
if (value == null) {
return;
}
try {
Resource resource = context.getApplication().getResourceHandler().createResource("dynamiccontent.properties", "primefaces", "image/png");
String resourcePath = resource.getRequestPath();
String sessionKey = UUID.randomUUID().toString();
Map<String, Object> session = context.getExternalContext().getSessionMap();
Map<String, String> barcodeMapping = (Map) session.get(Constants.BARCODE_MAPPING);
if (barcodeMapping == null) {
barcodeMapping = new HashMap<>();
session.put(Constants.BARCODE_MAPPING, barcodeMapping);
}
barcodeMapping.put(sessionKey, (String) value);
StringBuilder builder = SharedStringBuilder.get(context, SB_BUILD);
src = builder.append(resourcePath).append("&").append(Constants.DYNAMIC_CONTENT_PARAM).append("=").append(URLEncoder.encode(sessionKey, "UTF-8")).append("&").append(Constants.DYNAMIC_CONTENT_TYPE_PARAM).append("=").append(dynamicContentType.toString()).append("&gen=").append(type).append("&fmt=").append(barcode.getFormat()).append("&qrec=").append(barcode.getQrErrorCorrection()).append("&hrp=").append(barcode.getHrp()).append("&").append(Constants.DYNAMIC_CONTENT_CACHE_PARAM).append("=").append(barcode.isCache()).append("&ori=").append(barcode.getOrientation()).toString();
} catch (UnsupportedEncodingException ex) {
throw new IOException(ex);
}
writer.startElement("img", barcode);
if (shouldWriteId(component)) {
writer.writeAttribute("id", clientId, "id");
}
if (styleClass != null) {
writer.writeAttribute("class", styleClass, "styleClass");
}
writer.writeAttribute("src", context.getExternalContext().encodeResourceURL(src), null);
renderPassThruAttributes(context, barcode, HTML.IMG_ATTRS);
writer.endElement("img");
}
use of javax.faces.application.Resource in project liferay-faces-bridge-impl by liferay.
the class ResourceRichFacesCSSImpl method filter.
@Override
protected String filter(String cssText) {
// Since the same image URL often appears more then once, maintain a cache of URLs for fast lookup.
Map<String, String> resourceURLCache = new HashMap<String, String>();
ResourceHandler resourceHandler = FacesContext.getCurrentInstance().getApplication().getResourceHandler();
// For each of the RichFaces image resource types:
for (RichFacesImageResource richFacesImageResource : RichFacesImageResource.values()) {
// Parse the specified CSS text, and replace each relative URL with a ResourceURL.
boolean doneProcessingURLs = false;
while (!doneProcessingURLs) {
String pathPrefix = richFacesImageResource.getPathPrefix();
int urlStartPos = cssText.indexOf(pathPrefix);
if (urlStartPos > 0) {
int fileNameStartPos = urlStartPos + pathPrefix.length();
int dotPos = cssText.indexOf(".", fileNameStartPos);
if (dotPos > 0) {
boolean doneFindingExtension = false;
int extensionStartPos = dotPos + 1;
int extensionFinishPos = extensionStartPos;
while (!doneFindingExtension) {
if ((extensionFinishPos < cssText.length()) && Character.isLetterOrDigit(cssText.charAt(extensionFinishPos))) {
extensionFinishPos++;
} else {
doneFindingExtension = true;
}
}
String relativePathKey = cssText.substring(urlStartPos, extensionFinishPos);
String imageResourceURL = resourceURLCache.get(relativePathKey);
if (imageResourceURL == null) {
String resourceName = cssText.substring(fileNameStartPos, extensionFinishPos);
String libraryName = richFacesImageResource.getLibraryName();
String substitutionToken = richFacesImageResource.getSubstitutionToken();
Resource imageResource = resourceHandler.createResource(resourceName, libraryName);
imageResourceURL = imageResource.getRequestPath();
imageResourceURL = imageResourceURL.replaceAll(libraryName, substitutionToken);
resourceURLCache.put(relativePathKey, imageResourceURL);
}
StringBuilder buf = new StringBuilder();
buf.append(cssText.substring(0, urlStartPos));
buf.append(imageResourceURL);
buf.append(cssText.substring(extensionFinishPos));
cssText = buf.toString();
} else {
logger.error("Unable to find image filename in URL");
}
} else {
doneProcessingURLs = true;
}
}
}
for (RichFacesImageResource richFacesImageResource : RichFacesImageResource.values()) {
cssText = cssText.replace(richFacesImageResource.getSubstitutionToken(), richFacesImageResource.getLibraryName());
}
return cssText;
}
use of javax.faces.application.Resource in project liferay-faces-bridge-impl by liferay.
the class ResourceRichFacesImplTest method assertResourceRichFacesImplRequestPathEquals.
private static void assertResourceRichFacesImplRequestPathEquals(String expectedRequestPath, String providedRequestPath) {
Resource resource = new ResourceMockImpl(providedRequestPath);
resource = new ResourceRichFacesImpl(resource);
Assert.assertEquals(expectedRequestPath, resource.getRequestPath());
}
use of javax.faces.application.Resource in project liferay-faces-alloy by liferay.
the class MediaRenderer method encodeChildren.
@Override
@SuppressWarnings("unchecked")
public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException {
Media media = (Media) uiComponent;
String name = media.getName();
Object value = media.getValue();
Application application = facesContext.getApplication();
ResourceHandler resourceHandler = application.getResourceHandler();
String firstMediaResourceURL = null;
ResponseWriter responseWriter = facesContext.getResponseWriter();
if (name != null) {
String library = media.getLibrary();
if ((name.length() > 0) && (library != null) && (library.length() > 0)) {
Resource mediaResource = resourceHandler.createResource(name, library);
String contentType = media.getContentType();
if (mediaResource != null) {
String mediaRequestPath = mediaResource.getRequestPath();
ExternalContext externalContext = facesContext.getExternalContext();
firstMediaResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
if (contentType == null) {
contentType = mediaResource.getContentType();
}
} else {
firstMediaResourceURL = RES_NOT_FOUND;
logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, name, library);
}
encodeMediaSource(responseWriter, firstMediaResourceURL, contentType);
}
} else if (value != null) {
if ((value instanceof Collection) || (value instanceof FacesResource[])) {
Collection<FacesResource> facesResources;
if (value instanceof Collection) {
facesResources = (Collection<FacesResource>) value;
} else {
FacesResource[] resourcesAsArray = (FacesResource[]) value;
facesResources = Arrays.asList(resourcesAsArray);
}
for (FacesResource facesResource : facesResources) {
String facesResourceName = facesResource.getName();
String facesResourceLibrary = facesResource.getLibrary();
Resource mediaResource = resourceHandler.createResource(facesResourceName, facesResourceLibrary);
String mediaResourceURL;
String contentType = facesResource.getContentType();
if (mediaResource != null) {
String mediaRequestPath = mediaResource.getRequestPath();
ExternalContext externalContext = facesContext.getExternalContext();
mediaResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
if (contentType == null) {
contentType = mediaResource.getContentType();
}
} else {
mediaResourceURL = RES_NOT_FOUND;
logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, facesResourceName, facesResourceLibrary);
}
if (firstMediaResourceURL == null) {
firstMediaResourceURL = mediaResourceURL;
}
encodeMediaSource(responseWriter, mediaResourceURL, contentType);
}
} else {
String contentType = media.getContentType();
if (value instanceof FacesResource) {
FacesResource facesResource = (FacesResource) value;
String facesResourceName = facesResource.getName();
String facesResourceLibrary = facesResource.getLibrary();
Resource mediaResource = resourceHandler.createResource(facesResourceName, facesResourceLibrary);
if (contentType == null) {
contentType = facesResource.getContentType();
}
if (mediaResource != null) {
String mediaRequestPath = mediaResource.getRequestPath();
ExternalContext externalContext = facesContext.getExternalContext();
firstMediaResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
if (contentType == null) {
contentType = mediaResource.getContentType();
}
} else {
firstMediaResourceURL = RES_NOT_FOUND;
logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, facesResourceName, facesResourceLibrary);
}
} else {
String valueAsString = value.toString();
firstMediaResourceURL = getEncodedResourceURL(facesContext, resourceHandler, application, valueAsString);
}
encodeMediaSource(responseWriter, firstMediaResourceURL, contentType);
}
}
List<UIComponent> children = uiComponent.getChildren();
for (UIComponent child : children) {
if (child instanceof com.liferay.faces.alloy.component.resource.Resource) {
String mediaResourceURL;
com.liferay.faces.alloy.component.resource.Resource resource = (com.liferay.faces.alloy.component.resource.Resource) child;
String contentType = resource.getContentType();
String resourceName = resource.getName();
String resourceLibrary = resource.getLibrary();
Resource mediaResource = resourceHandler.createResource(resourceName, resourceLibrary);
if (mediaResource != null) {
String mediaRequestPath = mediaResource.getRequestPath();
ExternalContext externalContext = facesContext.getExternalContext();
mediaResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
if (contentType == null) {
contentType = mediaResource.getContentType();
}
} else {
mediaResourceURL = RES_NOT_FOUND;
logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, resourceName, resourceLibrary);
}
encodeMediaSource(responseWriter, mediaResourceURL, contentType);
if (firstMediaResourceURL == null) {
firstMediaResourceURL = mediaResourceURL;
}
} else {
child.encodeAll(facesContext);
}
}
// If no media has been specified, then warn the developer.
if (firstMediaResourceURL == null) {
logger.warn("No {0} has been specified for <alloy:{0} id=[{1}] />.", getMediaType(), media.getClientId());
} else // first media URL.
if (media.isDegrade()) {
encodeFlashPlayer(facesContext, responseWriter, media, firstMediaResourceURL);
}
}
use of javax.faces.application.Resource in project liferay-faces-alloy by liferay.
the class MediaRenderer method getEncodedResourceURL.
protected String getEncodedResourceURL(FacesContext facesContext, ResourceHandler resourceHandler, Application application, Object value) {
String encodedResourceURL;
if (value instanceof FacesResource) {
FacesResource facesResource = (FacesResource) value;
String name = facesResource.getName();
String library = facesResource.getLibrary();
Resource resource = resourceHandler.createResource(name, library);
if (resource != null) {
String mediaRequestPath = resource.getRequestPath();
ExternalContext externalContext = facesContext.getExternalContext();
encodedResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
} else {
encodedResourceURL = RES_NOT_FOUND;
logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, name, library);
}
} else {
String posterString = value.toString();
encodedResourceURL = getEncodedResourceURL(facesContext, resourceHandler, application, posterString);
}
return encodedResourceURL;
}
Aggregations