Search in sources :

Example 61 with PortalResponse

use of com.enonic.xp.portal.PortalResponse 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 62 with PortalResponse

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

Example 63 with PortalResponse

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

the class PostProcessingRenderer method executeResponseProcessors.

private PortalResponse executeResponseProcessors(final PortalRequest portalRequest, final PortalResponse portalResponse) {
    final ResponseProcessorDescriptors filters = this.processorChainResolver.resolve(portalRequest);
    if (!portalResponse.applyFilters() || filters.isEmpty()) {
        return portalResponse;
    }
    PortalResponse filterResponse = portalResponse;
    for (ResponseProcessorDescriptor filter : filters) {
        final PortalResponse filterPortalResponse = filterResponse;
        final Trace trace = Tracer.newTrace("renderFilter");
        if (trace == null) {
            filterResponse = processorExecutor.execute(filter, portalRequest, filterPortalResponse);
        } else {
            trace.put("app", filter.getApplication().toString());
            trace.put("name", filter.getName());
            trace.put("type", "filter");
            filterResponse = Tracer.trace(trace, () -> processorExecutor.execute(filter, portalRequest, filterPortalResponse));
        }
        if (!filterResponse.applyFilters()) {
            break;
        }
    }
    return filterResponse;
}
Also used : Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ResponseProcessorDescriptors(com.enonic.xp.site.processor.ResponseProcessorDescriptors)

Example 64 with PortalResponse

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

the class ExceptionRendererImplTest method render_html_withCause.

@Test
void render_html_withCause() {
    this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
    final RuntimeException cause = new RuntimeException("Custom message");
    final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
    assertEquals(HttpStatus.BAD_REQUEST, res.getStatus());
    assertEquals(MediaType.HTML_UTF_8.withoutParameters(), res.getContentType());
    final String body = res.getBody().toString();
    assertTrue(body.contains("400 Bad Request"));
    assertTrue(body.contains("Custom message"));
    // Should show exception
    assertTrue(body.contains(RuntimeException.class.getName()));
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Test(org.junit.jupiter.api.Test)

Example 65 with PortalResponse

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

the class ExceptionRendererImplTest method testRender_RenderMode_Admin.

@Test
void testRender_RenderMode_Admin() {
    this.request.setMode(RenderMode.ADMIN);
    this.request.setApplicationKey(ApplicationKey.from("myapplication-key"));
    final PortalResponse result = renderer.render(this.request, new WebException(HttpStatus.NOT_FOUND, "Resource not found"));
    assertNotNull(result);
    assertEquals("{\"status\":404,\"message\":\"Resource not found\"}", result.getBody().toString());
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Test(org.junit.jupiter.api.Test)

Aggregations

PortalResponse (com.enonic.xp.portal.PortalResponse)104 Test (org.junit.jupiter.api.Test)78 PortalRequest (com.enonic.xp.portal.PortalRequest)21 WebException (com.enonic.xp.web.WebException)14 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)14 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)13 PostProcessInstruction (com.enonic.xp.portal.postprocess.PostProcessInstruction)10 WebResponse (com.enonic.xp.web.WebResponse)10 ContentService (com.enonic.xp.content.ContentService)9 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ResourceKey (com.enonic.xp.resource.ResourceKey)9 RenderMode (com.enonic.xp.portal.RenderMode)8 HtmlTag (com.enonic.xp.portal.postprocess.HtmlTag)8 PostProcessInjection (com.enonic.xp.portal.postprocess.PostProcessInjection)8 ByteSource (com.google.common.io.ByteSource)8 InputStream (java.io.InputStream)8 StandardCharsets (java.nio.charset.StandardCharsets)8 Arrays (java.util.Arrays)8 Collections (java.util.Collections)8 List (java.util.List)8