use of org.apache.sling.api.resource.ResourceWrapper in project sling by apache.
the class ResourceResolverWrapperTest method testGetResource1.
@Test
public void testGetResource1() throws Exception {
final Resource parent = mock(Resource.class);
final Resource resource = mock(Resource.class);
when(resource.getPath()).thenReturn(PATH);
when(wrappedResolver.getResource(parent, PATH)).thenReturn(resource);
final Resource result = underTest.getResource(parent, PATH);
assertTrue(result instanceof ResourceWrapper);
assertEquals(underTest, result.getResourceResolver());
assertEquals(resource.getPath(), result.getPath());
verify(wrappedResolver).getResource(parent, PATH);
}
use of org.apache.sling.api.resource.ResourceWrapper in project sling by apache.
the class PathBasedResourceDecorator method decorate.
/** Return a resource type for given node, if we have a mapping that applies */
public Resource decorate(Resource resource) {
String result = null;
if (mappings != null) {
// let's check when we should apply the mapping
// 1. if the resource is a star resource
boolean apply = false;
String resourceType = null;
if (resource.getPath().endsWith("/*")) {
apply = true;
resourceType = Mapping.DEFAULT_NODE_TYPE;
} else {
// and the primary node type equals the resource type
try {
final Node node = resource.adaptTo(Node.class);
if (node != null && node.getPrimaryNodeType().getName().equals(resource.getResourceType())) {
apply = true;
resourceType = resource.getResourceType();
}
} catch (RepositoryException re) {
// we ignore this
}
}
if (apply) {
final String path = resource.getPath();
for (Mapping m : mappings) {
result = m.getResourceType(path, resourceType);
if (result != null) {
log.debug("Default resource type {} used for resource {}", result, path);
break;
}
}
}
}
if (result == null && log.isDebugEnabled()) {
log.debug("No Mapping applies to node {}, no resource type provided", resource.getPath());
}
if (result != null) {
final String resourceType = result;
return new ResourceWrapper(resource) {
@Override
public String getResourceType() {
return resourceType;
}
};
}
return resource;
}
use of org.apache.sling.api.resource.ResourceWrapper in project sling by apache.
the class SuperimposingResourceProviderImplTest method testListChildrenWithResourceWrapper.
@Test
public void testListChildrenWithResourceWrapper() {
Resource resource = underTest.getResource(resourceResolver, SUPERIMPOSED_PATH);
Iterator<Resource> iterator = underTest.listChildren(new ResourceWrapper(resource));
assertTrue(iterator.hasNext());
assertEquals(SUPERIMPOSED_PATH + "/sub1", iterator.next().getPath());
}
use of org.apache.sling.api.resource.ResourceWrapper in project sling by apache.
the class ProcessorConfigurationImpl method match.
/**
* @see org.apache.sling.rewriter.ProcessorConfiguration#match(org.apache.sling.rewriter.ProcessingContext)
*/
public boolean match(final ProcessingContext processContext) {
if (!this.processErrorResponse && processContext.getRequest().getAttribute("javax.servlet.error.status_code") != null) {
return false;
}
final Object pipelineName = processContext.getRequest().getAttribute(ATTR_PIPELINE);
if (pipelineName != null && !pipelineName.equals(this.name)) {
return false;
}
String contentType = processContext.getContentType();
// if no content type is supplied, we assume html
if (contentType == null) {
contentType = ProcessorManagerImpl.MIME_TYPE_HTML;
} else {
final int idx = contentType.indexOf(';');
if (idx != -1) {
contentType = contentType.substring(0, idx);
}
}
// if no content type is configured we apply to all
if (this.contentTypes != null && this.contentTypes.length > 0) {
int index = 0;
boolean found = false;
while (!found && index < this.contentTypes.length) {
if (this.contentTypes[index].equals("*")) {
found = true;
} else if (this.contentTypes[index].equals(contentType)) {
found = true;
}
index++;
}
if (!found) {
return false;
}
}
// if no extenstion is configured, we apply to all extenstions
if (this.extensions != null && this.extensions.length > 0) {
boolean found = false;
int index = 0;
while (!found && index < this.extensions.length) {
if (this.extensions[index].equals(processContext.getRequest().getRequestPathInfo().getExtension())) {
found = true;
}
index++;
}
if (!found) {
return false;
}
}
// check resource types
if (this.resourceTypes != null && this.resourceTypes.length > 0) {
final ResourceResolver resourceResolver = processContext.getRequest().getResourceResolver();
final Resource resource = processContext.getRequest().getResource();
boolean found = false;
int index = 0;
while (!found && index < this.resourceTypes.length) {
if (resourceResolver.isResourceType(resource, resourceTypes[index])) {
found = true;
} else if (unwrapResources && resource instanceof ResourceWrapper) {
// accept resource as well if type was overridden and unwrapped resource has a matching type
final Resource unwrappedResource = unwrap(resource);
if (resourceResolver.isResourceType(unwrappedResource, resourceTypes[index])) {
found = true;
}
}
index++;
}
if (!found) {
return false;
}
}
// if no path is configured, we apply to all paths
if (this.paths != null && this.paths.length > 0) {
final String path = processContext.getRequest().getRequestPathInfo().getResourcePath();
int index = 0;
boolean found = false;
while (!found && index < this.paths.length) {
if (this.paths[index].equals("*")) {
found = true;
} else if (path.startsWith(this.paths[index])) {
found = true;
}
index++;
}
if (!found) {
return false;
}
}
// now check for selectors
if (this.selectors != null && this.selectors.length > 0) {
final String selectorString = processContext.getRequest().getRequestPathInfo().getSelectorString();
if (selectorString == null || "".equals(selectorString)) {
// selectors required but not set
return false;
}
final Set<String> selectors = new HashSet<String>(Arrays.asList(selectorString.split("\\.")));
int index = 0;
boolean found = false;
while (!found && index < this.selectors.length) {
final String selector = this.selectors[index];
if (selectors.contains(selector)) {
found = true;
}
index++;
}
if (!found) {
return false;
}
}
return true;
}
use of org.apache.sling.api.resource.ResourceWrapper in project sling by apache.
the class ProcessorConfigurationImplTest method testMatchAtLeastOneResourceTypeWithResourceWrapper_UnwrapEnabled.
@Test
public void testMatchAtLeastOneResourceTypeWithResourceWrapper_UnwrapEnabled() {
Resource resource = context.create().resource("/content/test", ImmutableMap.<String, Object>of("sling:resourceType", "type/1"));
// overwrite resource type via ResourceWrapper
Resource resourceWrapper = new ResourceWrapper(resource) {
@Override
public String getResourceType() {
return "/type/override/1";
}
};
context.currentResource(resourceWrapper);
assertMatch(ImmutableMap.<String, Object>of(PROPERTY_RESOURCE_TYPES, new String[] { "type/1", "type/2" }, PROPERTY_UNWRAP_RESOURCES, true));
}
Aggregations