Search in sources :

Example 6 with ResourceMetadata

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;
}
Also used : Resource(org.apache.sling.api.resource.Resource) SlingException(org.apache.sling.api.SlingException) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 7 with ResourceMetadata

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);
    }
}
Also used : ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 8 with ResourceMetadata

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();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) Date(java.util.Date)

Example 9 with ResourceMetadata

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;
}
Also used : AbstractResourceImpl(com.adobe.acs.commons.mcp.impl.AbstractResourceImpl) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 10 with ResourceMetadata

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);
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ValueMapResource(com.adobe.granite.ui.components.ds.ValueMapResource) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ResourceMetadata (org.apache.sling.api.resource.ResourceMetadata)31 Resource (org.apache.sling.api.resource.Resource)15 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)7 Test (org.junit.Test)6 AbstractResourceImpl (com.adobe.acs.commons.mcp.impl.AbstractResourceImpl)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 InputStream (java.io.InputStream)4 ValueMapResource (com.adobe.granite.ui.components.ds.ValueMapResource)3 HashMap (java.util.HashMap)3 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)3 BufferedInputStream (java.io.BufferedInputStream)2 ArrayList (java.util.ArrayList)2 Session (javax.jcr.Session)2 AccessControlManager (javax.jcr.security.AccessControlManager)2 Privilege (javax.jcr.security.Privilege)2 SlingException (org.apache.sling.api.SlingException)2 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)2 ScriptingResourceResolverProvider (org.apache.sling.scripting.api.resource.ScriptingResourceResolverProvider)2 SourceIdentifier (org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier)2 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)2