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();
}
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;
}
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;
}
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()));
}
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());
}
Aggregations