Search in sources :

Example 86 with PortalResponse

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

the class ExceptionRendererImpl method render.

@Override
public PortalResponse render(final WebRequest webRequest, final WebException cause) {
    String tip = null;
    final ExceptionInfo info = toErrorInfo(cause);
    logIfNeeded(info);
    if (webRequest instanceof PortalRequest) {
        PortalRequest portalRequest = (PortalRequest) webRequest;
        final HttpStatus httpStatus = cause.getStatus();
        if (httpStatus != null) {
            final String handlerMethod = "handle" + httpStatus.value();
            final PortalResponse statusCustomError = renderCustomError(portalRequest, cause, handlerMethod);
            if (statusCustomError != null) {
                logIfNeeded(toErrorInfo(cause));
                return statusCustomError;
            }
        }
        final PortalResponse idProviderError = renderIdProviderError(portalRequest, cause);
        if (idProviderError != null) {
            logIfNeeded(toErrorInfo(cause));
            return idProviderError;
        }
        final PortalResponse defaultCustomError = renderCustomError(portalRequest, cause, DEFAULT_HANDLER);
        if (defaultCustomError != null) {
            logIfNeeded(toErrorInfo(cause));
            return defaultCustomError;
        }
        if ("/site".equals(portalRequest.getBaseUri()) && ContentConstants.BRANCH_MASTER.equals(portalRequest.getBranch()) && HttpStatus.NOT_FOUND.equals(cause.getStatus())) {
            tip = "Tip: Did you remember to publish the site?";
        }
    }
    return renderInternalErrorPage(webRequest, tip, cause);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) HttpStatus(com.enonic.xp.web.HttpStatus) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 87 with PortalResponse

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

the class AttachmentHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final Content content = getContent(this.id);
    final Attachment attachment = resolveAttachment(content, this.name);
    final BinaryReference binaryReference = attachment.getBinaryReference();
    final ByteSource binary = resolveBinary(this.id, binaryReference);
    if (request.getMethod() == HttpMethod.OPTIONS) {
        // it will be handled by default OPTIONS handler in BaseWebHandler
        return PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
    }
    final MediaType contentType = MediaType.parse(attachment.getMimeType());
    final PortalResponse.Builder portalResponse = PortalResponse.create().contentType(contentType).body(binary);
    if (this.download) {
        portalResponse.header("Content-Disposition", contentDispositionAttachment(attachment.getName()));
    }
    if (this.name.endsWith(".svgz")) {
        portalResponse.header("Content-Encoding", "gzip");
    }
    if (!nullToEmpty(this.fingerprint).isBlank()) {
        final boolean isPublic = content.getPermissions().isAllowedFor(RoleKeys.EVERYONE, Permission.READ) && ContentConstants.BRANCH_MASTER.equals(request.getBranch());
        final String cacheControlHeaderConfig = isPublic ? publicCacheControlHeaderConfig : privateCacheControlHeaderConfig;
        if (!nullToEmpty(cacheControlHeaderConfig).isBlank() && this.fingerprint.equals(resolveHash(this.id, binaryReference))) {
            portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
        }
    }
    new RangeRequestHelper().handleRangeRequest(request, portalResponse, binary, contentType);
    return portalResponse.build();
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) ServletRequestUrlHelper.contentDispositionAttachment(com.enonic.xp.web.servlet.ServletRequestUrlHelper.contentDispositionAttachment) Attachment(com.enonic.xp.attachment.Attachment) BinaryReference(com.enonic.xp.util.BinaryReference)

Example 88 with PortalResponse

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

the class IdentityHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final ContentResolverResult resolvedContent = contentResolver.resolve(this.request);
    this.request.setContent(resolvedContent.getContent());
    this.request.setSite(resolvedContent.getNearestSite());
    final IdProviderControllerExecutionParams executionParams = IdProviderControllerExecutionParams.create().idProviderKey(idProviderKey).functionName(idProviderFunction).portalRequest(this.request).build();
    final PortalResponse portalResponse = idProviderControllerService.execute(executionParams);
    if (portalResponse == null) {
        throw WebException.notFound(String.format("ID Provider function [%s] not found for id provider [%s]", idProviderFunction, idProviderKey));
    } else {
        return portalResponse;
    }
}
Also used : ContentResolverResult(com.enonic.xp.portal.impl.ContentResolverResult) PortalResponse(com.enonic.xp.portal.PortalResponse) IdProviderControllerExecutionParams(com.enonic.xp.portal.idprovider.IdProviderControllerExecutionParams)

Example 89 with PortalResponse

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

the class MappingHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final ControllerScript controllerScript = getScript();
    final Trace trace = Tracer.current();
    if (trace != null) {
        trace.put("contentPath", this.request.getContentPath().toString());
        trace.put("type", "mapping");
    }
    this.request.setControllerScript(controllerScript);
    final PortalResponse portalResponse = rendererDelegate.render(mappingDescriptor, this.request);
    final WebSocketConfig webSocketConfig = portalResponse.getWebSocket();
    final WebSocketContext webSocketContext = this.request.getWebSocketContext();
    if (webSocketContext != null && webSocketConfig != null) {
        final WebSocketEndpoint webSocketEndpoint = newWebSocketEndpoint(webSocketConfig, this::getScript, request.getApplicationKey());
        webSocketContext.apply(webSocketEndpoint);
    }
    return portalResponse;
}
Also used : Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) WebSocketConfig(com.enonic.xp.web.websocket.WebSocketConfig) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) WebSocketEndpoint(com.enonic.xp.web.websocket.WebSocketEndpoint) WebSocketContext(com.enonic.xp.web.websocket.WebSocketContext)

Example 90 with PortalResponse

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

the class PostProcessEvaluator method executeInstruction.

private HtmlBlocks executeInstruction(final String content) {
    for (final PostProcessInstruction instruction : this.instructions) {
        final PortalResponse instructionResponse = instruction.evaluate(this.portalRequest, content);
        if (instructionResponse == null) {
            continue;
        }
        final boolean hasHeaders = !instructionResponse.getHeaders().isEmpty();
        final boolean hasCookies = !instructionResponse.getCookies().isEmpty();
        final boolean hasContributions = instructionResponse.hasContributions();
        final boolean skipFilters = !instructionResponse.applyFilters();
        if (hasContributions || hasHeaders || skipFilters || hasCookies) {
            final PortalResponse.Builder newPortalResponse = PortalResponse.create(this.portalResponse);
            if (hasContributions) {
                newPortalResponse.contributionsFrom(instructionResponse);
            }
            if (hasHeaders) {
                newPortalResponse.headers(instructionResponse.getHeaders());
            }
            if (hasCookies) {
                newPortalResponse.cookies(instructionResponse.getCookies());
            }
            if (skipFilters) {
                newPortalResponse.applyFilters(false);
            }
            this.portalResponse = newPortalResponse.build();
        }
        final String resultBody = instructionResponse.getAsString();
        return resultBody == null ? null : new HtmlBlockParser().parse(resultBody);
    }
    return null;
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction) HtmlBlockParser(com.enonic.xp.portal.impl.parser.HtmlBlockParser)

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