Search in sources :

Example 1 with ComponentDescriptor

use of com.enonic.xp.region.ComponentDescriptor in project xp by enonic.

the class DescriptorBasedComponentRenderer method doRender.

private PortalResponse doRender(final R component, final PortalRequest portalRequest) {
    final ComponentDescriptor descriptor = resolveDescriptor(component);
    if (descriptor == null) {
        return renderEmptyComponent(component, portalRequest);
    }
    // create controller
    final ControllerScript controllerScript = this.controllerScriptFactory.fromDir(descriptor.getComponentPath());
    // render
    final Component previousComponent = portalRequest.getComponent();
    final ApplicationKey previousApplication = portalRequest.getApplicationKey();
    try {
        portalRequest.setComponent(component);
        portalRequest.setApplicationKey(descriptor.getKey().getApplicationKey());
        final PortalResponse portalResponse = controllerScript.execute(portalRequest);
        final RenderMode renderMode = portalRequest.getMode();
        final MediaType contentType = portalResponse.getContentType();
        if (renderMode == RenderMode.EDIT && contentType != null && contentType.withoutParameters().type().equals("text")) {
            final Object bodyObj = portalResponse.getBody();
            if (bodyObj == null || (bodyObj instanceof String && nullToEmpty((String) bodyObj).isBlank())) {
                if (portalResponse.getStatus().equals(HttpStatus.METHOD_NOT_ALLOWED)) {
                    final String errorMessage = "No method provided to handle request";
                    return renderErrorComponentPlaceHolder(component, errorMessage);
                }
                return renderEmptyComponent(component, portalRequest);
            }
        }
        final PortalResponse injectedResponse = LIVE_EDIT_ATTRIBUTE_INJECTION.injectLiveEditAttribute(portalResponse, component.getType());
        return injectedResponse;
    } finally {
        portalRequest.setComponent(previousComponent);
        portalRequest.setApplicationKey(previousApplication);
    }
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) PortalResponse(com.enonic.xp.portal.PortalResponse) RenderMode(com.enonic.xp.portal.RenderMode) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) ComponentDescriptor(com.enonic.xp.region.ComponentDescriptor) MediaType(com.google.common.net.MediaType) DescriptorBasedComponent(com.enonic.xp.region.DescriptorBasedComponent) Component(com.enonic.xp.region.Component)

Aggregations

ApplicationKey (com.enonic.xp.app.ApplicationKey)1 PortalResponse (com.enonic.xp.portal.PortalResponse)1 RenderMode (com.enonic.xp.portal.RenderMode)1 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)1 Component (com.enonic.xp.region.Component)1 ComponentDescriptor (com.enonic.xp.region.ComponentDescriptor)1 DescriptorBasedComponent (com.enonic.xp.region.DescriptorBasedComponent)1 MediaType (com.google.common.net.MediaType)1