use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.
the class SlingIOProvider method lastModified.
/**
* Returns the value of the last modified meta data field of the resource
* found at file name or zero if the meta data field is not set. If the
* resource does not exist or an error occurrs finding the resource, -1 is
* returned.
*/
public long lastModified(String fileName) {
try {
final Resource resource = getResourceInternal(fileName);
if (resource != null) {
final ResourceMetadata meta = resource.getResourceMetadata();
final long modTime = meta.getModificationTime();
return (modTime > 0) ? modTime : 0;
}
} catch (SlingException se) {
logger.error("Cannot get last modification time for " + fileName, se);
}
// fallback to "non-existant" in case of problems
return -1;
}
use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.
the class StreamRendererServlet method setHeaders.
/**
* @param resource
* @param response
*/
private void setHeaders(Resource resource, SlingHttpServletResponse response) {
final ResourceMetadata meta = resource.getResourceMetadata();
final long modifTime = meta.getModificationTime();
if (modifTime > 0) {
response.setDateHeader(HEADER_LAST_MODIFIED, modifTime);
}
final String defaultContentType = "application/octet-stream";
String contentType = meta.getContentType();
if (contentType == null || defaultContentType.equals(contentType)) {
// if repository doesn't provide a content-type, or
// provides the
// default one,
// try to do better using our servlet context
final String ct = getServletContext().getMimeType(resource.getPath());
if (ct != null) {
contentType = ct;
}
}
if (contentType == null) {
contentType = defaultContentType;
}
response.setContentType(contentType);
String encoding = meta.getCharacterEncoding();
if (encoding != null) {
response.setCharacterEncoding(encoding);
}
// announce support for ranges if we know the size to be larger than 100KB
if (meta.getContentLength() > ACCEPT_RANGES_THRESHOLD) {
response.setHeader(ACCEPT_RANGES_HEADER, ACCEPT_RANGES_BYTES);
}
}
use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.
the class StreamRendererServlet method renderChild.
private void renderChild(PrintWriter pw, Resource resource) {
String name = ResourceUtil.getName(resource.getPath());
InputStream ins = resource.adaptTo(InputStream.class);
if (ins == null) {
name += "/";
} else {
closeSilently(ins);
}
String displayName = name;
String suffix;
if (displayName.length() >= 32) {
displayName = displayName.substring(0, 29) + "...";
suffix = "";
} else {
suffix = " ".substring(0, 32 - displayName.length());
}
pw.printf("<a href='%s'>%s</a>%s", name, displayName, suffix);
ResourceMetadata meta = resource.getResourceMetadata();
long lastModified = meta.getModificationTime();
pw.print(" " + new Date(lastModified) + " ");
long length = meta.getContentLength();
if (length > 0) {
pw.print(length);
} else {
pw.print('-');
}
pw.println();
}
use of org.apache.sling.api.resource.ResourceMetadata in project acs-aem-commons by Adobe-Consulting-Services.
the class SelectComponent method buildComponentResource.
@Override
public Resource buildComponentResource() {
AbstractResourceImpl component = (AbstractResourceImpl) super.buildComponentResource();
AbstractResourceImpl options = new AbstractResourceImpl("items", null, null, new ResourceMetadata());
component.addChild(options);
String defaultValue = getOption("default").orElse(null);
getOptions().forEach((value, name) -> {
final ResourceMetadata meta = new ResourceMetadata();
final String nodeName = JcrUtil.escapeIllegalJcrChars(value);
if (value.equals(defaultValue)) {
meta.put("selected", true);
}
meta.put("value", value);
meta.put("text", name);
AbstractResourceImpl option = new AbstractResourceImpl("option_" + nodeName, null, null, meta);
options.addChild(option);
});
return component;
}
use of org.apache.sling.api.resource.ResourceMetadata in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AbstractDataSourceServlet method createResource.
/**
* Creates a virtual resource to use in a datasource.
*/
@NotNull
protected Resource createResource(@NotNull ResourceResolver resolver, @NotNull String textValue, @NotNull String valueValue) {
ValueMap properties = new ValueMapDecorator(new HashMap<>());
properties.put("text", textValue);
properties.put("value", valueValue);
return new ValueMapResource(resolver, new ResourceMetadata(), RESOURCE_TYPE_NON_EXISTING, properties);
}
Aggregations