Search in sources :

Example 1 with PortalResponse

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

the class ExceptionRendererImpl method doRenderCustomError.

private PortalResponse doRenderCustomError(final PortalRequest req, final WebException cause, final String handlerMethod) {
    final PortalError portalError = PortalError.create().status(cause.getStatus()).message(cause.getMessage()).exception(cause).request(req).build();
    final Site siteInRequest = req.getSite();
    final Site site = siteInRequest != null ? siteInRequest : callAsContentAdmin(() -> this.contentService.findNearestSiteByPath(req.getContentPath()));
    if (site != null) {
        req.setSite(site);
        try {
            for (SiteConfig siteConfig : site.getSiteConfigs()) {
                final ApplicationKey applicationKey = siteConfig.getApplicationKey();
                for (final String scriptPath : SITE_ERROR_SCRIPT_PATHS) {
                    final PortalResponse response = renderApplicationCustomError(applicationKey, scriptPath, portalError, handlerMethod);
                    if (response != null) {
                        if (response.isPostProcess()) {
                            req.setApplicationKey(applicationKey);
                        }
                        return response;
                    }
                }
            }
        } finally {
            req.setSite(siteInRequest);
        }
    } else if (req.getApplicationKey() != null) {
        final ApplicationKey applicationKey = req.getApplicationKey();
        final PortalResponse response = renderApplicationCustomError(applicationKey, GENERIC_ERROR_SCRIPT_PATH, portalError, handlerMethod);
        if (response != null) {
            if (response.isPostProcess()) {
                req.setApplicationKey(applicationKey);
            }
            return response;
        }
    }
    return null;
}
Also used : Site(com.enonic.xp.site.Site) ApplicationKey(com.enonic.xp.app.ApplicationKey) PortalResponse(com.enonic.xp.portal.PortalResponse) PortalError(com.enonic.xp.portal.impl.error.PortalError) SiteConfig(com.enonic.xp.site.SiteConfig)

Example 2 with PortalResponse

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

the class IdentityHandler method doHandle.

@Override
protected PortalResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    final String restPath = findRestPath(webRequest);
    final Matcher matcher = PATTERN.matcher(restPath);
    if (!matcher.find()) {
        throw WebException.notFound("Not a valid idprovider url pattern");
    }
    final IdProviderKey idProviderKey = IdProviderKey.from(matcher.group(ID_PROVIDER_GROUP_INDEX));
    final VirtualHost virtualHost = VirtualHostHelper.getVirtualHost(webRequest.getRawRequest());
    if (!(virtualHost == null || virtualHost.getIdProviderKeys().contains(idProviderKey))) {
        throw WebException.forbidden(String.format("'%s' id provider is forbidden", idProviderKey));
    }
    String idProviderFunction = matcher.group(2);
    final PortalRequest portalRequest = webRequest instanceof PortalRequest ? (PortalRequest) webRequest : new PortalRequest(webRequest);
    portalRequest.setContextPath(findPreRestPath(portalRequest) + "/" + matcher.group(ID_PROVIDER_GROUP_INDEX));
    if (idProviderFunction != null) {
        checkTicket(portalRequest);
    }
    if (idProviderFunction == null) {
        idProviderFunction = webRequest.getMethod().toString().toLowerCase();
    }
    final IdentityHandlerWorker worker = new IdentityHandlerWorker(portalRequest);
    worker.idProviderKey = idProviderKey;
    worker.idProviderFunction = idProviderFunction;
    worker.contentResolver = new ContentResolver(contentService);
    worker.idProviderControllerService = this.idProviderControllerService;
    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) Matcher(java.util.regex.Matcher) IdProviderKey(com.enonic.xp.security.IdProviderKey) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) PortalRequest(com.enonic.xp.portal.PortalRequest) ContentResolver(com.enonic.xp.portal.impl.ContentResolver)

Example 3 with PortalResponse

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

the class ExceptionMapperTest method assertThrowIfNeeded.

private void assertThrowIfNeeded(final HttpStatus status) {
    final PortalResponse response = PortalResponse.create().status(status).build();
    try {
        this.mapper.throwIfNeeded(response);
        fail("Should throw exception");
    } catch (final WebException e) {
        assertEquals(status, e.getStatus());
    }
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException)

Example 4 with PortalResponse

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

the class ExceptionMapperTest method throwIfNeeded_notNeeded.

@Test
public void throwIfNeeded_notNeeded() {
    final PortalResponse response = PortalResponse.create().status(HttpStatus.OK).build();
    this.mapper.throwIfNeeded(response);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Test(org.junit.jupiter.api.Test)

Example 5 with PortalResponse

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

the class MappingHandlerTest method methodNotAllowed.

@Test
public void methodNotAllowed() {
    final PortalResponse response = PortalResponse.create().build();
    this.request.setBaseUri("/admin/site");
    this.request.setContentPath(ContentPath.from("/site/content"));
    this.request.setMethod(HttpMethod.LOCK);
    final WebException webException = assertThrows(WebException.class, () -> this.handler.handle(this.request, response, null));
    assertEquals(HttpStatus.METHOD_NOT_ALLOWED, webException.getStatus());
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) 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