Search in sources :

Example 6 with ControllerMappingDescriptor

use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.

the class ControllerMappingsResolverTest method newDescriptorForFragments.

private SiteDescriptor newDescriptorForFragments() {
    final ControllerMappingDescriptor mapping1 = ControllerMappingDescriptor.create().controller(ResourceKey.from(getAppKey(), "/site/controllers/controller1.js")).pattern("/.*").contentConstraint("type:'portal:fragment'").order(10).build();
    final ControllerMappingDescriptors mappings = ControllerMappingDescriptors.from(mapping1);
    return SiteDescriptor.create().mappingDescriptors(mappings).build();
}
Also used : ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) ControllerMappingDescriptors(com.enonic.xp.site.mapping.ControllerMappingDescriptors)

Example 7 with ControllerMappingDescriptor

use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.

the class ControllerMappingsResolverTest method newSiteDescriptor3.

private SiteDescriptor newSiteDescriptor3() {
    final ControllerMappingDescriptor mapping1 = ControllerMappingDescriptor.create().controller(ResourceKey.from(getAppKey2(), "/other/controller1.js")).pattern("/.*api.*\\?category=.*&key=\\d+").order(10).build();
    final ControllerMappingDescriptors mappings = ControllerMappingDescriptors.from(mapping1);
    return SiteDescriptor.create().mappingDescriptors(mappings).build();
}
Also used : ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) ControllerMappingDescriptors(com.enonic.xp.site.mapping.ControllerMappingDescriptors)

Example 8 with ControllerMappingDescriptor

use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.

the class ControllerMappingsResolverTest method testResolvePatternWithParametersNoMatch.

@Test
public void testResolvePatternWithParametersNoMatch() {
    final Content content = newContent();
    final Site site = newSite();
    final SiteDescriptor siteDescriptor = newSiteDescriptor3();
    Mockito.when(this.siteService.getDescriptor(getAppKey2())).thenReturn(siteDescriptor);
    final ControllerMappingsResolver resolver = new ControllerMappingsResolver(this.siteService);
    final Optional<ControllerMappingDescriptor> mapping = resolver.resolve("/api", ImmutableMultimap.of(), content, site.getSiteConfigs());
    assertTrue(mapping.isEmpty());
}
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 9 with ControllerMappingDescriptor

use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.

the class MappingHandler method handle.

@Override
public WebResponse handle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    if (!(webRequest instanceof PortalRequest) || webRequest.getEndpointPath() != null) {
        return webHandlerChain.handle(webRequest, webResponse);
    }
    final PortalRequest request = (PortalRequest) webRequest;
    if (request.getMode() == RenderMode.ADMIN || !request.isSiteBase()) {
        return webHandlerChain.handle(webRequest, webResponse);
    }
    WebHandlerHelper.checkAdminAccess(request);
    final HttpMethod method = webRequest.getMethod();
    if (!HttpMethod.standard().contains(method)) {
        throw new WebException(HttpStatus.METHOD_NOT_ALLOWED, String.format("Method %s not allowed", method));
    }
    final ContentResolverResult resolvedContent = contentResolver.resolve(request);
    final Site site = resolvedContent.getNearestSite();
    if (site == null) {
        return webHandlerChain.handle(request, webResponse);
    }
    final Content content = resolvedContent.getContent();
    final Optional<ControllerMappingDescriptor> resolve = controllerMappingsResolver.resolve(resolvedContent.getSiteRelativePath(), request.getParams(), content, site.getSiteConfigs());
    if (resolve.isPresent()) {
        final ControllerMappingDescriptor mapping = resolve.get();
        request.setContent(content);
        request.setSite(site);
        request.setContextPath(request.getBaseUri() + "/" + request.getBranch() + site.getPath());
        request.setApplicationKey(mapping.getApplication());
        if (mapping.isController()) {
            return handleController(request, mapping);
        } else {
            return handleFilter(request, webResponse, webHandlerChain, mapping);
        }
    } else {
        return webHandlerChain.handle(request, webResponse);
    }
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) Site(com.enonic.xp.site.Site) WebException(com.enonic.xp.web.WebException) Content(com.enonic.xp.content.Content) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) HttpMethod(com.enonic.xp.web.HttpMethod) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 10 with ControllerMappingDescriptor

use of com.enonic.xp.site.mapping.ControllerMappingDescriptor in project xp by enonic.

the class XmlSiteParserTest method testSiteXmlDeserializationWithMappings.

@Test
public void testSiteXmlDeserializationWithMappings() {
    final String xml = loadTestXml("serialized-site-with-mappings.xml");
    final SiteDescriptor.Builder siteDescriptorBuilder = SiteDescriptor.create();
    ApplicationKey applicationKey = ApplicationKey.from("myapplication");
    this.parser.source(xml).currentApplication(applicationKey).siteDescriptorBuilder(siteDescriptorBuilder).parse();
    SiteDescriptor siteDescriptor = siteDescriptorBuilder.build();
    assertEquals(1, siteDescriptor.getForm().getFormItems().size());
    assertEquals(2, siteDescriptor.getXDataMappings().getSize());
    assertEquals(2, siteDescriptor.getResponseProcessors().getSize());
    assertEquals(3, siteDescriptor.getMappingDescriptors().getSize());
    final ControllerMappingDescriptor mapping1 = siteDescriptor.getMappingDescriptors().get(0);
    final ControllerMappingDescriptor mapping2 = siteDescriptor.getMappingDescriptors().get(1);
    final ControllerMappingDescriptor mapping3 = siteDescriptor.getMappingDescriptors().get(2);
    assertEquals("myapplication:/site/page/person/person.js", mapping1.getController().toString());
    assertNull(mapping1.getContentConstraint());
    assertEquals("/person/*", mapping1.getPattern().toString());
    assertEquals(10, mapping1.getOrder());
    assertEquals("myapplication:/controller1.js", mapping2.getController().toString());
    assertEquals("_path:'/*/fisk'", mapping2.getContentConstraint().toString());
    assertEquals("/.*", mapping2.getPattern().toString());
    assertEquals(50, mapping2.getOrder());
    assertEquals("myapplication:/controller2.js", mapping3.getController().toString());
    assertEquals("type:'portal:fragment'", mapping3.getContentConstraint().toString());
    assertEquals("/.*", mapping3.getPattern().toString());
    assertEquals(5, mapping3.getOrder());
}
Also used : SiteDescriptor(com.enonic.xp.site.SiteDescriptor) ApplicationKey(com.enonic.xp.app.ApplicationKey) ControllerMappingDescriptor(com.enonic.xp.site.mapping.ControllerMappingDescriptor) 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