Search in sources :

Example 11 with ControllerMappingDescriptor

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();
}
Also used : DomElement(com.enonic.xp.xml.DomElement) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor)

Example 12 with ControllerMappingDescriptor

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());
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 13 with ControllerMappingDescriptor

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();
}
Also used : ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) ControllerMappingDescriptors(com.enonic.xp.site.mapping.ControllerMappingDescriptors)

Example 14 with ControllerMappingDescriptor

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");
}
Also used : Site(com.enonic.xp.site.Site) SiteDescriptor(com.enonic.xp.site.SiteDescriptor) Content(com.enonic.xp.content.Content) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) Test(org.junit.jupiter.api.Test)

Example 15 with ControllerMappingDescriptor

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");
}
Also used : Site(com.enonic.xp.site.Site) SiteDescriptor(com.enonic.xp.site.SiteDescriptor) Content(com.enonic.xp.content.Content) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) ControllerMappingDescriptors(com.enonic.xp.site.mapping.ControllerMappingDescriptors) Test(org.junit.jupiter.api.Test)

Aggregations

ControllerMappingDescriptor (com.enonic.xp.site.mapping.ControllerMappingDescriptor)17 Test (org.junit.jupiter.api.Test)11 Site (com.enonic.xp.site.Site)7 SiteDescriptor (com.enonic.xp.site.SiteDescriptor)7 Content (com.enonic.xp.content.Content)6 ControllerMappingDescriptors (com.enonic.xp.site.mapping.ControllerMappingDescriptors)6 ResourceKey (com.enonic.xp.resource.ResourceKey)3 ApplicationKey (com.enonic.xp.app.ApplicationKey)2 WebResponse (com.enonic.xp.web.WebResponse)2 PortalRequest (com.enonic.xp.portal.PortalRequest)1 PortalResponse (com.enonic.xp.portal.PortalResponse)1 ContentResolverResult (com.enonic.xp.portal.impl.ContentResolverResult)1 HttpMethod (com.enonic.xp.web.HttpMethod)1 WebException (com.enonic.xp.web.WebException)1 WebHandlerChain (com.enonic.xp.web.handler.WebHandlerChain)1 DomElement (com.enonic.xp.xml.DomElement)1