Search in sources :

Example 56 with PortalResponse

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

the class RendererDelegateImplTest method given_Renderable_having_not_matching_Renderer_when_getRenderer_then_Renderer_for_that_type_is_returned.

@Test
public void given_Renderable_having_not_matching_Renderer_when_getRenderer_then_Renderer_for_that_type_is_returned() {
    RendererDelegateImpl factory = new RendererDelegateImpl(mock(ContentService.class));
    final PortalResponse response = PortalResponse.create().build();
    factory.addRenderer(createRenderer(Content.class, response));
    // exercise
    final PortalResponse renderResponse = factory.render(createContent(), null);
    // verify
    assertSame(response, renderResponse);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) ContentService(com.enonic.xp.content.ContentService) Test(org.junit.jupiter.api.Test)

Example 57 with PortalResponse

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

the class WidgetHandlerTest method testOptions.

@Test
public void testOptions() throws Exception {
    mockDescriptor(true);
    final PortalResponse portalResponse = PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
    Mockito.when(this.controllerScript.execute(Mockito.any())).thenReturn(portalResponse);
    this.request.setMethod(HttpMethod.OPTIONS);
    this.request.setMode(RenderMode.ADMIN);
    final WebResponse response = this.handler.handle(this.request, WebResponse.create().build(), null);
    assertNotNull(response);
    assertEquals(HttpStatus.OK, response.getStatus());
    assertEquals("GET,POST,HEAD,OPTIONS,PUT,DELETE,TRACE", response.getHeaders().get("Allow"));
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebResponse(com.enonic.xp.web.WebResponse) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 58 with PortalResponse

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

the class WidgetHandlerTest method setup.

@BeforeEach
public final void setup() {
    this.request = new PortalRequest();
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    this.controllerScript = Mockito.mock(ControllerScript.class);
    Mockito.when(controllerScriptFactory.fromDir(Mockito.any())).thenReturn(this.controllerScript);
    final PortalResponse portalResponse = PortalResponse.create().build();
    Mockito.when(this.controllerScript.execute(Mockito.any())).thenReturn(portalResponse);
    this.contentService = Mockito.mock(ContentService.class);
    this.widgetDescriptorService = Mockito.mock(WidgetDescriptorService.class);
    this.handler = new WidgetHandler();
    this.handler.setControllerScriptFactory(controllerScriptFactory);
    this.handler.setWidgetDescriptorService(this.widgetDescriptorService);
    HttpServletRequest rawRequest = Mockito.mock(HttpServletRequest.class);
    Mockito.when(rawRequest.isUserInRole(Mockito.anyString())).thenReturn(Boolean.TRUE);
    this.request.setRawRequest(rawRequest);
    this.request.setMethod(HttpMethod.GET);
    this.request.setContentPath(ContentPath.from("/site/somepath/content"));
    this.request.setEndpointPath("/_/widgets/demo/mywidget");
    this.request.setRawPath("/admin/tool/_/widgets/demo/mywidget");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) ContentService(com.enonic.xp.content.ContentService) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) WidgetDescriptorService(com.enonic.xp.admin.widget.WidgetDescriptorService) PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 59 with PortalResponse

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

the class AdminToolHandler method doHandle.

@Override
protected WebResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    WebHandlerHelper.checkAdminAccess(webRequest);
    PortalRequest portalRequest = (PortalRequest) webRequest;
    portalRequest.setContextPath(portalRequest.getBaseUri());
    final AdminToolHandlerWorker worker = new AdminToolHandlerWorker(portalRequest);
    worker.controllerScriptFactory = this.controllerScriptFactory;
    worker.adminToolDescriptorService = adminToolDescriptorService;
    final DescriptorKey descriptorKey = AdminToolPortalHandler.getDescriptorKey(webRequest);
    worker.descriptorKey = descriptorKey == null ? AdminToolPortalHandler.DEFAULT_DESCRIPTOR_KEY : descriptorKey;
    final Trace trace = Tracer.newTrace("portalRequest");
    if (trace == null) {
        return worker.execute();
    }
    trace.put("path", webRequest.getPath());
    trace.put("method", webRequest.getMethod().toString());
    trace.put("host", webRequest.getHost());
    trace.put("httpRequest", webRequest);
    trace.put("httpResponse", webResponse);
    trace.put("context", ContextAccessor.current());
    return Tracer.traceEx(trace, () -> {
        final PortalResponse response = worker.execute();
        addTraceInfo(trace, response);
        return response;
    });
}
Also used : Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) DescriptorKey(com.enonic.xp.page.DescriptorKey) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 60 with PortalResponse

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

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