Search in sources :

Example 91 with PortalResponse

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;
}
Also used : RenderMode(com.enonic.xp.portal.RenderMode) PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) Page(com.enonic.xp.page.Page) FragmentComponent(com.enonic.xp.region.FragmentComponent) Component(com.enonic.xp.region.Component)

Example 92 with PortalResponse

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()));
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Test(org.junit.jupiter.api.Test)

Example 93 with PortalResponse

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);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Test(org.junit.jupiter.api.Test)

Example 94 with PortalResponse

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"));
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 95 with PortalResponse

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());
}
Also used : Site(com.enonic.xp.site.Site) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ContentService(com.enonic.xp.content.ContentService) WebException(com.enonic.xp.web.WebException) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpHeaders(com.google.common.net.HttpHeaders) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PortalRequest(com.enonic.xp.portal.PortalRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) MediaType(com.google.common.net.MediaType) ResourceService(com.enonic.xp.resource.ResourceService) ContentPath(com.enonic.xp.content.ContentPath) SiteConfig(com.enonic.xp.site.SiteConfig) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) Mockito.when(org.mockito.Mockito.when) RenderMode(com.enonic.xp.portal.RenderMode) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Site(com.enonic.xp.site.Site) RunMode(com.enonic.xp.server.RunMode) Resource(com.enonic.xp.resource.Resource) SiteConfigs(com.enonic.xp.site.SiteConfigs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpStatus(com.enonic.xp.web.HttpStatus) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) Mockito.mock(org.mockito.Mockito.mock) PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Resource(com.enonic.xp.resource.Resource) ErrorHandlerScript(com.enonic.xp.portal.impl.error.ErrorHandlerScript) ResourceKey(com.enonic.xp.resource.ResourceKey) 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