Class CaptchaServiceBean


  • public class CaptchaServiceBean
    extends Object
    This class provides services related to CAPTCHA challenges and validation. It includes method delegates for generating challenges, validating challenge responses, and managing CAPTCHA challenges for specific hosts.
    Version:
    1.0
    Author:
    Ivan Mrsulja
    • Constructor Detail

      • CaptchaServiceBean

        public CaptchaServiceBean()
    • Method Detail

      • generateRefreshedChallenge

        public static CaptchaBundle generateRefreshedChallenge()
                                                        throws IOException
        Generates a new CAPTCHA challenge (returns empty CaptchaBundle for 3rd party providers).
        Returns:
        A CaptchaBundle containing the CAPTCHA image in Base64 format, the content, and a unique identifier.
        Throws:
        IOException - If an error occurs during image conversion.
      • getChallenge

        public static Optional<CaptchaBundle> getChallenge​(String captchaId)
        Retrieves a CAPTCHA challenge for a specific host based on the provided CAPTCHA ID Removes the challenge from the storage after retrieval.
        Parameters:
        captchaId - The CAPTCHA ID to match.
        Returns:
        An Optional containing the CaptchaBundle if a matching challenge is found, or an empty Optional otherwise.
      • getCaptchaChallenges

        public static com.google.common.cache.Cache<String,​CaptchaBundle> getCaptchaChallenges()
        Gets the map containing CAPTCHA challenges for different hosts.
        Returns:
        A ConcurrentHashMap with host addresses as keys and CaptchaBundle objects as values.
      • getCaptchaImpl

        public static CaptchaImplementation getCaptchaImpl()
        Retrieves the configured captcha implementation based on the application's configuration properties. If captcha functionality is disabled, returns NONE. If the captcha implementation is not specified, defaults to NANOCAPTCHA.
        Returns:
        The selected captcha implementation (NANOCAPTCHA, RECAPTCHAv2, or NONE).
      • addCaptchaRelatedFieldsToPageContext

        public static void addCaptchaRelatedFieldsToPageContext​(Map<String,​Object> context)
                                                         throws IOException
        Adds captcha-related fields to the given page context map. The specific fields added depend on the configured captcha implementation.
        Parameters:
        context - The page context map to which captcha-related fields are added.
        Throws:
        IOException - If there is an IO error during captcha challenge generation.
      • validateCaptcha

        public static boolean validateCaptcha​(String captchaInput,
                                              String challengeId)
        Validates a user's captcha input.
        Parameters:
        captchaInput - The user's input for the captcha challenge.
        challengeId - The unique identifier for the challenge (if captcha is 3rd party, this param is ignored).
        Returns:
        true if the captcha input is valid, false otherwise.