All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rformagio.observability.logging.logger.LoggerHttp Maven / Gradle / Ivy

package com.github.rformagio.observability.logging.logger;

import com.github.rformagio.observability.logging.model.RequestContent;
import com.github.rformagio.observability.logging.model.ResponseContent;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Aspect
@Component
public class LoggerHttp {

    @Pointcut("(@annotation(org.springframework.web.bind.annotation.GetMapping)" +
            " || @annotation(org.springframework.web.bind.annotation.PostMapping)" +
            " || @annotation(org.springframework.web.bind.annotation.PutMapping)" +
            " || @annotation(org.springframework.web.bind.annotation.PatchMapping)) " +
            " && @annotation(com.github.rformagio.observability.annotation.EnableRequestResponseLogging)")
    public void doAction() {}


    @Before("doAction()")
    public void beforeAction(JoinPoint joinPoint) {

        RequestContent requestContent = new RequestContent();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest();

        StringBuilder sb = new StringBuilder();
        sb.append(request.getScheme());
        sb.append("://");
        sb.append(request.getServerName());
        sb.append(":");
        sb.append(request.getServerPort());
        sb.append(request.getContextPath());
        sb.append(request.getRequestURI());

        requestContent.setUrl(sb.toString());
        requestContent.setQueryString(request.getQueryString());
        requestContent.setMethod(request.getMethod());
        requestContent.setIp(request.getRemoteAddr());
        requestContent.setProtocol(request.getProtocol());

        requestContent.setHeaders(getHeadersInfo(request));
        requestContent.setPayload(joinPoint.getArgs());

        log.info("REQUEST", requestContent);

    }

    @AfterReturning(value = "doAction()", returning = "result")
    public void afterAction(JoinPoint joinPoint, Object result) {

        ResponseContent responseContent = new ResponseContent();

        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getResponse();

        responseContent.setStatus(String.valueOf(response.getStatus()));
        responseContent.setHeaders(getHeadersInfo(response));
        responseContent.setBody(result);
        log.info("RESPONSE", responseContent);
    }

    private  Map getHeadersInfo(HttpServletRequest request) {

        Map map = new HashMap<>();

        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }

        return map;
    }

    private  Map getHeadersInfo(HttpServletResponse response) {

        Map map;

        Collection headerNames = response.getHeaderNames();
        map = headerNames.stream().collect(Collectors.toMap( String::trim, response::getHeader));

        return map;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy