Search in sources :

Example 51 with PortalRequest

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

the class WidgetHandler method doHandle.

@Override
protected WebResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    WebHandlerHelper.checkAdminAccess(webRequest);
    final String restPath = findRestPath(webRequest);
    final Matcher matcher = PATTERN.matcher(restPath);
    if (!matcher.find()) {
        throw WebException.notFound("Not a valid service url pattern");
    }
    final PortalRequest portalRequest = webRequest instanceof PortalRequest ? (PortalRequest) webRequest : new PortalRequest(webRequest);
    portalRequest.setContextPath(findPreRestPath(portalRequest) + "/" + matcher.group(0));
    final WidgetHandlerWorker worker = new WidgetHandlerWorker(portalRequest);
    worker.controllerScriptFactory = this.controllerScriptFactory;
    worker.widgetDescriptorService = this.widgetDescriptorService;
    worker.descriptorKey = DescriptorKey.from(ApplicationKey.from(matcher.group(1)), matcher.group(2));
    return worker.execute();
}
Also used : Matcher(java.util.regex.Matcher) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 52 with PortalRequest

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

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

the class AdminSiteHandlerTest method testCreatePortalRequest.

@Test
public void testCreatePortalRequest() {
    this.request.setRawPath("/admin/site/edit/repo/master/content/1");
    PortalRequest portalRequest = this.handler.createPortalRequest(this.request, this.response);
    assertEquals("/admin/site/edit", portalRequest.getBaseUri());
    assertEquals("com.enonic.cms.repo", portalRequest.getRepositoryId().toString());
    assertEquals("master", portalRequest.getBranch().toString());
    assertEquals("/content/1", portalRequest.getContentPath().toString());
    assertEquals("edit", 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 54 with PortalRequest

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

the class LocaleScriptBean method resolveLocaleFromSite.

private Locale resolveLocaleFromSite() {
    final PortalRequest request = portalRequest.get();
    if (request == null) {
        return null;
    }
    final Site site = request.getSite();
    if (site != null) {
        return site.getLanguage();
    }
    return null;
}
Also used : Site(com.enonic.xp.site.Site) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 55 with PortalRequest

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

the class BasePortalHandler method doHandle.

@Override
protected WebResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) {
    final PortalRequest portalRequest;
    if (webRequest instanceof PortalRequest) {
        portalRequest = (PortalRequest) webRequest;
    } else {
        portalRequest = createPortalRequest(webRequest, webResponse);
    }
    try {
        PortalRequestAccessor.set(portalRequest.getRawRequest(), portalRequest);
        ContextAccessor.current().getLocalScope().setAttribute(portalRequest.getRepositoryId());
        ContextAccessor.current().getLocalScope().setAttribute(portalRequest.getBranch());
        final WebResponse returnedWebResponse = webHandlerChain.handle(portalRequest, webResponse);
        exceptionMapper.throwIfNeeded(returnedWebResponse);
        return returnedWebResponse;
    } catch (Exception e) {
        return handleError(portalRequest, e);
    }
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) WebException(com.enonic.xp.web.WebException) PortalRequest(com.enonic.xp.portal.PortalRequest)

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