From: siva k. <k_s...@ya...> - 2007-06-05 08:20:08
|
Hi , I am sorry for late reply.I was on vacation and in between i was sick too. I tried to implement soundcaptcha.Its working fine. But the problem is that i am hearing more than one word/number.I would like to get only one word. For that i tried to change the rate value of FreeTTSSoundConfigurator at Engine.Still i am facing the same problem Please find the code below. 1.SimpleSoundCaptchaServlet 2.MySoundCaptchaService 3.CKSpellerSoundCaptchaEngine 4.CKSimpleListSoundCaptchaEngine When i use predefined engines like SimpleListSoundCaptchaEngine,SpellerSoundCaptchaEngine.MySoundCaptchaService(line no 17 instead of CKSimpleListSoundCaptchaEngine) it works.But it gives more than one word/number. I would like to get only one word/number. For that i copied SpellerSoundCaptchaEngine and SimpleListSoundCaptchaEngine and saved as CKSpellerSoundCaptchaEngine and CKSimpleListSoundCaptchaEngine.Changed the value of rate(number of words per minute). It compiles.But when i try to acces CKSimpleListSoundCaptchaEngine or CKSpellerSoundCaptchaEngine,it wont display wav file. I would like to hear only one word/number. Can you please tell me how can i do that. The other problem is when access my server from any other client. It displays the wav file .But the sound is played at server.I hope the problem is at servlet code.Can you please tell me what is the wrong in that servlet. My Files are:: =================================================== package org.sound.captcha; import com.octo.captcha.service.CaptchaServiceException; import com.octo.captcha.sound.SoundCaptcha; import javax.servlet.*; import javax.servlet.http.*; import java.awt.image.BufferedImage; import java.io.File; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.FloatControl; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Map; import javax.imageio.ImageIO; public class SimpleSoundCaptchaServlet extends HttpServlet { protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; int nBytesRead = 0; int nBytesWritten = 0; if ( request.getQueryString()!=null ) { response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "GET request should have no query string." ); return; } try { String captchaId = request.getSession().getId(); AudioInputStream thechallenge = MySoundCaptchaService.getInstance().getService().getSoundChallengeForID(captchaId,request.getLocale()); AudioFormat audioFormat = thechallenge.getFormat(); SourceDataLine line = null; try { line = (SourceDataLine) AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, audioFormat)); line.open(audioFormat); line.start(); } catch (LineUnavailableException e) { e.printStackTrace(); } while (nBytesRead != -1) { try { nBytesRead = thechallenge.read(abData, 0, abData.length); System.out.println( "while::nBytesRead::" +nBytesRead); } catch (IOException e) { e.printStackTrace(); } if (nBytesRead >= 0) { nBytesWritten = line.write(abData, 0, nBytesRead); } } // Clear any existing flag. request.getSession().removeAttribute( "PassedCaptcha" ); } catch( CaptchaServiceException cse ) { System.out.println( "CaptchaServiceException - " + cse.getMessage() ); response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Problem generating captcha image." ); return; } // Set appropriate http headers. response.setHeader( "Cache-Control", "no-store" ); response.setHeader( "Pragma", "no-cache" ); response.setDateHeader( "Expires", 0 ); response.setContentType("audio/x-wav"); // Write the wav to the client. ServletOutputStream outStream = response.getOutputStream(); outStream.write(abData); outStream.flush(); outStream.close(); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // doGet(request, response); Map paramMap = request.getParameterMap(); if ( paramMap.isEmpty() ) { System.out.println( "[CKCaptch] ImageCaptchaServlet :: Post method not allowed without parameters."); return; } String[] arr1 = (String[])paramMap.get( "hidCaptchaID" ); String[] arr2 = (String[])paramMap.get( "inCaptchaChars" ); if ( arr1==null || arr2==null ) { System.out.println( "[CKCaptch] ImageCaptchaServlet :: Expected parameters were not found."); return; } String sessId = request.getSession().getId(); String incomingCaptchaId = arr1.length>0 ? arr1[0] : ""; String inputChars = arr2.length>0 ? arr2[0] : ""; // Check validity and consistency of the data. if ( sessId==null || incomingCaptchaId==null || !sessId.equals(incomingCaptchaId) ) { System.out.println( "[CKCaptch] ImageCaptchaServlet :: Browser must support session cookies."); return; } // Validate whether input from user is correct. boolean passedCaptchaTest = validateCaptcha( incomingCaptchaId, inputChars ); // Set flag into session. request.getSession().setAttribute( "PassedCaptcha", new Boolean(passedCaptchaTest) ); // Forward request to results page. RequestDispatcher rd = getServletContext().getRequestDispatcher( "/results.jsp" ); rd.forward( request, response ); } private boolean validateCaptcha( String captchaId, String inputChars ) { try { return MySoundCaptchaService.getInstance().getService().validateResponseForID( captchaId, inputChars ); } catch( CaptchaServiceException cse ) { System.err.println( "[CKCaptch] ImageCaptchaServlet :: validateCaptcha :: CaptchaServiceException :: " +cse.getMessage()); } return false; } } =================================================== package org.sound.captcha; import com.octo.captcha.service.sound.SoundCaptchaService; import com.octo.captcha.service.sound.DefaultManageableSoundCaptchaService; import com.octo.captcha.engine.sound.gimpy.SimpleListSoundCaptchaEngine; import com.octo.captcha.engine.sound.speller.SpellerSoundCaptchaEngine; import org.sound.captcha.CKSimpleListSoundCaptchaEngine; import org.sound.captcha.CKSpellerSoundCaptchaEngine; public class MySoundCaptchaService { private SoundCaptchaService service; private static MySoundCaptchaService instance = new MySoundCaptchaService(); private MySoundCaptchaService() { DefaultManageableSoundCaptchaService serv = new DefaultManageableSoundCaptchaService(); serv.setCaptchaEngineClass(CKSimpleListSoundCaptchaEngine.class.getName()); service = serv; } public static MySoundCaptchaService getInstance() { return instance; } public SoundCaptchaService getService() { return service; } } =================================================== package org.sound.captcha; import com.octo.captcha.component.sound.soundconfigurator.FreeTTSSoundConfigurator; import com.octo.captcha.component.sound.soundconfigurator.SoundConfigurator; import com.octo.captcha.component.sound.wordtosound.FreeTTSWordToSound; import com.octo.captcha.engine.sound.ListSoundCaptchaEngine; import com.octo.captcha.sound.gimpy.GimpySoundFactory; /** * <p>Description: simple gimpy sound engine </p> * * @author Benoit Doumas * @version 1.0 */ public class CKSimpleListSoundCaptchaEngine extends ListSoundCaptchaEngine { protected void buildInitialFactories() { com.octo.captcha.component.word.wordgenerator.WordGenerator words = new com.octo.captcha.component.word.wordgenerator.DictionaryWordGenerator( new com.octo.captcha.component.word.FileDictionary("toddlist")); SoundConfigurator configurator = new FreeTTSSoundConfigurator("kevin16", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory",1.0f,100,1); FreeTTSWordToSound wordToSound = new FreeTTSWordToSound(configurator, 4, 6); this.addFactory(new GimpySoundFactory(words, wordToSound)); } } =================================================== /* * JCaptcha, the open source java framework for captcha definition and integration * Copyright (c) 2007 jcaptcha.net. All Rights Reserved. * See the LICENSE.txt file distributed with this package. */ /* * jcaptcha, the open source java framework for captcha definition and integration * copyright (c) 2007 jcaptcha.net. All Rights Reserved. * See the LICENSE.txt file distributed with this package. */ /* * jcaptcha, the open source java framework for captcha definition and integration * copyright (c) 2007 jcaptcha.net. All Rights Reserved. * See the LICENSE.txt file distributed with this package. */ package org.sound.captcha; import com.octo.captcha.component.sound.soundconfigurator.FreeTTSSoundConfigurator; import com.octo.captcha.component.sound.soundconfigurator.SoundConfigurator; import com.octo.captcha.component.sound.wordtosound.FreeTTSWordToSound; import com.octo.captcha.component.word.worddecorator.SpellerWordDecorator; import com.octo.captcha.engine.sound.ListSoundCaptchaEngine; import com.octo.captcha.sound.speller.SpellerSoundFactory; /** * <p/> * Engine to generate a SpellerSound captcha. This captcha provide a sound that is the spelling of a word </p> * * @author Benoit Doumas * @version 1.0 */ public class CKSpellerSoundCaptchaEngine extends ListSoundCaptchaEngine { /** * @see com.octo.captcha.engine.sound.ListSoundCaptchaEngine#buildInitialFactories() */ protected void buildInitialFactories() { com.octo.captcha.component.word.wordgenerator.WordGenerator words = new com.octo.captcha.component.word.wordgenerator.RandomWordGenerator("0123456789"); SoundConfigurator configurator = new FreeTTSSoundConfigurator("kevin16", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory", 1.0f, 100, 1); FreeTTSWordToSound wordToSound = new FreeTTSWordToSound(configurator, 3, 5); SpellerWordDecorator decorator = new SpellerWordDecorator(", "); this.addFactory(new SpellerSoundFactory(words, wordToSound, decorator)); } } =================================================== Thanks & Regards, Sivamma. ____________________________________________________________________________________ Choose the right car based on your needs. Check out Yahoo! Autos new Car Finder tool. http://autos.yahoo.com/carfinder/ |