use of com.enonic.xp.region.FragmentComponent 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.region.FragmentComponent in project xp by enonic.
the class FragmentComponentDataSerializer method fromData.
@Override
public FragmentComponent fromData(final PropertySet data) {
final FragmentComponent.Builder component = FragmentComponent.create();
final PropertySet specialBlockSet = data.getSet(FragmentComponentType.INSTANCE.toString());
if (specialBlockSet != null && specialBlockSet.isNotNull(ID)) {
final ContentId contentId = ContentId.from(specialBlockSet.getString(ID));
component.fragment(contentId);
}
return component.build();
}
use of com.enonic.xp.region.FragmentComponent in project xp by enonic.
the class ComponentHandlerWorker method inlineFragments.
private Page inlineFragments(Page page, final ComponentPath componentPath) {
// traverse page based on componentPath, inline fragments components if found
final List<ComponentPath.RegionAndComponent> partialComponentPathParts = new ArrayList<>();
for (ComponentPath.RegionAndComponent pathPart : componentPath) {
partialComponentPathParts.add(pathPart);
final ComponentPath path = new ComponentPath(ImmutableList.copyOf(partialComponentPathParts));
final Component component = page.getRegions().getComponent(path);
if (component == null) {
break;
}
if (component instanceof FragmentComponent) {
final FragmentComponent fragment = (FragmentComponent) component;
final Component fragmentComponent = getFragmentComponent(fragment);
if (fragmentComponent == null) {
break;
}
page = new FragmentPageResolver().inlineFragmentInPage(page, fragmentComponent, path);
}
}
return page;
}
use of com.enonic.xp.region.FragmentComponent in project xp by enonic.
the class FragmentRenderer method render.
public PortalResponse render(final FragmentComponent component, final PortalRequest portalRequest) {
final RenderMode renderMode = portalRequest.getMode();
final String type = component.getType().toString();
if (component.getFragment() == null) {
return renderEmptyFragment(renderMode, component);
}
final Component fragmentComponent = getFragmentComponent(component);
if (fragmentComponent == null) {
LOG.warn("Fragment content could not be found. ContentId: " + component.getFragment().toString());
if (renderMode == RenderMode.EDIT) {
final String errorMessage = "Fragment content could not be found";
return renderErrorComponentPlaceHolder(component, errorMessage);
} else {
return renderEmptyFragment(renderMode, component);
}
}
// replace resolved fragment in current PortalRequest Page
final Page sourcePage = portalRequest.getContent().getPage();
final Page page = fragmentPageResolver.inlineFragmentInPage(sourcePage, fragmentComponent, component.getPath());
final Content content = Content.create(portalRequest.getContent()).page(page).build();
portalRequest.setContent(content);
final PortalResponse fragmentResponse = rendererDelegate.render(fragmentComponent, portalRequest);
if (renderMode == RenderMode.EDIT && fragmentResponse != null) {
if (!(fragmentResponse.getBody() instanceof String) || !fragmentResponse.getContentType().is(MediaType.parse("text/html"))) {
return fragmentResponse;
}
final String body = (String) fragmentResponse.getBody();
final String noMethodErrorMessage = "No method provided to handle request";
if (body.contains(noMethodErrorMessage)) {
return renderErrorComponentPlaceHolder(component, noMethodErrorMessage);
}
return wrapFragmentForEditMode(fragmentResponse, type);
}
return fragmentResponse;
}
Aggregations