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;
}
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;
}
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();
}
}
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());
}
}
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;
}
Aggregations