Search in sources :

Example 1 with RenderMode

use of com.enonic.xp.portal.RenderMode in project xp by enonic.

the class AdminSiteHandler method createPortalRequest.

@Override
protected PortalRequest createPortalRequest(final WebRequest webRequest, final WebResponse webResponse) {
    final Matcher matcher = BASE_URI_PATTERN.matcher(webRequest.getRawPath());
    if (!matcher.find()) {
        throw WebException.notFound("Mode needs to be specified");
    }
    final String baseUri = matcher.group(0);
    final RenderMode mode = RenderMode.from(matcher.group(1));
    final String baseSubPath = webRequest.getRawPath().substring(baseUri.length() + 1);
    final PortalRequest portalRequest = doCreatePortalRequest(webRequest, baseUri, baseSubPath);
    portalRequest.setMode(mode);
    return portalRequest;
}
Also used : RenderMode(com.enonic.xp.portal.RenderMode) Matcher(java.util.regex.Matcher) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 2 with RenderMode

use of com.enonic.xp.portal.RenderMode in project xp by enonic.

the class AdminSiteHandler method doHandle.

@Override
protected WebResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) {
    final WebResponse response = super.doHandle(webRequest, webResponse, webHandlerChain);
    final PortalRequest request = PortalRequestAccessor.get(webRequest.getRawRequest());
    final RenderMode mode = request.getMode();
    if (mode == RenderMode.LIVE || request.getEndpointPath() != null) {
        return response;
    }
    final PortalResponse.Builder builder = PortalResponse.create(response);
    if (mode == RenderMode.INLINE || mode == RenderMode.EDIT) {
        builder.header("X-Frame-Options", "SAMEORIGIN");
    }
    if (mode == RenderMode.EDIT) {
        builder.removeHeader("Content-Security-Policy");
    } else if (!nullToEmpty(previewContentSecurityPolicy).isBlank() && !response.getHeaders().containsKey("Content-Security-Policy")) {
        builder.header("Content-Security-Policy", previewContentSecurityPolicy);
    }
    return builder.build();
}
Also used : RenderMode(com.enonic.xp.portal.RenderMode) PortalResponse(com.enonic.xp.portal.PortalResponse) WebResponse(com.enonic.xp.web.WebResponse) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 3 with RenderMode

use of com.enonic.xp.portal.RenderMode 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)

Example 4 with RenderMode

use of com.enonic.xp.portal.RenderMode in project xp by enonic.

the class TextRenderer method render.

@Override
public PortalResponse render(final TextComponent textComponent, final PortalRequest portalRequest) {
    final RenderMode renderMode = portalRequest.getMode();
    final PortalResponse.Builder portalResponseBuilder = PortalResponse.create();
    portalResponseBuilder.contentType(MediaType.create("text", "html")).postProcess(false);
    if (textComponent.getText().isEmpty()) {
        renderEmptyTextComponent(textComponent, renderMode, portalResponseBuilder);
    } else {
        if (renderMode == RenderMode.EDIT) {
            portalResponseBuilder.body(MessageFormat.format(COMPONENT_EDIT_MODE_HTML, textComponent.getType().toString(), textComponent.getText()));
        } else {
            ProcessHtmlParams params = new ProcessHtmlParams().portalRequest(portalRequest).value(textComponent.getText());
            final String processedHtml = removeEmptyFigCaptionTags(service.processHtml(params));
            portalResponseBuilder.body(MessageFormat.format(COMPONENT_PREVIEW_MODE_HTML, textComponent.getType().toString(), processedHtml));
        }
    }
    return portalResponseBuilder.build();
}
Also used : RenderMode(com.enonic.xp.portal.RenderMode) PortalResponse(com.enonic.xp.portal.PortalResponse) ProcessHtmlParams(com.enonic.xp.portal.url.ProcessHtmlParams)

Example 5 with RenderMode

use of com.enonic.xp.portal.RenderMode in project xp by enonic.

the class PageRenderer method doRender.

@Override
public PortalResponse doRender(final Content content, final PortalRequest portalRequest) {
    final PageDescriptor pageDescriptor = portalRequest.getPageDescriptor();
    final RenderMode mode = portalRequest.getMode();
    PortalResponse portalResponse;
    if (pageDescriptor != null) {
        final ControllerScript controllerScript = this.controllerScriptFactory.fromDir(pageDescriptor.getComponentPath());
        portalResponse = controllerScript.execute(portalRequest);
    } else if (portalRequest.getControllerScript() != null) {
        portalResponse = portalRequest.getControllerScript().execute(portalRequest);
    } else if ((mode == EDIT || mode == PREVIEW || mode == INLINE) && portalRequest.getContent().getType().isFragment()) {
        portalResponse = renderDefaultFragmentPage(portalRequest, content);
    } else {
        portalResponse = renderForNoPageDescriptor(portalRequest, content);
    }
    return portalResponse;
}
Also used : PageDescriptor(com.enonic.xp.page.PageDescriptor) RenderMode(com.enonic.xp.portal.RenderMode) PortalResponse(com.enonic.xp.portal.PortalResponse) ControllerScript(com.enonic.xp.portal.controller.ControllerScript)

Aggregations

RenderMode (com.enonic.xp.portal.RenderMode)6 PortalResponse (com.enonic.xp.portal.PortalResponse)5 PortalRequest (com.enonic.xp.portal.PortalRequest)2 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)2 Component (com.enonic.xp.region.Component)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 Content (com.enonic.xp.content.Content)1 Page (com.enonic.xp.page.Page)1 PageDescriptor (com.enonic.xp.page.PageDescriptor)1 ProcessHtmlParams (com.enonic.xp.portal.url.ProcessHtmlParams)1 ComponentDescriptor (com.enonic.xp.region.ComponentDescriptor)1 DescriptorBasedComponent (com.enonic.xp.region.DescriptorBasedComponent)1 FragmentComponent (com.enonic.xp.region.FragmentComponent)1 WebResponse (com.enonic.xp.web.WebResponse)1 MediaType (com.google.common.net.MediaType)1 Matcher (java.util.regex.Matcher)1