Class HttpUtils

java.lang.Object
com.erudika.para.server.utils.HttpUtils

public final class HttpUtils extends Object
Various utilities for HTTP stuff - cookies, AJAX, etc.
Author:
Alex Bogdanovski [[email protected]]
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A class for handling the form data from form submissions.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    getCookieValue(jakarta.servlet.http.HttpServletRequest req, String name)
    Reads a cookie.
    static String
    getStateParam(String name, jakarta.servlet.http.HttpServletRequest req)
    Reads a cookie.
    static boolean
    isAjaxRequest(jakarta.servlet.http.HttpServletRequest request)
    Checks if a request comes from JavaScript.
    static boolean
    isValidCaptchaResponse(String captchaSecretKey, jakarta.servlet.http.HttpServletRequest request)
    Captcha validation method for reCAPTCHA v3, Turnstile and hCaptcha.
    static boolean
    isValidCaptchaResponse(String captchaParamKey, String captchaParamValue, String captchaSecretKey)
    Captcha validation method for reCAPTCHA v3, Turnstile and hCaptcha.
    static void
    removeStateParam(String name, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res)
    Deletes a cookie.
    static org.springframework.http.HttpStatus
    sendEmail(HttpUtils.MultipartForm formData, com.erudika.para.core.App app, com.erudika.para.core.Form form)
    Sends emails with rate limiting enabled.
    static void
    setAuthCookie(String name, String value, boolean httpOnly, int maxAge, String sameSite, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Sets the auth cookie.
    static void
    setAuthCookie(String name, String value, int maxAge, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Sets the auth cookie.
    static void
    setRawCookie(String name, String value, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, boolean httpOnly, int maxAge)
    Sets a cookie.
    static void
    setStateParam(String name, String value, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res)
    Sets a cookie.
    static void
    setStateParam(String name, String value, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, boolean httpOnly)
    Sets a cookie.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isAjaxRequest

      public static boolean isAjaxRequest(jakarta.servlet.http.HttpServletRequest request)
      Checks if a request comes from JavaScript.
      Parameters:
      request - HTTP request
      Returns:
      true if AJAX
    • setStateParam

      public static void setStateParam(String name, String value, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res)
      Sets a cookie.
      Parameters:
      name - the name
      value - the value
      req - HTTP request
      res - HTTP response
    • setStateParam

      public static void setStateParam(String name, String value, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, boolean httpOnly)
      Sets a cookie.
      Parameters:
      name - the name
      value - the value
      req - HTTP request
      res - HTTP response
      httpOnly - HTTP only flag
    • getStateParam

      public static String getStateParam(String name, jakarta.servlet.http.HttpServletRequest req)
      Reads a cookie.
      Parameters:
      name - the name
      req - HTTP request
      Returns:
      the cookie value
    • removeStateParam

      public static void removeStateParam(String name, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res)
      Deletes a cookie.
      Parameters:
      name - the name
      req - HTTP request
      res - HTTP response
    • setRawCookie

      public static void setRawCookie(String name, String value, jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, boolean httpOnly, int maxAge)
      Sets a cookie.
      Parameters:
      name - the name
      value - the value
      req - HTTP request
      res - HTTP response
      httpOnly - HTTP only flag
      maxAge - max age
    • getCookieValue

      public static String getCookieValue(jakarta.servlet.http.HttpServletRequest req, String name)
      Reads a cookie.
      Parameters:
      req - HTTP request
      name - the name
      Returns:
      the cookie value
    • setAuthCookie

      public static void setAuthCookie(String name, String value, int maxAge, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
      Sets the auth cookie.
      Parameters:
      name - name
      value - value
      maxAge - maxAge
      request - request
      response - response
    • setAuthCookie

      public static void setAuthCookie(String name, String value, boolean httpOnly, int maxAge, String sameSite, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
      Sets the auth cookie.
      Parameters:
      name - name
      value - value
      httpOnly - HTTP only flag
      maxAge - cookie validity in seconds
      sameSite - SameSite value
      request - request
      response - response
    • isValidCaptchaResponse

      public static boolean isValidCaptchaResponse(String captchaSecretKey, jakarta.servlet.http.HttpServletRequest request)
      Captcha validation method for reCAPTCHA v3, Turnstile and hCaptcha.
      Parameters:
      captchaSecretKey - serverside secret key
      request - HTTP request
      Returns:
      true if validation was successful
    • isValidCaptchaResponse

      public static boolean isValidCaptchaResponse(String captchaParamKey, String captchaParamValue, String captchaSecretKey)
      Captcha validation method for reCAPTCHA v3, Turnstile and hCaptcha.
      Parameters:
      captchaParamKey - param key, i.e. g_captcha_response
      captchaParamValue - param value (response)
      captchaSecretKey - serverside secret key
      Returns:
      true if validation was successful
    • sendEmail

      public static org.springframework.http.HttpStatus sendEmail(HttpUtils.MultipartForm formData, com.erudika.para.core.App app, com.erudika.para.core.Form form) throws IOException
      Sends emails with rate limiting enabled.
      Parameters:
      formData - form data
      app - app
      form - form object
      Returns:
      OK if email was sent
      Throws:
      IOException - exception