Search in sources :

Example 1 with FragmentComponent

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);
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) Site(com.enonic.xp.site.Site) Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) DescriptorKey(com.enonic.xp.page.DescriptorKey) Page(com.enonic.xp.page.Page) FragmentComponent(com.enonic.xp.region.FragmentComponent) Component(com.enonic.xp.region.Component) LayoutComponent(com.enonic.xp.region.LayoutComponent) LayoutComponent(com.enonic.xp.region.LayoutComponent)

Example 2 with FragmentComponent

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();
}
Also used : FragmentComponent(com.enonic.xp.region.FragmentComponent) PropertySet(com.enonic.xp.data.PropertySet) ContentId(com.enonic.xp.content.ContentId)

Example 3 with FragmentComponent

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;
}
Also used : ComponentPath(com.enonic.xp.region.ComponentPath) FragmentPageResolver(com.enonic.xp.portal.impl.rendering.FragmentPageResolver) ArrayList(java.util.ArrayList) FragmentComponent(com.enonic.xp.region.FragmentComponent) FragmentComponent(com.enonic.xp.region.FragmentComponent) Component(com.enonic.xp.region.Component) LayoutComponent(com.enonic.xp.region.LayoutComponent)

Example 4 with FragmentComponent

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;
}
Also used : RenderMode(com.enonic.xp.portal.RenderMode) PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) Page(com.enonic.xp.page.Page) FragmentComponent(com.enonic.xp.region.FragmentComponent) Component(com.enonic.xp.region.Component)

Aggregations

FragmentComponent (com.enonic.xp.region.FragmentComponent)4 Component (com.enonic.xp.region.Component)3 Content (com.enonic.xp.content.Content)2 Page (com.enonic.xp.page.Page)2 PortalResponse (com.enonic.xp.portal.PortalResponse)2 LayoutComponent (com.enonic.xp.region.LayoutComponent)2 ContentId (com.enonic.xp.content.ContentId)1 PropertySet (com.enonic.xp.data.PropertySet)1 DescriptorKey (com.enonic.xp.page.DescriptorKey)1 RenderMode (com.enonic.xp.portal.RenderMode)1 ContentResolverResult (com.enonic.xp.portal.impl.ContentResolverResult)1 FragmentPageResolver (com.enonic.xp.portal.impl.rendering.FragmentPageResolver)1 ComponentPath (com.enonic.xp.region.ComponentPath)1 Site (com.enonic.xp.site.Site)1 Trace (com.enonic.xp.trace.Trace)1 ArrayList (java.util.ArrayList)1