Permalink
Browse files

Added Servlets#getRemoteAddr().

  • Loading branch information...
1 parent eef138e commit 72d3aae8a8149c37ebf29b2a5667759b79cd1de8 @BalusC BalusC committed Dec 30, 2015
Showing with 15 additions and 7 deletions.
  1. +1 −7 src/main/java/org/omnifaces/util/FacesLocal.java
  2. +14 −0 src/main/java/org/omnifaces/util/Servlets.java
@@ -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 --------------------------------------------------------------------------------------------------
@@ -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 --------------------------------------------------------------------------------------------
/**

0 comments on commit 72d3aae

Please sign in to comment.