Search in sources :

Example 11 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class AllowedColorSwatchesDataSourceServlet method getAllowedColorSwatches.

protected List<Resource> getAllowedColorSwatches(@NotNull SlingHttpServletRequest request) {
    List<Resource> colors = new ArrayList<>();
    ResourceResolver resolver = request.getResourceResolver();
    Resource contentResource = resolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));
    ContentPolicyManager policyMgr = resolver.adaptTo(ContentPolicyManager.class);
    if (policyMgr != null) {
        ContentPolicy policy = policyMgr.getPolicy(contentResource);
        if (policy != null) {
            ValueMap color = null;
            ValueMap properties = policy.getProperties();
            if (properties != null) {
                String[] allowedColorSwatches = properties.get(PN_ALLOWED_COLOR_SWATCHES, String[].class);
                if (allowedColorSwatches != null && allowedColorSwatches.length > 0) {
                    for (String allowedColorSwatch : allowedColorSwatches) {
                        color = new ValueMapDecorator(new HashMap<String, Object>());
                        color.put(PN_COLOR_VALUE, allowedColorSwatch);
                        colors.add(new ValueMapResource(resolver, new ResourceMetadata(), JcrConstants.NT_UNSTRUCTURED, color));
                    }
                }
            }
        }
    }
    return colors;
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapResource(com.adobe.granite.ui.components.ds.ValueMapResource) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) ContentPolicy(com.day.cq.wcm.api.policies.ContentPolicy) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) ContentPolicyManager(com.day.cq.wcm.api.policies.ContentPolicyManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ValueMapResource(com.adobe.granite.ui.components.ds.ValueMapResource)

Example 12 with ResourceMetadata

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

the class StarResource method getResourceMetadata.

/** Get our ResourceMetadata for given path */
static ResourceMetadata getResourceMetadata(String path) {
    ResourceMetadata result = new ResourceMetadata();
    // The path is up to /*, what follows is pathInfo
    final int index = path.indexOf(SLASH_STAR);
    if (index >= 0) {
        result.setResolutionPath(path.substring(0, index) + SLASH_STAR);
        result.setResolutionPathInfo(path.substring(index + SLASH_STAR.length()));
    } else {
        result.setResolutionPath(path);
    }
    return result;
}
Also used : ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 13 with ResourceMetadata

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

the class FileRetrievalIT method createAndeRtrieveFile.

@Test
public void createAndeRtrieveFile() throws IOException, ServletException {
    final String expected = "yes, it worked";
    final long startTime = System.currentTimeMillis();
    final String mimeType = "application/javascript";
    E.builder.resource("somefolder").file("the-model.js", getClass().getResourceAsStream("/files/models.js")).commit();
    final Resource r = A.assertFile("somefolder/the-model.js", mimeType, expected, -1L);
    final ResourceMetadata meta = r.getResourceMetadata();
    assertTrue("Expecting a last modified time >= startTime", meta.getModificationTime() >= startTime);
    assertEquals("Expecting the correct mime-type", mimeType, meta.getContentType());
    final InputStream is = r.adaptTo(InputStream.class);
    assertNotNull("Expecting InputStream for file resource " + r.getPath(), is);
    try {
        final String content = A.readFully(is);
        assertTrue("Expecting [" + expected + "] in content", content.contains(expected));
    } finally {
        is.close();
    }
}
Also used : InputStream(java.io.InputStream) Resource(org.apache.sling.api.resource.Resource) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) Test(org.junit.Test)

Example 14 with ResourceMetadata

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

the class JcrResourceBundle method loadJsonDictionary.

private void loadJsonDictionary(Resource resource, final Map<String, Object> targetDictionary) {
    log.info("Loading json dictionary: {}", resource.getPath());
    // use streaming parser (we don't need the dict in memory twice)
    JsonParser parser = new JsonParser(new JsonHandler() {

        private String key;

        @Override
        public void key(String key) throws IOException {
            this.key = key;
        }

        @Override
        public void value(String value) throws IOException {
            targetDictionary.put(key, value);
        }

        @Override
        public void object() throws IOException {
        }

        @Override
        public void endObject() throws IOException {
        }

        @Override
        public void array() throws IOException {
        }

        @Override
        public void endArray() throws IOException {
        }

        @Override
        public void value(boolean value) throws IOException {
        }

        @Override
        public void value(long value) throws IOException {
        }

        @Override
        public void value(double value) throws IOException {
        }
    });
    final InputStream stream = resource.adaptTo(InputStream.class);
    if (stream != null) {
        String encoding = "utf-8";
        final ResourceMetadata metadata = resource.getResourceMetadata();
        if (metadata.getCharacterEncoding() != null) {
            encoding = metadata.getCharacterEncoding();
        }
        try {
            parser.parse(stream, encoding);
        } catch (IOException e) {
            log.warn("Could not parse i18n json dictionary {}: {}", resource.getPath(), e.getMessage());
        } finally {
            try {
                stream.close();
            } catch (IOException ignore) {
            }
        }
    } else {
        log.warn("Not a json file: {}", resource.getPath());
    }
}
Also used : InputStream(java.io.InputStream) JsonHandler(org.apache.jackrabbit.commons.json.JsonHandler) IOException(java.io.IOException) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) JsonParser(org.apache.jackrabbit.commons.json.JsonParser)

Example 15 with ResourceMetadata

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

the class ResourceProviderBasedResourceDecorator method decorate.

/**
	 * @see org.apache.sling.api.resource.ResourceDecorator#decorate(org.apache.sling.api.resource.Resource)
	 */
public Resource decorate(Resource resource) {
    Resource result = null;
    if (resource != null) {
        ResourceMetadata resourceMetadata = resource.getResourceMetadata();
        if (resourceMetadata != null) {
            String resolutionPathInfo = resourceMetadata.getResolutionPathInfo();
            result = getResourceEditorResourceWrapper(resource, resolutionPathInfo);
        }
    }
    return result;
}
Also used : Resource(org.apache.sling.api.resource.Resource) 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