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();
}
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();
}
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());
}
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);
}
}
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());
}
Aggregations