Skip to content

Instantly share code, notes, and snippets.

@ankushs92
Created May 26, 2016 17:43
Show Gist options
  • Save ankushs92/26fe78553478a5c8d6d6a58d7c4c494f to your computer and use it in GitHub Desktop.
Save ankushs92/26fe78553478a5c8d6d6a58d7c4c494f to your computer and use it in GitHub Desktop.
Some helper methods for HttpServletRequest
public class HttpServletRequestUtils {
public static String getIp(final HttpServletRequest request) {
PreConditions.checkNull(request, "request cannot be null");
String ip = request.getHeader("X-FORWARDED-FOR");
if (ip==null || ip.trim().length()==0)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static Integer getErrorCode(final HttpServletRequest request){
PreConditions.checkNull(request, "request cannot be null");
return (Integer) request.getAttribute("javax.servlet.error.status_code");
}
public static Throwable getThrowable(final HttpServletRequest request){
PreConditions.checkNull(request, "request cannot be null");
return (Throwable)request.getAttribute("javax.servlet.error.exception");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment