use of com.enonic.xp.page.Page in project xp by enonic.
the class PageResolver method resolve.
public PageResolverResult resolve(final RenderMode mode, final Content content, final Site site) {
final Page page = content.getPage();
final PageTemplate pageTemplate;
final DescriptorKey controller;
final Page effectivePage;
if (content instanceof PageTemplate) {
pageTemplate = (PageTemplate) content;
controller = getControllerFromTemplate(pageTemplate, mode);
effectivePage = pageTemplate.getPage();
} else if (page != null) {
if (page.getFragment() != null) {
controller = null;
effectivePage = page;
} else if (page.getDescriptor() != null) {
controller = page.getDescriptor();
effectivePage = page;
} else if (page.getTemplate() != null) {
pageTemplate = getPageTemplateOrFindDefault(page.getTemplate(), content.getType(), site.getPath());
if (pageTemplate != null) {
controller = getControllerFromTemplate(pageTemplate, mode);
effectivePage = mergePageFromPageTemplate(pageTemplate, page);
} else {
controller = errorOrNull(null, mode, String.format("Template [%s] is missing and no default template found for content", page.getTemplate()));
effectivePage = page;
}
} else {
controller = errorOrNull(null, mode, "Content page has neither template nor descriptor");
effectivePage = page;
}
} else {
pageTemplate = this.pageTemplateService.getDefault(GetDefaultPageTemplateParams.create().sitePath(site.getPath()).contentType(content.getType()).build());
if (pageTemplate != null) {
controller = getControllerFromTemplate(pageTemplate, mode);
effectivePage = mergePageFromPageTemplate(pageTemplate, null);
} else {
controller = errorOrNull(null, mode, "No default template found for content");
effectivePage = null;
}
}
return new PageResolverResult(effectivePage, controller);
}
use of com.enonic.xp.page.Page in project xp by enonic.
the class ComponentHandlerWorker method execute.
@Override
public PortalResponse execute() throws Exception {
final ContentResolverResult resolvedContent = contentResolver.resolve(this.request);
final Content content = resolvedContent.getContentOrElseThrow();
final Site site = resolvedContent.getNearestSiteOrElseThrow();
Page page = content.getPage();
final PageResolverResult resolvedPage = pageResolver.resolve(request.getMode(), content, site);
Component component = null;
if (content.getType().isFragment()) {
// fragment content, try resolving component path in Layout fragment
final Component fragmentComponent = page.getFragment();
if (this.componentPath.isEmpty()) {
component = fragmentComponent;
} else if (fragmentComponent instanceof LayoutComponent) {
component = ((LayoutComponent) fragmentComponent).getComponent(this.componentPath);
}
}
final Page effectivePage;
if (component == null) {
effectivePage = inlineFragments(resolvedPage.getEffectivePage(), this.componentPath);
component = effectivePage.getRegions().getComponent(this.componentPath);
} else {
effectivePage = resolvedPage.getEffectivePage();
}
if (component == null) {
throw WebException.notFound(String.format("Page component for [%s] not found", this.componentPath));
}
final Content effectiveContent = Content.create(content).page(effectivePage).build();
final DescriptorKey controller = resolvedPage.getController();
this.request.setSite(site);
this.request.setContent(effectiveContent);
this.request.setComponent(component);
this.request.setApplicationKey(controller != null ? controller.getApplicationKey() : null);
final Trace trace = Tracer.current();
if (trace != null) {
trace.put("componentPath", component.getPath());
trace.put("type", component.getType().toString());
}
final PortalResponse response = rendererDelegate.render(component, this.request);
return this.postProcessor.processResponseInstructions(this.request, response);
}
use of com.enonic.xp.page.Page in project xp by enonic.
the class ComponentInstruction method resolveComponent.
private Component resolveComponent(final PortalRequest portalRequest, final ComponentPath path) {
final Content content = portalRequest.getContent();
if (content == null) {
return null;
}
final Page page = content.getPage();
if (content.getType().isFragment()) {
return resolveComponentInFragment(page, path);
}
final PageRegions pageRegions = page.getRegions();
Component component = pageRegions.getComponent(path);
if (component == null) {
throw new RenderException("Component not found: [{0}]", path);
}
return component;
}
use of com.enonic.xp.page.Page in project xp by enonic.
the class RenderBaseHandlerTest method setupCustomizedTemplateContentAndSite.
protected final void setupCustomizedTemplateContentAndSite() {
Content content = createPage("id", "site/somepath/content", "myapplication:ctype", true);
final PageDescriptor controllerDescriptor = createDescriptor();
Page page = Page.create(content.getPage()).template(null).descriptor(controllerDescriptor.getKey()).build();
content = Content.create(content).page(page).build();
when(this.contentService.getByPath(ContentPath.from("site/somepath/content").asAbsolute())).thenReturn(content);
when(this.contentService.getNearestSite(isA(ContentId.class))).thenReturn(createSite("id", "site"));
when(this.contentService.getById(content.getId())).thenReturn(content);
}
use of com.enonic.xp.page.Page in project xp by enonic.
the class PageResolverTest method content_without_Page_and_no_default_template.
@Test
public void content_without_Page_and_no_default_template() {
Site site = Site.create().id(ContentId.from("site-id")).path(ContentPath.from("/site")).displayName("My Site").type(ContentTypeName.from("portal:site")).build();
Content content = Content.create().parentPath(site.getPath()).name("my-content").build();
when(pageTemplateService.getDefault(notNull())).thenReturn(null);
final WebException webExceptionInLive = assertThrows(WebException.class, () -> pageResolver.resolve(RenderMode.LIVE, content, site));
assertEquals(HttpStatus.NOT_FOUND, webExceptionInLive.getStatus());
assertEquals(webExceptionInLive.getMessage(), "No default template found for content");
final WebException webExceptionInInline = assertThrows(WebException.class, () -> pageResolver.resolve(RenderMode.INLINE, content, site));
assertEquals(HttpStatus.IM_A_TEAPOT, webExceptionInInline.getStatus());
final WebException webExceptionInPreview = assertThrows(WebException.class, () -> pageResolver.resolve(RenderMode.PREVIEW, content, site));
assertEquals(HttpStatus.NOT_FOUND, webExceptionInPreview.getStatus());
final PageResolverResult result = pageResolver.resolve(RenderMode.EDIT, content, site);
final Page effectivePage = result.getEffectivePage();
assertNull(effectivePage);
assertNull(result.getController());
}
Aggregations