Skip to content

Commit

Permalink
Added Servlets#getRemoteAddr().
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauke Scholtz committed Dec 30, 2015
1 parent eef138e commit 72d3aae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/main/java/org/omnifaces/util/FacesLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -846,13 +846,7 @@ public static String getForwardRequestURIWithQueryString(FacesContext context) {
* @see Faces#getRemoteAddr()
*/
public static String getRemoteAddr(FacesContext context) {
String forwardedFor = getRequestHeader(context, "X-Forwarded-For");

if (!Utils.isEmpty(forwardedFor)) {
return forwardedFor.split("\\s*,\\s*", 2)[0]; // It's a comma separated string: client,proxy1,proxy2,...
}

return getRequest(context).getRemoteAddr();
return Servlets.getRemoteAddr(getRequest(context));
}

// HTTP response --------------------------------------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/omnifaces/util/Servlets.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,20 @@ public static String toQueryString(List<ParamHolder> params) {
return queryString.toString();
}

/**
* Returns the Internet Protocol (IP) address of the client that sent the request. This will first check the
* <code>X-Forwarded-For</code> request header and if it's present, then return its first IP address, else just
* return {@link HttpServletRequest#getRemoteAddr()} unmodified.
* @param request The involved HTTP servlet request.
* @return The IP address of the client.
* @see HttpServletRequest#getRemoteAddr()
* @since 2.3
*/
public static String getRemoteAddr(HttpServletRequest request) {
String forwardedFor = request.getHeader("X-Forwarded-For");
return isEmpty(forwardedFor) ? request.getRemoteAddr() : forwardedFor.split("\\s*,\\s*", 2)[0]; // It's a comma separated string: client,proxy1,proxy2,...
}

// HttpServletResponse --------------------------------------------------------------------------------------------

/**
Expand Down

0 comments on commit 72d3aae

Please sign in to comment.