use of com.enonic.xp.portal.PortalResponse 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;
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class ExceptionRendererImplTest method render_internal_server_error.
@Test
void render_internal_server_error() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.INTERNAL_SERVER_ERROR, "Custom message"));
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatus());
assertEquals(MediaType.HTML_UTF_8.withoutParameters(), res.getContentType());
final String body = res.getBody().toString();
assertTrue(body.contains("500 Internal Server Error"));
assertTrue(body.contains("Custom message"));
// Should not show exception
assertTrue(body.contains(ExceptionRendererImplTest.class.getName()));
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class ExceptionRendererImplTest method render_json.
@Test
void render_json() {
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.NOT_FOUND, "Custom message"));
assertEquals(HttpStatus.NOT_FOUND, res.getStatus());
assertEquals(MediaType.JSON_UTF_8.withoutParameters(), res.getContentType());
final String body = res.getBody().toString();
assertEquals("{\"status\":404,\"message\":\"Custom message\"}", body);
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class ExceptionRendererImplTest method render_default_error_page_when_error_in_custom_handler.
@Test
void render_default_error_page_when_error_in_custom_handler() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
final Site site = newSite();
this.request.setSite(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> {
throw new RuntimeException("Something went wrong in the handler script");
};
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.BAD_REQUEST, cause));
final String body = res.getBody().toString();
assertTrue(body.contains("400 Bad Request"));
assertTrue(body.contains("Custom message"));
}
use of com.enonic.xp.portal.PortalResponse in project xp by enonic.
the class ExceptionRendererImplTest method render_custom_error_for_404_in_site_path.
@Test
void render_custom_error_for_404_in_site_path() {
this.request.getHeaders().put(HttpHeaders.ACCEPT, "text/html,text/*");
this.request.setContentPath(ContentPath.from("/mysite/some/long/path"));
final Site site = newSite();
when(contentService.findNearestSiteByPath(eq(ContentPath.from("/mysite/some/long/path")))).thenReturn(site);
final ResourceKey errorResource = ResourceKey.from(ApplicationKey.from("myapplication"), "site/error/error.js");
final ErrorHandlerScript errorHandlerScript = (portalError, handleMethod) -> PortalResponse.create().body("Custom message page").status(HttpStatus.NOT_FOUND).postProcess(false).build();
when(this.errorHandlerScriptFactory.errorScript(errorResource)).thenReturn(errorHandlerScript);
final Resource resource = mock(Resource.class);
when(resource.exists()).thenReturn(true);
when(this.resourceService.getResource(errorResource)).thenReturn(resource);
final RuntimeException cause = new RuntimeException("Custom message");
final PortalResponse res = this.renderer.render(this.request, new WebException(HttpStatus.NOT_FOUND, cause));
assertEquals(HttpStatus.NOT_FOUND, res.getStatus());
assertEquals("Custom message page", res.getBody().toString());
assertFalse(postProcessor.isExecuted());
}
Aggregations