package main.java.filters;

import main.java.dao.MongoDBAccessLogDao;
import main.java.model.AccessLogModel;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

public class LoggingRequestFilter implements Filter {

    private ServletContext context;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.context = filterConfig.getServletContext();
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        AccessLogModel accessLogModel = new AccessLogModel();
        accessLogModel.setCreated(new Date());
        accessLogModel.setIp(request.getRemoteAddr());
        accessLogModel.setUrlPath(req.getRequestURI());
        accessLogModel.setUserAgent(req.getHeader("User-Agent"));
        MongoDBAccessLogDao mongoDBAccessLogDao = new MongoDBAccessLogDao();
        mongoDBAccessLogDao.createAccessLog(accessLogModel);

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}

@@ -824,28 +824,19 @@
 p(re
-quest.getRemoteAddr(
+solveIp(req
 ));%0A
@@ -1194,11 +1194,264 @@
 %7B%0A%0A    %7D
+%0A%0A    protected String resolveIp( HttpServletRequest request ) %7B%0A        String ipAddress = request.getHeader(%22X-FORWARDED-FOR%22);%0A        if (ipAddress == null) %7B%0A            ipAddress = request.getRemoteAddr();%0A        %7D%0A        return ipAddress;%0A    %7D
 %0A%7D%0A

change to get forward ip