I need to get the parent folder name (String type), once a user provides a filepath in cq dialog. This is my approach:
import lombok.Getter;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.*;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
@Getter
@Model(adaptables = {
Resource.class,
SlingHttpServletRequest.class
},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Test {
@SlingObject
private Resource resource;
@OSGiService
private ResourceResolver resourceResolver;
@ValueMapValue
private String fileUrl;
@PostConstruct
public String getData() {
Resource resource = resourceResolver.getResource(fileUrl);
Resource parentProps = resource.getParent();
System.out.println("parent node is =>" + parentProps);
}
}
Is there a problem? My code builds correctly but does not return anything
ResourceResolver is not a OSGI service, its SlingObject
Also you need to make sure that logged-in user making the request have right access setup for the file path