aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/helper/ServletHelper.java
blob: e3481e2491bc6584c88720d3e8a02b71ef456426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.traccar.helper;

import javax.servlet.http.HttpServletRequest;


public final class ServletHelper {

    private ServletHelper() {
    }

    public static String retrieveRemoteAddress(HttpServletRequest request) {

        if (request != null) {
            String remoteAddress = request.getHeader("X-FORWARDED-FOR");

            if (remoteAddress != null && !remoteAddress.isEmpty()) {
                return remoteAddress.substring(0, remoteAddress.indexOf(",")); // removes the additional data
            } else {
                remoteAddress = request.getRemoteAddr();
                return remoteAddress;
            }
        } else {
            return null;
        }
    }

}