From 45b4ccf8f6c397fe7a010c3d639ac558eaa51a9c Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 11 Feb 2018 21:58:03 +0200 Subject: [PATCH] refactor if --- .../dsl/ClientErrorLoggingFilter.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java index 56f7cea3b8..e4a88c8592 100644 --- a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java +++ b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java @@ -32,19 +32,24 @@ public class ClientErrorLoggingFilter extends GenericFilterBean { Authentication auth = SecurityContextHolder.getContext() .getAuthentication(); - if (auth != null) { - int status = ((HttpServletResponse) response).getStatus(); - if (status >= 400 && status < 500) { - if (errorCodes == null) { - logger.debug("User " + auth.getName() + " encountered error " + status); - } else { - if (errorCodes.stream() - .filter(s -> s.value() == status) - .findFirst() - .isPresent()) { - logger.debug("User " + auth.getName() + " encountered error " + status); - } - } + if (auth == null) { + chain.doFilter(request, response); + return; + } + int status = ((HttpServletResponse) response).getStatus(); + if (status < 400 || status >= 500) { + chain.doFilter(request, response); + return; + } + + if (errorCodes == null) { + logger.debug("User " + auth.getName() + " encountered error " + status); + } else { + if (errorCodes.stream() + .filter(s -> s.value() == status) + .findFirst() + .isPresent()) { + logger.debug("User " + auth.getName() + " encountered error " + status); } }