From 13b27f8d7ac08bb3ff1a56a48865201263d6462d Mon Sep 17 00:00:00 2001 From: developerKurt Date: Fri, 3 Jan 2020 10:20:07 +0300 Subject: As the owner requested: • Javadocs are removed • Spring test framework removed, created custom mock implementation instead • Added class and method renamed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/traccar/api/resource/SessionResource.java | 4 +- src/main/java/org/traccar/helper/IpRetriever.java | 50 ---------------------- .../java/org/traccar/helper/ServletHelper.java | 27 ++++++++++++ 3 files changed, 29 insertions(+), 52 deletions(-) delete mode 100644 src/main/java/org/traccar/helper/IpRetriever.java create mode 100644 src/main/java/org/traccar/helper/ServletHelper.java (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/api/resource/SessionResource.java b/src/main/java/org/traccar/api/resource/SessionResource.java index 65e05a4a2..e3c5d457f 100644 --- a/src/main/java/org/traccar/api/resource/SessionResource.java +++ b/src/main/java/org/traccar/api/resource/SessionResource.java @@ -18,7 +18,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.helper.DataConverter; -import org.traccar.helper.IpRetriever; +import org.traccar.helper.ServletHelper; import org.traccar.helper.LogAction; import org.traccar.model.User; @@ -107,7 +107,7 @@ public class SessionResource extends BaseResource { LogAction.login(user.getId()); return user; } else { - LogAction.failedLogin(IpRetriever.retrieveIP(request)); + LogAction.failedLogin(ServletHelper.retrieveRemoteAddress(request)); throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED).build()); } } diff --git a/src/main/java/org/traccar/helper/IpRetriever.java b/src/main/java/org/traccar/helper/IpRetriever.java deleted file mode 100644 index ff211d181..000000000 --- a/src/main/java/org/traccar/helper/IpRetriever.java +++ /dev/null @@ -1,50 +0,0 @@ -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. - */ -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 (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) { - return ipAddress.contains(",") ? ipAddress.split(",")[0] : ipAddress; - } - - private IpRetriever() { - } -} diff --git a/src/main/java/org/traccar/helper/ServletHelper.java b/src/main/java/org/traccar/helper/ServletHelper.java new file mode 100644 index 000000000..4eab49001 --- /dev/null +++ b/src/main/java/org/traccar/helper/ServletHelper.java @@ -0,0 +1,27 @@ +package org.traccar.helper; + +import javax.servlet.http.HttpServletRequest; + + +public final class ServletHelper { + + + public static String retrieveRemoteAddress(HttpServletRequest request) { + + if (request != null) { + String ipAddress = request.getHeader("X-FORWARDED-FOR"); + + if (ipAddress != null && !ipAddress.isEmpty()) { + return ipAddress.substring(0, ipAddress.indexOf(",")); //Removes the additional data + } else { + ipAddress = request.getRemoteAddr(); + return ipAddress; + } + + } else { + return null; + } + + + } +} \ No newline at end of file -- cgit v1.2.3