Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3600502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:30:12+00:00 2026-05-18T20:30:12+00:00

My save method in controller: def save = { def userInstance = new User(params)

  • 0

My save method in controller:

def save = {

        def userInstance = new User(params)
        boolean captcha = jcaptchaService.validateResponse("image", session.id, params.response)

        if ( (captcha) && (userInstance.save(flush: true))) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])}"
            redirect(url:'http://localhost:8080/TrafficManFinal/index.gsp?page=registThx')
        }
        else {
            if (captcha == false)
                flash.message2 = "Wrong Captcha!"
            render(view: "create", model: [userInstance: userInstance])
        }
    }

My User/create.gsp view

     <tr class="prop">
                                    <td valign="top" class="name">
                                        <label for="secretAnswer"><g:message code="user.secretAnswer.label" default="Secret Answer" /></label>
                                    </td>
                                    <td valign="top" class="value ${hasErrors(bean: userInstance, field: 'secretAnswer', 'errors')}">
                                        <g:textField name="secretAnswer" value="${userInstance?.secretAnswer}" />

                                        <g:form name="challenge" action="image">
                                          <jcaptcha:jpeg name="image" /><br>
                                          <br>
                                          <g:textField name="response" value="" /><br>
                                          <br>
                                        </g:form>
                                    </td>
                                </tr

>

The Service:

import com.octo.captcha.service.CaptchaService;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

/**
 * Provides access to the captchas as well as provides some util
 * type methods to convert captchas to usable data.
 * 
 * @author LD <ld@ldaley.com>
 */
class JcaptchaService 
{
 /**
  * Used to access the captchas defined as part of the app config.
  */
 def grailsApplication

 /**
  * Retrieves a captcha by name.
  * 
  * @param captchaName The 'key' of the captcha defined in config.
  * @throws IllegalArgumentException If captchaName is null.
  * @throws IllegalStateException If there is no captcha by that name.
  * @returns The captcha service keyed by 'captchaName'
  */
 CaptchaService getCaptchaService(String captchaName) {

  if (captchaName == null) throw IllegalArgumentException("'captchaName' cannot be null")
  def c = grailsApplication.config.jcaptchas[captchaName]
  if (c == null) throw new IllegalStateException("There is no jcaptcha defined with name '${captchaName}'")
  c
 }

 /**
  * Used to verify the response to a challenge.
  * 
  * @param captchaName The key of the captcha
  * @param id The identifier used when retrieving the challenge (often session.id)
  * @param response What the user 'entered' to meet the challenge
  * @return True if the response meets the challenge
  * @see getCaptchaService()
  */
 boolean validateResponse(captchaName, id, response)
 {
  def c = getCaptchaService(captchaName)
                c.validateResponseForID(id, response)
 }

 /**
  * Utility routine to turn an image challenge into a JPEG stream.
  * 
  * @param challenge The image data
  * @return A raw bunch of bytes which come together to be a JPEG.
  */
 byte[] challengeAsJpeg(BufferedImage challenge)
 {
  def jpegOutputStream = new ByteArrayOutputStream()
  def jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream)
  jpegEncoder.encode(challenge)
  jpegOutputStream.toByteArray()
 } 

 /**
  * Utility routine to turn a sound challenge into a WAV stream.
  * 
  * @param challenge The sound data
  * @return A raw bunch of bytes which come together to be a WAV.
  */ 
 byte[] challengeAsWav(AudioInputStream challenge)
 {
  ByteArrayOutputStream soundOutputStream = new ByteArrayOutputStream()
  AudioSystem.write(challenge, AudioFileFormat.Type.WAVE, soundOutputStream)
  soundOutputStream.flush()
  soundOutputStream.close()
  soundOutputStream.toByteArray()
 }
}

The way it is implemented, only if i introduce the right captch once, i can see the flush.messages comming from the constraints. i want a way to merge the two flush messages or display the two deferent flushes at same time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:30:12+00:00Added an answer on May 18, 2026 at 8:30 pm

    Now you are on the right track. 😉 (from reading comments)

    typically controllers calls services, and services operate on domain models.

    In this case, if your case is typical, captchas are used to verify that a person entered the data, not some sort of program.

    So this is in the realm of controllers. In your controller action you would use the CaptchaService to validate the the user entered value. If it is wrong you probably rerender the captcha page. If it is correct, you proceed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My save method in controller: def save = { def userInstance = new User(params)
I have the following methods in my controller: def create @user = User.new(params[:user]) if
Here is the create in rfq controller: def create if has_create_right? @rfq = Rfq.new(params[:rfq],
I have the following code in my controller def create @severity = Severity.new(params[:severity]) if
I have a method in controller - def undo_link view_context.link_to(undo, revert_version_path(@page.versions.scoped.last), :method => :post)
I have a create action like so in a rails controller: def create @user
I have the following in my controller: def create @board = Board.find(session[:board]) @greeting =
For example, I have something like this in my controller: def save ... render
Can the #save method be used to update a record? I know that I
We have repositories which have a Save method. They also throw a Created event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.