use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.
the class XmlSiteParser method toMappingDescriptor.
private ControllerMappingDescriptor toMappingDescriptor(final DomElement mappingElement) {
final ControllerMappingDescriptor.Builder builder = ControllerMappingDescriptor.create();
final String controllerPath = mappingElement.getAttribute(MAPPING_DESCRIPTOR_CONTROLLER_ATTRIBUTE);
if (!nullToEmpty(controllerPath).isBlank()) {
builder.controller(ResourceKey.from(this.currentApplication, controllerPath));
}
final String filterPath = mappingElement.getAttribute(MAPPING_DESCRIPTOR_FILTER_ATTRIBUTE);
if (!nullToEmpty(filterPath).isBlank()) {
builder.filter(ResourceKey.from(this.currentApplication, filterPath));
}
final String orderValue = mappingElement.getAttribute(MAPPING_DESCRIPTOR_ORDER_ATTRIBUTE);
if (!isNullOrEmpty(orderValue)) {
builder.order(Integer.parseInt(orderValue));
}
final DomElement matchElement = mappingElement.getChild(MAPPING_DESCRIPTOR_MATCH_TAG_NAME);
if (matchElement != null) {
final String match = matchElement.getValue();
if (!isNullOrEmpty(match)) {
builder.contentConstraint(match);
}
}
final DomElement patternElement = mappingElement.getChild(MAPPING_DESCRIPTOR_PATTERN_TAG_NAME);
if (patternElement != null) {
final String pattern = patternElement.getValue();
if (!isNullOrEmpty(pattern)) {
final boolean invert = "true".equals(patternElement.getAttribute(MAPPING_DESCRIPTOR_INVERT_ATTRIBUTE, "false"));
builder.pattern(pattern);
builder.invertPattern(invert);
}
}
return builder.build();
}
use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.
the class MappingHandlerTest method executeFilter.
@Test
public void executeFilter() throws Exception {
final ResourceKey filter = ResourceKey.from("demo:/services/test");
final ControllerMappingDescriptor mapping = ControllerMappingDescriptor.create().filter(filter).pattern(".*/content").build();
setupContentAndSite(mapping);
this.request.setBaseUri("/site");
this.request.setContentPath(ContentPath.from("/site/somesite/content"));
final WebResponse response = this.handler.handle(this.request, PortalResponse.create().build(), null);
assertEquals(HttpStatus.OK, response.getStatus());
assertNotNull(this.request.getApplicationKey());
assertNotNull(this.request.getSite());
assertNotNull(this.request.getContent());
assertEquals("/site/draft/site", this.request.getContextPath());
}
use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.
the class ControllerMappingsResolverTest method newSiteDescriptor2.
private SiteDescriptor newSiteDescriptor2() {
final ControllerMappingDescriptor mapping1 = ControllerMappingDescriptor.create().controller(ResourceKey.from(getAppKey2(), "/other/controller1.js")).pattern("/.*").contentConstraint("_id:'123456'").order(10).build();
final ControllerMappingDescriptor mapping2 = ControllerMappingDescriptor.create().controller(ResourceKey.from(getAppKey2(), "/other/controller2.js")).pattern("/.*").contentConstraint("_path:'/mysite/landing-page'").order(5).build();
final ControllerMappingDescriptors mappings = ControllerMappingDescriptors.from(mapping1, mapping2);
return SiteDescriptor.create().mappingDescriptors(mappings).build();
}
use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.
the class ControllerMappingsResolverTest method testResolve.
@Test
public void testResolve() {
final Content content = newContent();
final Site site = newSite();
final SiteDescriptor siteDescriptor = newSiteDescriptor();
Mockito.when(this.siteService.getDescriptor(getAppKey())).thenReturn(siteDescriptor);
final SiteDescriptor siteDescriptor2 = newSiteDescriptor2();
Mockito.when(this.siteService.getDescriptor(getAppKey2())).thenReturn(siteDescriptor2);
final ControllerMappingsResolver resolver = new ControllerMappingsResolver(this.siteService);
final Optional<ControllerMappingDescriptor> mapping = resolver.resolve("/landing-page", ImmutableMultimap.of(), content, site.getSiteConfigs());
assertThat(mapping).map(ControllerMappingDescriptor::getController).map(ResourceKey::getPath).contains("/site/controllers/controller2.js");
}
use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.
the class ControllerMappingsResolverTest method testPatternCatchAll_matches_anything.
@Test
public void testPatternCatchAll_matches_anything() {
final ControllerMappingDescriptor mapping1 = ControllerMappingDescriptor.create().controller(ResourceKey.from(getAppKey2(), "/other/controller1.js")).pattern("/.*").order(10).build();
final ControllerMappingDescriptors mappings = ControllerMappingDescriptors.from(mapping1);
final Content content = newContent();
final Site site = newSite();
final SiteDescriptor siteDescriptor = SiteDescriptor.create().mappingDescriptors(mappings).build();
Mockito.when(this.siteService.getDescriptor(getAppKey2())).thenReturn(siteDescriptor);
final ControllerMappingsResolver resolver = new ControllerMappingsResolver(this.siteService);
final Optional<ControllerMappingDescriptor> mapping = resolver.resolve("/landing-page", ImmutableMultimap.of(), content, site.getSiteConfigs());
assertThat(mapping).map(ControllerMappingDescriptor::getController).map(ResourceKey::getPath).contains("/other/controller1.js");
final Optional<ControllerMappingDescriptor> mapping2 = resolver.resolve("/does-not-exist", ImmutableMultimap.of(), null, site.getSiteConfigs());
assertThat(mapping2).map(ControllerMappingDescriptor::getController).map(ResourceKey::getPath).contains("/other/controller1.js");
final Optional<ControllerMappingDescriptor> mapping3 = resolver.resolve("/", ImmutableMultimap.of(), site, site.getSiteConfigs());
assertThat(mapping3).map(ControllerMappingDescriptor::getController).map(ResourceKey::getPath).contains("/other/controller1.js");
}
Aggregations