private void create(@NotNull final String path, @NotNull final Map<String, Object> properties) throws PersistenceException { final String parentPath = ResourceUtil.getParent(path); final String name = ResourceUtil.getName(path); final Resource parent = this.resolver.getResource(parentPath); this.resolver.create(parent, name, properties); }
private static Resource createResource(Resource root, String relPath) throws PersistenceException { ResourceResolver resourceResolver = root.getResourceResolver(); String path = root.getPath() + "/" + relPath; final String parentPath = ResourceUtil.getParent(path); final String name = ResourceUtil.getName(path); Resource parent = ResourceUtil.getOrCreateResource(resourceResolver, parentPath, RESOURCE_FOLDER, RESOURCE_FOLDER, false); Map<String, Object> props = Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) RESOURCE_FOLDER); return resourceResolver.create(parent, name, props); }
private String getActualContentPath(final String path){ final String checkPath; if ( path.endsWith(JCR_CONTENT_SUFFIX) ) { checkPath = ResourceUtil.getParent(path); } else { checkPath = path; } return checkPath; }
public MockPropertyResource(final String path, final ValueMap props, final ResourceResolver resolver) { this.path = path; this.props = props; this.key = ResourceUtil.getName(path); this.resolver = resolver; }
@Override public Resource getResource(final String path) { Resource resource = getResourceInternal(path); // if not resource found check if this is a reference to a property if (resource == null && path != null) { String parentPath = ResourceUtil.getParent(path); if (parentPath != null) { String name = ResourceUtil.getName(path); Resource parentResource = getResourceInternal(parentPath); if (parentResource!=null) { ValueMap props = ResourceUtil.getValueMap(parentResource); if (props.containsKey(name)) { return new MockPropertyResource(path, props, this); } } } } return resource; }
private String buildResourcePath(String path, String name) { return ResourceUtil.normalize(path + "/" + name); }
@Test public void testMapResourceAliasJcrContent() throws Exception { // define an alias for the rootPath in the jcr:content child node String alias = "testAlias"; Node content = rootNode.addNode("jcr:content", "nt:unstructured"); content.setProperty("sling:alias", alias); saveMappings(session); try { String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias); String mapped = resResolver.map(rootNode.getPath()); assertEquals(path, mapped); path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias+"/_jcr_content"); mapped = resResolver.map(content.getPath()); assertEquals(path, mapped); Node child = content.addNode("child"); session.save(); path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias+"/_jcr_content/child"); mapped = resResolver.map(child.getPath()); assertEquals(path, mapped); } finally { content.remove(); session.save(); } }
public static void getOrCreateBasePath(final ResourceResolver resolver, final String path) throws PersistenceException { ResourceUtil.getOrCreateResource(resolver, path, ResourceHelper.RESOURCE_TYPE_FOLDER, ResourceHelper.RESOURCE_TYPE_FOLDER, true); }
private PageCompareData load(Resource resource, String version) { if (resource != null && !ResourceUtil.isNonExistingResource(resource)) { try { return loader.load(resource, version); } catch (RepositoryException e) { log.error("Error loading data", e); } } return null; } }
void loadProperties(Resource resource) { ValueMap jcrVM = ResourceUtil.getValueMap(resource); Map<String, Object> localMap = new HashMap<String, Object>(); // copy all items to local map localMap.putAll(jcrVM); // decoarate it as value map ValueMapDecorator localVM = new ValueMapDecorator(localMap); this.vm = localVM; }
private String buildCrxDeLinkToResourceType(HttpServletRequest request, String type) { return getResourceResolver().map(r -> { try { String path = resourceTypeToPath(type); Resource resource = null; Resource iconResource = null; for (String searchPath : r.getSearchPath()) { resource = r.getResource(searchPath + path); if (resource != null && !isNonExistingResource(resource) && !isSyntheticResource(resource)) { iconResource = resource.getChild("icon.png"); break; } } return resource != null ? "<a href=\"" + request.getContextPath() + "/crx/de/#" + resource.getPath() + "\" " + "class=\"crxdelink\">" + "<img class=\"componentIcon\" src=\"" + getLabel() + API_PATH + API_COMPONENTICON + (iconResource == null ? "" : resource.getPath()) + "\"/>" + type + "</a>" : "<span class=\"unresolved\">" + type + "</span>"; } finally { r.close(); } }).orElse("#"); }
/** * Does the resource exist ? * @param resource * @return */ private boolean resourceExists(final Resource resource) { return (resource != null && !ResourceUtil.isSyntheticResource(resource)); }
@Override public Resource getParent() { if (path == null) { return null; } return SyntheticNavigatableResource.get(ResourceUtil.getParent(path), resolver); }
/** * @see org.apache.sling.api.resource.AbstractResource#getName() */ @Override public String getName() { return ResourceUtil.getName(this.path); }
protected String makeAbsolutePath(final String relativePath) { String absolutePath = relativePath; // ensure the path is absolute and normalized if (!StringUtils.startsWith(absolutePath, "/")) { absolutePath = this.path + "/" + absolutePath; //NOPMD } return ResourceUtil.normalize(absolutePath); }
@Test public void testMapResourceAlias() throws Exception { // define an alias for the rootPath String alias = "testAlias"; rootNode.setProperty("sling:alias", alias); saveMappings(session); try { String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias); String mapped = resResolver.map(rootNode.getPath()); assertEquals(path, mapped); Node child = rootNode.addNode("child"); session.save(); path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias+"/child"); mapped = resResolver.map(child.getPath()); assertEquals(path, mapped); } finally { rootNode.getProperty("sling:alias").remove(); if ( rootNode.hasNode("child") ) { rootNode.getNode("child").remove(); } session.save(); } }
/** * Create a configuration resource * @param resolver current resolver * @param path path of the resource * @param type type of the node to be created * @param data map of properties to add * @throws PersistenceException in case configuration resource couldn't be persisted * @return resource created */ protected Resource createResource(ResourceResolver resolver, String path, String type, Map data) throws PersistenceException { return ResourceUtil.getOrCreateResource(resolver, path, data, type, false); }
/** * @see org.apache.sling.api.resource.ResourceResolver#delete(org.apache.sling.api.resource.Resource) */ @Override public void delete(final Resource resource) throws PersistenceException { // check if the resource is non existing - throws NPE if resource is null as stated in the API if ( ResourceUtil.isNonExistingResource(resource) ) { // nothing to do return; } // if resource is null, we get an NPE as stated in the API this.control.delete(this.context, resource); }
public static ValueMap getValueMap(final Resource resource) throws InstantiationException { final ValueMap vm = ResourceUtil.getValueMap(resource); // trigger full loading try { vm.size(); } catch ( final IllegalArgumentException iae) { // the JCR implementation might throw an IAE if something goes wrong throw (InstantiationException)new InstantiationException(iae.getMessage()).initCause(iae); } return vm; }