Example usage for javax.servlet.http HttpServletRequest getRemoteAddr

List of usage examples for javax.servlet.http HttpServletRequest getRemoteAddr

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getRemoteAddr.

Prototype

public String getRemoteAddr();

Source Link

Document

Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.

Usage

From source file:com.qatickets.common.HttpUtils.java

public static String getIP(HttpServletRequest req) {
    if (StringUtils.isNotBlank(req.getHeader("X-Real-IP"))) {
        return req.getHeader("X-Real-IP");
    }/* w  w w . ja  v  a 2  s .  com*/
    return req.getRemoteAddr();
}

From source file:com.inkubator.hrm.util.HrmUserInfoUtil.java

public static String getRequestRemoteAddrByJSF() {
    //        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
    //                   .getRequest(); 
    //        return request.getRemoteAddr();

    HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance()
            .getExternalContext().getRequest();
    return httpServletRequest.getRemoteAddr();
}

From source file:com.neocotic.bloggaer.common.Requests.java

/**
 * Returns the IP address that appears to be the source of the specified {@code request}.
 * //from  www  .ja  v a  2s  .  com
 * @param request
 *            the {@code HttpServletRequest} to check
 * @return The IP address.
 */
public static String getIpAddress(HttpServletRequest request) {
    logger.entering(request);

    String ipAddress = request.getHeader("X-Forwarded-For");
    if (ipAddress == null) {
        ipAddress = request.getRemoteAddr();
    } else {
        String[] ipAddresses = StringUtils.split(ipAddress, ",");
        if (ipAddresses.length > 0)
            ipAddress = ipAddresses[0];
    }

    logger.exiting(ipAddress);
    return ipAddress;
}

From source file:eu.eubrazilcc.lvl.core.servlet.ServletUtils.java

/**
 * Gets the client's IP address that's accessing the application, inspecting the request for 
 * HTTP header "X-Forwarded-For (XFF)" and, when no header is found, it uses the IP reported
 * in the request.//from  ww  w .j  a  va  2s.c o  m
 * @param request - input request
 * @return the client's IP address that's accessing the application.
 */
public static final String getClientAddress(final HttpServletRequest request) {
    checkArgument(request != null, "Uninitialized request");
    String address = request.getHeader("X-Forwarded-For");
    if (isBlank(address)) {
        address = request.getRemoteAddr();
    }
    return address;
}

From source file:de.metas.ui.web.config.ServletLoggingFilter.java

private static final String extractRemoteAddr(final HttpServletRequest httpRequest) {
    try {//from w ww  .  j  a va  2  s  .co  m
        final String remoteAddr = httpRequest.getRemoteAddr();
        if (remoteAddr == null || remoteAddr.isEmpty()) {
            return MDC_Param_RemoteAddr_DefaultValue;
        }
        return remoteAddr;
    } catch (final Exception e) {
        return "?";
    }
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditInfoExtractorTest.java

private static HttpServletRequest getHttpServletRequest() {
    HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
    Mockito.when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
    Mockito.when(httpServletRequest.getRemoteHost()).thenReturn("127.0.0.1");
    Mockito.when(httpServletRequest.getRemoteUser())
            .thenReturn("User[id=2, name=admin, group=[UserGroup[id=1, groupName=everyone]], role=ADMIN]");
    Mockito.when(httpServletRequest.getServerName()).thenReturn("localhost");
    UserGroup userGroup = Mockito.mock(UserGroup.class);
    Mockito.when(userGroup.getGroupName()).thenReturn("everyone");
    User user = Mockito.mock(User.class);
    Mockito.when(user.getName()).thenReturn("admin");
    Mockito.when(user.getRole()).thenReturn(Role.ADMIN);
    Mockito.when(user.getGroups()).thenReturn(Collections.singleton(userGroup));
    Authentication authentication = Mockito.mock(Authentication.class);
    Mockito.when(authentication.getPrincipal()).thenReturn(user);
    Mockito.when(httpServletRequest.getUserPrincipal()).thenReturn(authentication);
    return httpServletRequest;
}

From source file:com.envision.envservice.common.util.IPUtil.java

/**
 * ?IP?/*from  w ww . j  a  va  2  s  .  c  om*/
 * 
 * @Title: getRemoteAddr 
 * @return IP 
 * @Date 2015-10-30
 */
public static String getRemoteAddr(HttpServletRequest request) {
    String ipAddress = null;

    String proxyIP = request.getHeader(Constants.PROXY_IP);
    if (StringUtils.isNotEmpty(proxyIP)) {
        ipAddress = proxyIP;
    } else {
        ipAddress = request.getRemoteAddr();
    }

    return ipAddress;
}

From source file:com.asual.summer.core.RequestFilter.java

static String getRemoteAddr(HttpServletRequest request) {
    String result = request.getHeader("X-Forwarded-For");
    if (result == null) {
        result = request.getRemoteAddr();
    }/*from ww w.ja v a2 s  .  c  o  m*/
    try {
        result = java.net.InetAddress.getByName(result).getHostAddress();
    } catch (NoClassDefFoundError e) {
    } catch (UnknownHostException e) {
    }
    return result;
}

From source file:ee.ria.xroad.proxy.clientproxy.AbstractClientProxyHandler.java

private static long logPerformanceBegin(HttpServletRequest request) {
    long start = PerformanceLogger.log(log, "Received request from " + request.getRemoteAddr());
    log.info("Received request from {}", request.getRemoteAddr());

    return start;
}

From source file:com.pushinginertia.commons.net.util.HttpServletRequestUtils.java

/**
 * Identifies the remote IP address from a request, even if the application is running behind a forwarding agent.
 * @param req request received from the user agent
 * @return null if the ip address cannot be identified
 */// w  w w .  j  a  v  a 2 s  .c  o m
public static String getRemoteIpAddress(final HttpServletRequest req) {
    ValidateAs.notNull(req, "req");

    final String xForwardedFor = req.getHeader(X_FORWARDED_FOR); // method is case insensitive
    if (xForwardedFor == null) {
        return req.getRemoteAddr();
    }

    final IpAddress remoteIpAddress = getRemoteIpAddressFromXForwardedFor(xForwardedFor);
    if (IpAddressUtils.isNonRoutable(remoteIpAddress)) {
        final String remoteAddr = req.getRemoteAddr();
        LOG.warn(X_FORWARDED_FOR + " reports a non-routable IP [" + xForwardedFor
                + "]. This should always report the remote IP address. Servlet reports remote addr ["
                + remoteAddr + "]. Full request:\n" + toString(req));
        if (!IpAddressUtils.isNonRoutable(new IpAddress(remoteAddr))) {
            // return the ip address reported by the servlet if it's routable
            return remoteAddr;
        }
    }
    return remoteIpAddress.getIpAddress();
}