cl
4 天以前 450a97460b086663bb07b418b48354b0a3125e85
rsf-http-audit/src/main/java/com/vincent/rsf/httpaudit/support/HttpAuditSupport.java
@@ -2,9 +2,14 @@
import com.vincent.rsf.httpaudit.props.HttpAuditProperties;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import javax.servlet.http.HttpServletRequest;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@@ -55,7 +60,7 @@
    public static boolean shouldExclude(HttpServletRequest request, HttpAuditProperties props) {
        String path = safePath(request);
        for (String p : props.getExcludePathPrefixes()) {
        for (String p : props.getEffectiveExcludePrefixes()) {
            if (p != null && !p.isEmpty() && path.startsWith(p)) {
                return true;
            }
@@ -119,4 +124,64 @@
        }
        return s.substring(0, maxChars) + "...(truncated,len=" + s.length() + ")";
    }
    /** maxChars<0 不截断 */
    public static String storeWithCharLimit(String s, int maxChars) {
        if (s == null) {
            return null;
        }
        if (maxChars < 0) {
            return s;
        }
        return truncateForStore(s, maxChars);
    }
    public static boolean overCharLimit(String s, int maxChars) {
        return s != null && maxChars >= 0 && s.length() > maxChars;
    }
    public static String truncateUriForStore(String uri, int maxLen) {
        if (uri == null) {
            return "";
        }
        if (maxLen <= 0 || uri.length() <= maxLen) {
            return uri;
        }
        return uri.substring(0, maxLen - 3) + "...";
    }
    /** 无 HTTP 状态码时由异常推断落库状态;超时类为 504 */
    public static Integer inferHttpStatusFromThrowable(Throwable ex) {
        if (ex == null) {
            return null;
        }
        for (Throwable t = ex; t != null; t = t.getCause()) {
            if (t instanceof SocketTimeoutException || t instanceof TimeoutException) {
                return 504;
            }
            if (t instanceof AsyncRequestTimeoutException) {
                return 504;
            }
            if (t instanceof ConnectException) {
                String m = t.getMessage();
                if (m != null && m.toLowerCase().contains("timed out")) {
                    return 504;
                }
            }
            if ("feign.RetryableException".equals(t.getClass().getName())) {
                Throwable c = t.getCause();
                if (c instanceof SocketTimeoutException) {
                    return 504;
                }
                String m = t.getMessage();
                if (m != null) {
                    String low = m.toLowerCase();
                    if (low.contains("timed out") || low.contains("timeout")) {
                        return 504;
                    }
                }
            }
        }
        return null;
    }
}