Search in sources :

Example 1 with Trace

use of com.enonic.xp.trace.Trace in project xp by enonic.

the class WebAppHandler method doHandle.

@Override
protected WebResponse doHandle(final WebRequest webRequest, final WebResponse res, final WebHandlerChain chain) throws Exception {
    PortalRequest portalRequest = (PortalRequest) webRequest;
    portalRequest.setContextPath(portalRequest.getBaseUri());
    final Matcher matcher = PATTERN.matcher(portalRequest.getRawPath());
    matcher.matches();
    final ApplicationKey applicationKey = ApplicationKey.from(matcher.group(1));
    final String restPath = matcher.group(2);
    final Trace trace = Tracer.newTrace("renderApp");
    if (trace == null) {
        return handleAppRequest(portalRequest, applicationKey, restPath);
    }
    return Tracer.traceEx(trace, () -> {
        final WebResponse resp = handleAppRequest(portalRequest, applicationKey, restPath);
        addTraceInfo(trace, applicationKey, restPath);
        return resp;
    });
}
Also used : Trace(com.enonic.xp.trace.Trace) ApplicationKey(com.enonic.xp.app.ApplicationKey) WebResponse(com.enonic.xp.web.WebResponse) Matcher(java.util.regex.Matcher) PortalRequest(com.enonic.xp.portal.PortalRequest)

Example 2 with Trace

use of com.enonic.xp.trace.Trace in project xp by enonic.

the class ComponentHandler method doHandle.

@Override
protected PortalResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    WebHandlerHelper.checkAdminAccess(webRequest);
    final String restPath = findRestPath(webRequest);
    final ComponentHandlerWorker worker = new ComponentHandlerWorker((PortalRequest) webRequest);
    worker.componentPath = ComponentPath.from(restPath);
    worker.contentService = contentService;
    worker.contentResolver = new ContentResolver(contentService);
    worker.rendererDelegate = rendererDelegate;
    worker.pageResolver = new PageResolver(pageTemplateService);
    worker.postProcessor = postProcessor;
    final Trace trace = Tracer.newTrace("renderComponent");
    if (trace == null) {
        return worker.execute();
    }
    return Tracer.traceEx(trace, worker::execute);
}
Also used : Trace(com.enonic.xp.trace.Trace) ContentResolver(com.enonic.xp.portal.impl.ContentResolver)

Example 3 with Trace

use of com.enonic.xp.trace.Trace 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 4 with Trace

use of com.enonic.xp.trace.Trace in project xp by enonic.

the class MappingHandler method handleController.

private PortalResponse handleController(final PortalRequest portalRequest, final ControllerMappingDescriptor mapping) throws Exception {
    final MappingHandlerWorker worker = new MappingHandlerWorker(portalRequest);
    worker.mappingDescriptor = mapping;
    worker.resourceService = this.resourceService;
    worker.controllerScriptFactory = this.controllerScriptFactory;
    worker.rendererDelegate = rendererDelegate;
    final Trace trace = Tracer.newTrace("renderComponent");
    if (trace == null) {
        return worker.execute();
    }
    return Tracer.traceEx(trace, worker::execute);
}
Also used : Trace(com.enonic.xp.trace.Trace)

Example 5 with Trace

use of com.enonic.xp.trace.Trace in project xp by enonic.

the class ImageHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final Media content = getImage(this.contentId);
    final String contentName = content.getName().toString();
    final boolean contentNameEquals = contentName.equals(this.name);
    if (!(contentNameEquals || contentName.equals(Files.getNameWithoutExtension(this.name)))) {
        throw WebException.notFound(String.format("Image [%s] not found for content [%s]", this.name, this.contentId));
    }
    final Attachment attachment = content.getMediaAttachment();
    if (attachment == null) {
        throw WebException.notFound(String.format("Attachment [%s] not found", content.getName()));
    }
    final BinaryReference binaryReference = attachment.getBinaryReference();
    final ByteSource binary = this.contentService.getBinary(this.contentId, binaryReference);
    if (binary == null) {
        throw WebException.notFound(String.format("Binary [%s] not found for content [%s]", binaryReference, this.contentId));
    }
    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 String attachmentMimeType = attachment.getMimeType();
    final PortalResponse.Builder portalResponse = PortalResponse.create();
    if ("svgz".equals(attachment.getExtension())) {
        portalResponse.contentType(MediaType.SVG_UTF_8.withoutParameters());
        portalResponse.header("Content-Encoding", "gzip");
        portalResponse.body(binary);
    } else if (attachmentMimeType.equals("image/svg+xml") || attachmentMimeType.equals("image/gif")) {
        portalResponse.contentType(MediaType.parse(attachmentMimeType));
        portalResponse.body(binary);
    } else {
        final ImageOrientation imageOrientation = Objects.requireNonNullElseGet(content.getOrientation(), () -> Objects.requireNonNullElse(mediaInfoService.getImageOrientation(binary), ImageOrientation.TopLeft));
        final MediaType mimeType = contentNameEquals ? MediaType.parse(attachmentMimeType) : MediaTypes.instance().fromFile(this.name);
        portalResponse.contentType(mimeType);
        try {
            final ReadImageParams readImageParams = ReadImageParams.newImageParams().contentId(this.contentId).binaryReference(binaryReference).cropping(content.getCropping()).scaleParams(this.scaleParams).focalPoint(content.getFocalPoint()).filterParam(this.filterParam).backgroundColor(getBackgroundColor()).mimeType(mimeType.toString()).quality(getImageQuality()).orientation(imageOrientation).build();
            portalResponse.body(this.imageService.readImage(readImageParams));
        } catch (IllegalArgumentException e) {
            throw new WebException(HttpStatus.BAD_REQUEST, "Invalid parameters", e);
        } catch (ThrottlingException e) {
            throw new WebException(HttpStatus.TOO_MANY_REQUESTS, "Try again later", e);
        }
    }
    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(content))) {
            portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
        }
    }
    final Trace trace = Tracer.current();
    if (trace != null) {
        trace.put("contentPath", content.getPath());
        trace.put("type", "image");
    }
    return portalResponse.build();
}
Also used : WebException(com.enonic.xp.web.WebException) Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) BinaryReference(com.enonic.xp.util.BinaryReference) ImageOrientation(com.enonic.xp.media.ImageOrientation) ReadImageParams(com.enonic.xp.image.ReadImageParams) Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) ThrottlingException(com.enonic.xp.exception.ThrottlingException)

Aggregations

Trace (com.enonic.xp.trace.Trace)37 Content (com.enonic.xp.content.Content)6 PortalResponse (com.enonic.xp.portal.PortalResponse)6 Node (com.enonic.xp.node.Node)4 PortalRequest (com.enonic.xp.portal.PortalRequest)4 Site (com.enonic.xp.site.Site)4 DescriptorKey (com.enonic.xp.page.DescriptorKey)3 ContentResolver (com.enonic.xp.portal.impl.ContentResolver)3 ApplicationKey (com.enonic.xp.app.ApplicationKey)2 ContentResolverResult (com.enonic.xp.portal.impl.ContentResolverResult)2 WebResponse (com.enonic.xp.web.WebResponse)2 Matcher (java.util.regex.Matcher)2 Attachment (com.enonic.xp.attachment.Attachment)1 FindContentByParentResult (com.enonic.xp.content.FindContentByParentResult)1 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 Media (com.enonic.xp.content.Media)1 ThrottlingException (com.enonic.xp.exception.ThrottlingException)1 ReadImageParams (com.enonic.xp.image.ReadImageParams)1 ImageOrientation (com.enonic.xp.media.ImageOrientation)1 FindNodesByParentResult (com.enonic.xp.node.FindNodesByParentResult)1