Search in sources :

Example 46 with PortalRequest

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

the class AdminSiteHandler method doHandle.

@Override
protected WebResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) {
    final WebResponse response = super.doHandle(webRequest, webResponse, webHandlerChain);
    final PortalRequest request = PortalRequestAccessor.get(webRequest.getRawRequest());
    final RenderMode mode = request.getMode();
    if (mode == RenderMode.LIVE || request.getEndpointPath() != null) {
        return response;
    }
    final PortalResponse.Builder builder = PortalResponse.create(response);
    if (mode == RenderMode.INLINE || mode == RenderMode.EDIT) {
        builder.header("X-Frame-Options", "SAMEORIGIN");
    }
    if (mode == RenderMode.EDIT) {
        builder.removeHeader("Content-Security-Policy");
    } else if (!nullToEmpty(previewContentSecurityPolicy).isBlank() && !response.getHeaders().containsKey("Content-Security-Policy")) {
        builder.header("Content-Security-Policy", previewContentSecurityPolicy);
    }
    return builder.build();
}
Also used : RenderMode(com.enonic.xp.portal.RenderMode) PortalResponse(com.enonic.xp.portal.PortalResponse) WebResponse(com.enonic.xp.web.WebResponse) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 47 with PortalRequest

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

the class AdminToolPortalHandler method createPortalRequest.

@Override
protected PortalRequest createPortalRequest(final WebRequest webRequest, final WebResponse webResponse) {
    final PortalRequest portalRequest = new PortalRequest(webRequest);
    final DescriptorKey descriptorKey = getDescriptorKey(webRequest);
    if (descriptorKey == null) {
        portalRequest.setBaseUri(ADMIN_TOOL_START);
        portalRequest.setApplicationKey(DEFAULT_DESCRIPTOR_KEY.getApplicationKey());
    } else {
        portalRequest.setBaseUri(ADMIN_TOOL_PREFIX + descriptorKey.getApplicationKey() + "/" + descriptorKey.getName());
        portalRequest.setApplicationKey(descriptorKey.getApplicationKey());
    }
    portalRequest.setMode(RenderMode.ADMIN);
    return portalRequest;
}
Also used : DescriptorKey(com.enonic.xp.page.DescriptorKey) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 48 with PortalRequest

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

the class AdminSiteHandlerTest method testInlineAssetRequest.

@Test
public void testInlineAssetRequest() {
    this.request.setRawPath("/admin/site/inline/repo/draft/_/asset/com.enonic.app.superhero:1622131535374/css/style.css");
    PortalRequest portalRequest = this.handler.createPortalRequest(this.request, this.response);
    assertEquals("/admin/site/inline", portalRequest.getBaseUri());
    assertEquals("com.enonic.cms.repo", portalRequest.getRepositoryId().toString());
    assertEquals("draft", portalRequest.getBranch().toString());
    assertEquals("/", portalRequest.getContentPath().toString());
    assertEquals("inline", portalRequest.getMode().toString());
}
Also used : PortalRequest(com.enonic.xp.portal.PortalRequest) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 49 with PortalRequest

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

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

the class AdminToolHandlerTest method setup.

@BeforeEach
public final void setup() throws Exception {
    this.adminToolDescriptorService = Mockito.mock(AdminToolDescriptorService.class);
    ControllerScript controllerScript = Mockito.mock(ControllerScript.class);
    this.portalResponse = PortalResponse.create().build();
    Mockito.when(controllerScript.execute(Mockito.any(PortalRequest.class))).thenReturn(this.portalResponse);
    final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
    Mockito.when(controllerScriptFactory.fromDir(Mockito.any(ResourceKey.class))).thenReturn(controllerScript);
    this.handler = new AdminToolHandler();
    this.handler.setAdminToolDescriptorService(this.adminToolDescriptorService);
    this.handler.setControllerScriptFactory(controllerScriptFactory);
    this.rawRequest = Mockito.mock(HttpServletRequest.class);
    Mockito.when(this.rawRequest.isUserInRole(Mockito.anyString())).thenReturn(true);
    this.portalRequest = new PortalRequest();
    this.portalRequest.setRawRequest(this.rawRequest);
    this.portalRequest.setMode(RenderMode.ADMIN);
    final DescriptorKey defaultDescriptorKey = AdminToolPortalHandler.DEFAULT_DESCRIPTOR_KEY;
    this.portalRequest.setBaseUri(AdminToolPortalHandler.ADMIN_TOOL_PREFIX + defaultDescriptorKey.getApplicationKey() + "/" + defaultDescriptorKey.getName());
    this.portalRequest.setApplicationKey(defaultDescriptorKey.getApplicationKey());
    this.webResponse = WebResponse.create().build();
    this.chain = Mockito.mock(WebHandlerChain.class);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) AdminToolDescriptorService(com.enonic.xp.admin.tool.AdminToolDescriptorService) DescriptorKey(com.enonic.xp.page.DescriptorKey) WebHandlerChain(com.enonic.xp.web.handler.WebHandlerChain) ControllerScriptFactory(com.enonic.xp.portal.controller.ControllerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) ResourceKey(com.enonic.xp.resource.ResourceKey) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

PortalRequest (com.enonic.xp.portal.PortalRequest)103 Test (org.junit.jupiter.api.Test)47 BeforeEach (org.junit.jupiter.api.BeforeEach)35 PortalResponse (com.enonic.xp.portal.PortalResponse)25 Site (com.enonic.xp.site.Site)19 Content (com.enonic.xp.content.Content)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ApplicationKey (com.enonic.xp.app.ApplicationKey)10 ContentPath (com.enonic.xp.content.ContentPath)10 ContentService (com.enonic.xp.content.ContentService)10 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)10 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ResourceKey (com.enonic.xp.resource.ResourceKey)9 ResourceService (com.enonic.xp.resource.ResourceService)9 ContentId (com.enonic.xp.content.ContentId)7 Matcher (java.util.regex.Matcher)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)7 Assertions.fail (org.junit.jupiter.api.Assertions.fail)7 Mockito.when (org.mockito.Mockito.when)7