Class HttpUtils

java.lang.Object
com.groiss.servlet.HttpUtils

public class HttpUtils extends Object
Utility class for dealing with special cases in the area of the HTTP protocol.
  • Method Details

    • setNoCache

      @Deprecated public static void setNoCache(HttpServletResponse res)
      Set response to no-cache. Note that you have to call this method before data is written (getWriter() or getOutputStream() is called).
      Parameters:
      res - the HTTP-response
    • cookieName

      public static String cookieName(String suffix)
      Generate a cookie name by prepending the serverid and _ to the suffix.
      Parameters:
      suffix -
      Returns:
      <serverid>_<suffix>
    • newCookie

      public static Cookie newCookie(String suffix, String value)
      Generate a new Cookie. The cookie name is generated from the suffix via cookieName(String).

      the Path is set to the servlet context path and the maxAge to -1.

      Please be sure to set httpOnly for the cookie via Cookie.setHttpOnly(boolean) if appropriate, or use newHttpOnlyCookie(String, String) directly.

      Parameters:
      suffix - the suffix of the cookie name
      value - the cookie value
      Returns:
    • newHttpOnlyCookie

      public static Cookie newHttpOnlyCookie(String suffix, String value)
      Generate a new HTTP-only Cookie. The cookie name is generated from the suffix via cookieName(String).
      Parameters:
      suffix - the suffix of the cookie name
      value - the cookie value
      Returns:
    • setCookieSecurity

      public static void setCookieSecurity(Cookie c, HttpServletRequest req)
      Set the security attribute of a cookie. Depends on the request and on the configuration property "httpd.cookie.forceSecure".
      The security attribute of the cookie will be set to true if req.isSecure() it true or if the property httpd.cookie.forceSecure is true. Otherwise the security attribute will be set to false.
      Parameters:
      c - the cookie
      req - the request
    • getCookie

      public static Cookie getCookie(HttpServletRequest req, String suffix)
      return the cookie from the request.
      Parameters:
      req -
      suffix - the name part of the cookie that follows the server id.
      Returns:
      the cookie or null if no such cookie
    • getCookieValue

      public static String getCookieValue(HttpServletRequest req, String suffix)
      Returns a cookie value.
      Parameters:
      req -
      suffix - the name part of the cookie that follows the server id.
      Returns:
      the cookie value or null if no such cookie.
    • getRemoteAddress

      public static String getRemoteAddress(HttpServletRequest req)
      Returns the "real" remote address of a HttpServletRequest.
      Parameters:
      req -
      Returns:
      the leftmost component of the X-Forwarded-For header field, if it exists, req.getRemoteAddr() if not.
    • extractIP

      public static String extractIP(String result)
    • getRequestURLContext

      public static StringBuilder getRequestURLContext(HttpServletRequest req)