<commit_before>package mingzuozhibi.action;

import mingzuozhibi.support.Dao;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
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.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Optional;

public class BaseController {

    protected static final String MEDIA_TYPE = MediaType.APPLICATION_JSON_UTF8_VALUE;
    protected Logger LOGGER;

    @Autowired
    protected Dao dao;

    public BaseController() {
        LOGGER = LoggerFactory.getLogger(this.getClass());
    }

    protected String objectResult(Object result) {
        JSONObject root = new JSONObject();
        root.put("success", true);
        root.put("data", result);
        return root.toString();
    }

    protected String errorMessage(String message) {
        JSONObject root = new JSONObject();
        root.put("success", false);
        root.put("message", message);
        return root.toString();
    }

    protected void responseText(HttpServletResponse response, String content) throws IOException {
        response.setContentType(MEDIA_TYPE);
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        response.setContentLength(bytes.length);
        response.getOutputStream().write(bytes);
        response.flushBuffer();
    }

    protected Optional<Authentication> getAuthentication() {
        return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication());
    }

    protected String getUserName() {
        return getAuthentication().map(Authentication::getName).orElse("null");
    }

    protected ServletRequestAttributes getAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return ((ServletRequestAttributes) attributes);

    }

    private String getCommon() {
        HttpServletRequest request = getAttributes().getRequest();
        String common = String.format("[%s][%s][%s][%s][%s]",
                request.getRemoteAddr(), getUserName(), request.getMethod(),
                request.getRequestURI(), paramString(request));
        return common.replace("{}", "\\{}");
    }

    private String paramString(HttpServletRequest request) {
        StringBuilder builder = new StringBuilder();
        request.getParameterMap().forEach((key, arr) -> {
            if (arr.length == 0) {
                builder.append(key).append('&');
            } else if (arr.length == 1) {
                builder.append(key).append('=').append(arr[0]).append('&');
            } else {
                builder.append(key).append('=').append(Arrays.toString(arr)).append('&');
            }
        });
        if (builder.length() > 0) {
            builder.deleteCharAt(builder.length() - 1);
        } else {
            builder.append("NoParam");
        }
        return builder.toString();
    }

    protected void debugRequest(String format, Object... args) {
        LOGGER.debug(getCommon() + format, args);
    }<commit_msg>fix(nginx): Handle Real Ip<commit_after>package mingzuozhibi.action;

import mingzuozhibi.support.Dao;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
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.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Optional;

public class BaseController {

    protected static final String MEDIA_TYPE = MediaType.APPLICATION_JSON_UTF8_VALUE;
    protected Logger LOGGER;

    @Autowired
    protected Dao dao;

    public BaseController() {
        LOGGER = LoggerFactory.getLogger(this.getClass());
    }

    protected String objectResult(Object result) {
        JSONObject root = new JSONObject();
        root.put("success", true);
        root.put("data", result);
        return root.toString();
    }

    protected String errorMessage(String message) {
        JSONObject root = new JSONObject();
        root.put("success", false);
        root.put("message", message);
        return root.toString();
    }

    protected void responseText(HttpServletResponse response, String content) throws IOException {
        response.setContentType(MEDIA_TYPE);
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        response.setContentLength(bytes.length);
        response.getOutputStream().write(bytes);
        response.flushBuffer();
    }

    protected Optional<Authentication> getAuthentication() {
        return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication());
    }

    protected String getUserName() {
        return getAuthentication().map(Authentication::getName).orElse("null");
    }

    protected ServletRequestAttributes getAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return ((ServletRequestAttributes) attributes);
    }

    private String getRemoteAddr() {
        HttpServletRequest request = getAttributes().getRequest();
        String xRealIp = request.getHeader("X-Real-IP");
        return xRealIp != null ? xRealIp : request.getRemoteAddr();
    }

    private String getCommon() {
        HttpServletRequest request = getAttributes().getRequest();
        String common = String.format("[%s][%s][%s][%s][%s]",
                getRemoteAddr(), getUserName(), request.getMethod(),
                request.getRequestURI(), paramString(request));
        return common.replace("{}", "\\{}");
    }

    private String paramString(HttpServletRequest request) {
        StringBuilder builder = new StringBuilder();
        request.getParameterMap().forEach((key, arr) -> {
            if (arr.length == 0) {
                builder.append(key).append('&');
            } else if (arr.length == 1) {
                builder.append(key).append('=').append(arr[0]).append('&');
            } else {
                builder.append(key).append('=').append(Arrays.toString(arr)).append('&');
            }
        });
        if (builder.length() > 0) {
            builder.deleteCharAt(builder.length() - 1);
        } else {
            builder.append("NoParam");
        }
        return builder.toString();
    }

    protected void debugRequest(String format, Object... args) {
        LOGGER.debug(getCommon() + format, args);
    }
{'fraction_non_alphanumeric': 0.12098092643051771, 'fraction_numerical': 0.002452316076294278, 'mean_word_length': 3.2655432887855897, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5708568', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1871, 'n_words': 363}