Search in sources :

Example 26 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.

the class StreamRendererServlet method processRequest.

private void processRequest(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    // whether this servlet is called as of a request include
    final boolean included = request.getAttribute(SlingConstants.ATTR_REQUEST_SERVLET) != null;
    // ensure no extension or "res"
    String ext = request.getRequestPathInfo().getExtension();
    if (ext != null && !ext.equals(EXT_RES)) {
        request.getRequestProgressTracker().log("StreamRendererServlet does not support for extension " + ext);
        if (included || response.isCommitted()) {
            log.error("StreamRendererServlet does not support extension {}", ext);
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
        return;
    }
    Resource resource = request.getResource();
    if (ResourceUtil.isNonExistingResource(resource)) {
        throw new ResourceNotFoundException("No data to render.");
    }
    if ("/".equals(request.getRequestPathInfo().getSuffix())) {
        renderDirectory(request, response, included);
        return;
    }
    // check the last modification time and If-Modified-Since header
    if (!included) {
        ResourceMetadata meta = resource.getResourceMetadata();
        long modifTime = meta.getModificationTime();
        if (unmodified(request, modifTime)) {
            response.setStatus(SC_NOT_MODIFIED);
            return;
        }
    }
    // fall back to plain text rendering if the resource has no stream
    if (resource.getResourceType().equals(JcrConstants.NT_LINKEDFILE)) {
        final ValueMap vm = resource.adaptTo(ValueMap.class);
        final String actualResourcePath = vm.get(JcrConstants.JCR_CONTENT, String.class);
        resource = request.getResourceResolver().getResource(actualResourcePath);
    }
    InputStream stream = resource.adaptTo(InputStream.class);
    if (stream != null) {
        if (isHeadRequest(request)) {
            setContentLength(response, resource.getResourceMetadata().getContentLength());
            setHeaders(resource, response);
            return;
        }
        streamResource(resource, stream, included, request, response);
    } else {
        // the resource is the root, do not redirect, immediately index
        if (isRootResourceRequest(resource)) {
            renderDirectory(request, response, included);
        } else if (included || response.isCommitted()) {
            // request is included or committed, not redirecting
            request.getRequestProgressTracker().log("StreamRendererServlet: Not redirecting with trailing slash, response is committed or request included");
            log.warn("StreamRendererServlet: Not redirecting with trailing slash, response is committed or request included");
        } else {
            // redirect to this with trailing slash to render the index
            String url = request.getResourceResolver().map(request, resource.getPath()) + "/";
            response.sendRedirect(url);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 27 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project acs-aem-commons by Adobe-Consulting-Services.

the class RadioComponent 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("checked", true);
        }
        meta.put("name", getName());
        meta.put("value", value);
        meta.put("text", name);
        AbstractResourceImpl option = new AbstractResourceImpl("option_" + nodeName, "granite/ui/components/foundation/form/radio", "granite/ui/components/foundation/form/field", meta);
        options.addChild(option);
    });
    return component;
}
Also used : AbstractResourceImpl(com.adobe.acs.commons.mcp.impl.AbstractResourceImpl) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 28 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project acs-aem-commons by Adobe-Consulting-Services.

the class FolderRelocatorTest method getEnhancedMockResolver.

private ResourceResolver getEnhancedMockResolver() throws RepositoryException, LoginException {
    ResourceResolver rr = getFreshMockResolver();
    when(rr.hasChanges()).thenReturn(true);
    AbstractResourceImpl mockFolderA = new AbstractResourceImpl("/content/folderA", "", "", new ResourceMetadata());
    when(rr.getResource("/content/folderA")).thenReturn(mockFolderA);
    AbstractResourceImpl mockFolderB = new AbstractResourceImpl("/content/folderB", "", "", new ResourceMetadata());
    when(rr.getResource("/content/folderB")).thenReturn(mockFolderB);
    AbstractResourceImpl mockFolder = new AbstractResourceImpl("/content", "", "", new ResourceMetadata());
    when(rr.getResource("/content")).thenReturn(mockFolder);
    mockFolder.addChild(mockFolderA);
    mockFolder.addChild(mockFolderB);
    Session ses = mock(Session.class);
    when(rr.adaptTo(Session.class)).thenReturn(ses);
    AccessControlManager acm = mock(AccessControlManager.class);
    when(ses.getAccessControlManager()).thenReturn(acm);
    when(acm.privilegeFromName(any())).thenReturn(mock(Privilege.class));
    return rr;
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) AbstractResourceImpl(com.adobe.acs.commons.mcp.impl.AbstractResourceImpl) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) Privilege(javax.jcr.security.Privilege) Session(javax.jcr.Session)

Example 29 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project acs-aem-commons by Adobe-Consulting-Services.

the class PageRelocatorTest method getEnhancedMockResolver.

private ResourceResolver getEnhancedMockResolver(final boolean aclChecksPass) throws RepositoryException, LoginException {
    ResourceResolver rr = getFreshMockResolver();
    when(rr.hasChanges()).thenReturn(true);
    AbstractResourceImpl pageA = new AbstractResourceImpl("/content/pageA", "cq:Page", "cq:Page", new ResourceMetadata());
    when(rr.getResource("/content/pageA")).thenReturn(pageA);
    AbstractResourceImpl pageAcontent = new AbstractResourceImpl("/content/pageA/jcr:content", "cq:PageContent", "cq:PageContent", new ResourceMetadata());
    when(rr.getResource("/content/pageA/jcr:content")).thenReturn(pageAcontent);
    AbstractResourceImpl pageB = new AbstractResourceImpl("/content/pageB", "cq:Page", "cq:Page", new ResourceMetadata());
    when(rr.getResource("/content/pageB")).thenReturn(pageB);
    AbstractResourceImpl content = new AbstractResourceImpl("/content", "cq:Page", "cq:Page", new ResourceMetadata());
    AbstractResourceImpl pageBcontent = new AbstractResourceImpl("/content/pageB/jcr:content", "cq:PageContent", "cq:PageContent", new ResourceMetadata());
    when(rr.getResource("/content/pageB/jcr:content")).thenReturn(pageBcontent);
    when(rr.getResource("/content")).thenReturn(content);
    content.addChild(pageA);
    content.addChild(pageB);
    AbstractResourceImpl processes = new AbstractResourceImpl(ProcessInstanceImpl.BASE_PATH, null, null, new ResourceMetadata());
    when(rr.getResource(ProcessInstanceImpl.BASE_PATH)).thenReturn(processes);
    Session ses = mock(Session.class);
    when(rr.adaptTo(Session.class)).thenReturn(ses);
    AccessControlManager acm = mock(AccessControlManager.class);
    when(ses.getAccessControlManager()).thenReturn(acm);
    when(acm.privilegeFromName(any())).thenReturn(mock(Privilege.class));
    when(acm.hasPrivileges(any(), any())).thenReturn(aclChecksPass);
    return rr;
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) AbstractResourceImpl(com.adobe.acs.commons.mcp.impl.AbstractResourceImpl) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) Privilege(javax.jcr.security.Privilege) Session(javax.jcr.Session)

Example 30 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project acs-aem-commons by Adobe-Consulting-Services.

the class PageRelocatorTest method initInstance.

private void initInstance(ProcessInstance instance, ResourceResolver rr) throws RepositoryException, DeserializeException {
    Map<String, Object> values = new HashMap<>();
    values.put("sourcePath", "/content/pageA");
    values.put("destinationPath", "/content/pageB");
    values.put("maxReferences", "0");
    values.put("mode", PageRelocator.Mode.MOVE.toString());
    values.put("publishMethod", PageRelocator.PublishMethod.SELF_MANAGED.toString());
    values.put("createVerionsOnReplicate", "false");
    values.put("updateStatus", "true");
    values.put("dryRun", "false");
    instance.init(rr, values);
    AbstractResourceImpl processNode = new AbstractResourceImpl(instance.getPath(), null, null, new ResourceMetadata());
    when(rr.getResource(instance.getPath())).thenReturn(processNode);
    AbstractResourceImpl processContentNode = new AbstractResourceImpl(instance.getPath() + "/jcr:content", null, null, new ResourceMetadata());
    when(rr.getResource(instance.getPath() + "/jcr:content")).thenReturn(processContentNode);
    AbstractResourceImpl processResultNode = new AbstractResourceImpl(instance.getPath() + "/jcr:content/result", null, null, new ResourceMetadata());
    when(rr.getResource(instance.getPath() + "/jcr:content/result")).thenReturn(processResultNode);
    AbstractResourceImpl failuresNode = new AbstractResourceImpl(instance.getPath() + "/jcr:content/failures", null, null, new ResourceMetadata());
    when(rr.getResource(instance.getPath() + "/jcr:content/failures")).thenReturn(processResultNode);
    when(rr.getResource(instance.getPath() + "/jcr:content/failures/step1")).thenReturn(processResultNode);
}
Also used : HashMap(java.util.HashMap) AbstractResourceImpl(com.adobe.acs.commons.mcp.impl.AbstractResourceImpl) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

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