package org.zalando.nakadi.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
import java.util.Optional;

@Component
public class LoggingFilter extends OncePerRequestFilter {

    private static final Logger LOG = LoggerFactory.getLogger(LoggingFilter.class);

    @Override
    protected void doFilterInternal(final HttpServletRequest request,
                                    final HttpServletResponse response, final FilterChain filterChain)
            throws ServletException, IOException {
        final long start = System.currentTimeMillis();
        final String userAgent = Optional.ofNullable(request.getHeader("User-Agent")).orElse("-");
        final String user = Optional.ofNullable(request.getUserPrincipal()).map(Principal::getName).orElse("-");
        final String method = request.getMethod();
        final String path = request.getRequestURI();
        LOG.info("[ACCESS_LOG_BEGIN] {} \"{}\" \"{}\" \"{}\"",
                method,
                path,
                userAgent,
                user);
        //execute request
        try {
            filterChain.doFilter(request, response);
        } finally {
            final long time = System.currentTimeMillis();
            final Long timing = time - start;
            LOG.info("[ACCESS_LOG_END] {} \"{}\" \"{}\" \"{}\" {} {} ms",
                    method,
                    path,
                    userAgent,
                    user,
                    response.getStatus(),
                    timing);
        }
    }
}

@@ -891,32 +891,257 @@
 ntTimeMillis();%0A
+        try %7B%0A            //execute request%0A            filterChain.doFilter(request, response);%0A        %7D finally %7B%0A            final long time = System.currentTimeMillis();%0A            final Long timing = time - start;%0A    
         final St
@@ -1215,32 +1215,36 @@
 )).orElse(%22-%22);%0A
+    
         final St
@@ -1340,32 +1340,36 @@
 e(%22-%22);%0A        
+   
+ 
 final String met
@@ -1391,24 +1391,28 @@
 etMethod();%0A
+    
         fina
@@ -1456,384 +1456,8 @@
 ();%0A
-        LOG.info(%22%5BACCESS_LOG_BEGIN%5D %7B%7D %5C%22%7B%7D%5C%22 %5C%22%7B%7D%5C%22 %5C%22%7B%7D%5C%22%22,%0A                method,%0A                path,%0A                userAgent,%0A                user);%0A        //execute request%0A        try %7B%0A            filterChain.doFilter(request, response);%0A        %7D finally %7B%0A            final long time = System.currentTimeMillis();%0A            final Long timing = time - start;%0A
     
@@ -1489,12 +1489,8 @@
 _LOG
-_END
 %5D %7B%7D

fix logging