diff options
-rw-r--r-- | src/main/java/org/traccar/helper/IpRetriever.java | 62 | ||||
-rw-r--r-- | src/main/java/org/traccar/helper/LogAction.java | 8 | ||||
-rw-r--r-- | src/test/java/org/traccar/helper/IpRetrieverTest.java | 12 |
3 files changed, 40 insertions, 42 deletions
diff --git a/src/main/java/org/traccar/helper/IpRetriever.java b/src/main/java/org/traccar/helper/IpRetriever.java index 4bab93edb..814a78a4b 100644 --- a/src/main/java/org/traccar/helper/IpRetriever.java +++ b/src/main/java/org/traccar/helper/IpRetriever.java @@ -3,45 +3,45 @@ package org.traccar.helper; import javax.servlet.http.HttpServletRequest; /** - * Gets the client's IP address regardless of whether the server is behind a proxy/reverse proxy server or a load balancer. - * + * Gets the client's IP address regardless of whether + * the server is behind a proxy/reverse proxy server or a load balancer. */ -public final class IpRetriever - { - - - /** - * Retrieves the client's IP address. - * Handles the cases like whether the server is behind a proxy/reverse proxy server or a load balancer - * - * @param request {@link HttpServletRequest} instance - * @return client's IP address - */ +public final class IpRetriever { + + /** + * Retrieves the client's IP address. + * Handles the cases like whether the server is behind a proxy/reverse proxy server or a load balancer + * + * @param request {@link HttpServletRequest} instance + * @return client's IP address + */ public static String retrieveIP(HttpServletRequest request) { - if(request != null){ - String ipAddress = request.getHeader("X-FORWARDED-FOR"); + if (request != null) { + String ipAddress = request.getHeader("X-FORWARDED-FOR"); - if (ipAddress != null && !ipAddress.isEmpty()) { - return removeUnwantedData(ipAddress); - } - else{ - ipAddress = request.getRemoteAddr(); - return ipAddress; - } + if (ipAddress != null && !ipAddress.isEmpty()) { + return removeUnwantedData(ipAddress); + } else { + ipAddress = request.getRemoteAddr(); + return ipAddress; + } } else return null; } - /** - * If the server is behind a reverse proxy, the header value will also contain the IP's from load balancer and reverse proxy - * This method gets rid of them. - * - * @param ipAddress IP address value from the header - * @return IP address of the client - */ - private static String removeUnwantedData(String ipAddress){ + /** + * If the server is behind a reverse proxy, the header value will also contain the IP's from load balancer and reverse proxy + * This method gets rid of them. + * + * @param ipAddress IP address value from the header + * @return IP address of the client + */ + private static String removeUnwantedData(String ipAddress) { return ipAddress.contains(",") ? ipAddress.split(",")[0] : ipAddress; - } + } + + private IpRetriever() { + } } diff --git a/src/main/java/org/traccar/helper/LogAction.java b/src/main/java/org/traccar/helper/LogAction.java index 1fbd78c4d..21fe4c3fa 100644 --- a/src/main/java/org/traccar/helper/LogAction.java +++ b/src/main/java/org/traccar/helper/LogAction.java @@ -78,11 +78,9 @@ public final class LogAction { public static void failedLogin(String ipAddress) { - if(ipAddress == null || ipAddress.isEmpty()) { + if (ipAddress == null || ipAddress.isEmpty()) { LOGGER.info(ACTION_FAILED_LOGIN_NO_IP); - } - - else{ + } else { LOGGER.info(String.format( PATTERN_FAILED_LOGIN, ipAddress)); } @@ -101,7 +99,7 @@ public final class LogAction { } private static void logLinkAction(String action, long userId, - Class<?> owner, long ownerId, Class<?> property, long propertyId) { + Class<?> owner, long ownerId, Class<?> property, long propertyId) { LOGGER.info(String.format( PATTERN_LINK, userId, action, Introspector.decapitalize(owner.getSimpleName()), ownerId, diff --git a/src/test/java/org/traccar/helper/IpRetrieverTest.java b/src/test/java/org/traccar/helper/IpRetrieverTest.java index 628b5dd63..63e2c0073 100644 --- a/src/test/java/org/traccar/helper/IpRetrieverTest.java +++ b/src/test/java/org/traccar/helper/IpRetrieverTest.java @@ -16,22 +16,22 @@ public class IpRetrieverTest { private MockHttpServletRequest mockHttpServletRequest; @Before - public void init(){ + public void init() { mockHttpServletRequest = new MockHttpServletRequest(); } @Test - public void testIpBehindReverseProxy(){ + public void testIpBehindReverseProxy() { mockHttpServletRequest.setRemoteAddr(GATEWAY_IP_ADDRESS); - mockHttpServletRequest.addHeader("X-FORWARDED-FOR",IP_ADDRESS_BEHIND_REVERSE_PROXY); + mockHttpServletRequest.addHeader("X-FORWARDED-FOR", IP_ADDRESS_BEHIND_REVERSE_PROXY); assertEquals(NORMAL_IP_ADDRESS, IpRetriever.retrieveIP(mockHttpServletRequest)); } @Test - public void testNormalIp(){ - mockHttpServletRequest.setRemoteAddr(NORMAL_IP_ADDRESS); - assertEquals(NORMAL_IP_ADDRESS, IpRetriever.retrieveIP(mockHttpServletRequest)); + public void testNormalIp() { + mockHttpServletRequest.setRemoteAddr(NORMAL_IP_ADDRESS); + assertEquals(NORMAL_IP_ADDRESS, IpRetriever.retrieveIP(mockHttpServletRequest)); } |