Clover coverage report - Maven Clover report
Coverage timestamp: Wed Sep 2 2009 20:38:15 GMT
file stats: LOC: 85   Methods: 2
NCLOC: 57   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SimpleSoundCaptchaServlet.java 0% 0% 0% 0%
coverage
 1    /*
 2    * JCaptcha, the open source java framework for captcha definition and integration
 3    * Copyright (c) 2007 jcaptcha.net. All Rights Reserved.
 4    * See the LICENSE.txt file distributed with this package.
 5    */
 6   
 7    package com.octo.captcha.module.servlet;
 8   
 9    import java.io.ByteArrayOutputStream;
 10    import java.io.IOException;
 11   
 12    import javax.servlet.Servlet;
 13    import javax.servlet.ServletException;
 14    import javax.servlet.ServletOutputStream;
 15    import javax.servlet.http.HttpServlet;
 16    import javax.servlet.http.HttpServletRequest;
 17    import javax.servlet.http.HttpServletResponse;
 18    import javax.sound.sampled.AudioInputStream;
 19    import javax.sound.sampled.AudioSystem;
 20   
 21    import com.octo.captcha.service.CaptchaServiceException;
 22    import com.octo.captcha.service.sound.DefaultManageableSoundCaptchaService;
 23    import com.octo.captcha.service.sound.SoundCaptchaService;
 24   
 25    @SuppressWarnings("serial")
 26    public class SimpleSoundCaptchaServlet extends HttpServlet implements Servlet {
 27   
 28    public static SoundCaptchaService service = new DefaultManageableSoundCaptchaService();
 29   
 30  0 @Override
 31    protected void doGet(HttpServletRequest httpServletRequest,
 32    HttpServletResponse httpServletResponse) throws ServletException,
 33    IOException {
 34    // Set to expire far in the past.
 35  0 httpServletResponse.setDateHeader("Expires", 0);
 36    // Set standard HTTP/1.1 no-cache headers.
 37  0 httpServletResponse.setHeader("Cache-Control",
 38    "no-store, no-cache, must-revalidate");
 39    // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
 40  0 httpServletResponse.addHeader("Cache-Control",
 41    "post-check=0, pre-check=0");
 42    // Set standard HTTP/1.0 no-cache header.
 43  0 httpServletResponse.setHeader("Pragma", "no-cache");
 44   
 45    // return a wav
 46  0 httpServletResponse.setContentType("audio/wav");
 47   
 48  0 AudioInputStream audioInputStream =
 49    service.getSoundChallengeForID(httpServletRequest.
 50    getSession(true).getId());
 51   
 52  0 ServletOutputStream out = httpServletResponse.getOutputStream();
 53   
 54    // write the data out
 55   
 56  0 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
 57  0 AudioSystem
 58    .write(audioInputStream,
 59    javax.sound.sampled.AudioFileFormat.Type.WAVE,
 60    byteOutputStream);
 61   
 62  0 out.write(byteOutputStream.toByteArray());
 63  0 try {
 64  0 out.flush();
 65    } finally {
 66  0 out.close();
 67    }
 68    }
 69   
 70  0 public static boolean validateResponse(HttpServletRequest request,
 71    String userCaptchaResponse) {
 72    // if no session found
 73  0 if (request.getSession(false) == null)
 74  0 return false;
 75    // else use service and session id to validate
 76  0 boolean validated = false;
 77  0 try {
 78  0 validated = service.validateResponseForID(request.getSession()
 79    .getId(), userCaptchaResponse);
 80    } catch (CaptchaServiceException e) {
 81    // do nothing.. false
 82    }
 83  0 return validated;
 84    }
 85    }