Search in sources :

Example 16 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 17 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)

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