[jboss-user] [JBoss Seam] - Re: Seam Captcha configuration
wise_guybg
do-not-reply at jboss.com
Wed Aug 15 06:22:56 EDT 2007
While waiting for the team to provide a way to configure captcha I have elaborated a tweak that I want make sure is correct. I have subclassed CaptchaImage like so:
| @Startup
| @Scope(APPLICATION)
| @Name("mycaptchaimage")
| @Intercept(NEVER)
| @Install(precedence = BUILT_IN,
| classDependencies = "com.octo.captcha.service.image.ImageCaptchaService")
| public class MyCaptchaImage extends CaptchaImage {
|
| private ImageCaptchaService service;
|
| public static MyCaptchaImage instance() {
| if (!Contexts.isApplicationContextActive()) {
| throw new IllegalStateException("No application context active");
| }
| return (MyCaptchaImage) Contexts.getApplicationContext().get(MyCaptchaImage.class);
| }
|
| @Override
| public void create() {
| service = new DefaultManageableImageCaptchaService(
| new FastHashMapCaptchaStore(),
| new MyImageCaptchaEngine(),
| 180,
| 100000,
| 75000);
| }
|
| @Override
| protected String getResourcePath() {
| return "/mycaptcha";
| }
|
| @Override
| public boolean validateResponse(String id, String response) {
| try {
| return service.validateResponseForID(id, response);
| }
| catch (CaptchaServiceException cse) {
| return false;
| }
| }
|
| @Override
| public void getResource(HttpServletRequest request, HttpServletResponse response) throws IOException {
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
| try {
| Lifecycle.beginRequest(getServletContext(), request.getSession(), request);
|
| String captchaId = request.getQueryString();
|
| BufferedImage challenge = service.getImageChallengeForID(captchaId, request.getLocale());
|
| ImageIO.write(challenge, "jpeg", out);
| }
| catch (IllegalArgumentException e) {
| response.sendError(HttpServletResponse.SC_NOT_FOUND);
| return;
| }
| catch (CaptchaServiceException e) {
| response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
| return;
| }
| finally {
| Lifecycle.endRequest();
| }
|
| response.setHeader("Cache-Control", "no-store");
| response.setHeader("Pragma", "no-cache");
| response.setDateHeader("Expires", 0);
| response.setContentType("image/jpeg");
| response.getOutputStream().write(out.toByteArray());
| response.getOutputStream().flush();
| response.getOutputStream().close();
| }
|
| }
|
The only change in the usage is that now the resource path is /mycaptcha. I have tested it and is seems to work. Is it OK to subclass the component like this?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074351#4074351
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074351
More information about the jboss-user
mailing list