Author: emuckenhuber
Date: 2007-08-21 13:08:08 -0400 (Tue, 21 Aug 2007)
New Revision: 8018
Added:
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/ImageCaptchaServlet.java
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/JCaptchaService.java
Log:
jcaptcha services commit
Added:
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/ImageCaptchaServlet.java
===================================================================
---
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/ImageCaptchaServlet.java
(rev 0)
+++
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/ImageCaptchaServlet.java 2007-08-21
17:08:08 UTC (rev 8018)
@@ -0,0 +1,95 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.core.identity.services.captcha;
+
+import com.octo.captcha.service.CaptchaServiceException;
+import com.sun.image.codec.jpeg.JPEGCodec;
+import com.sun.image.codec.jpeg.JPEGImageEncoder;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+
+
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:emuckenh@redhat.com">Emanuel
Muckenhuber</a>
+ * @version $Revision$
+ */
+public class ImageCaptchaServlet extends HttpServlet {
+
+
+ public void init(ServletConfig servletConfig) throws ServletException {
+
+ super.init(servletConfig);
+
+ }
+
+ protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse) throws ServletException, IOException {
+
+ byte[] captchaChallengeAsJpeg = null;
+ // the output stream to render the captcha image as jpeg into
+ ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
+ try {
+ // get the session id that will identify the generated captcha.
+ //the same id must be used to validate the response, the session id is a good
candidate!
+ String captchaId = httpServletRequest.getSession().getId();
+ // call the ImageCaptchaService getChallenge method
+ BufferedImage challenge =
+ JCaptchaService.getInstance().getImageChallengeForID(captchaId,
+ httpServletRequest.getLocale());
+
+ // a jpeg encoder
+ JPEGImageEncoder jpegEncoder =
+ JPEGCodec.createJPEGEncoder(jpegOutputStream);
+ jpegEncoder.encode(challenge);
+ } catch (IllegalArgumentException e) {
+ httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ } catch (CaptchaServiceException e) {
+ httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
+
+ // flush it in the response
+ httpServletResponse.setHeader("Cache-Control", "no-store");
+ httpServletResponse.setHeader("Pragma", "no-cache");
+ httpServletResponse.setDateHeader("Expires", 0);
+ httpServletResponse.setContentType("image/jpeg");
+ ServletOutputStream responseOutputStream =
+ httpServletResponse.getOutputStream();
+ responseOutputStream.write(captchaChallengeAsJpeg);
+ responseOutputStream.flush();
+ responseOutputStream.close();
+ }
+}
\ No newline at end of file
Added:
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/JCaptchaService.java
===================================================================
---
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/JCaptchaService.java
(rev 0)
+++
trunk/core-identity/src/main/org/jboss/portal/core/identity/services/captcha/JCaptchaService.java 2007-08-21
17:08:08 UTC (rev 8018)
@@ -0,0 +1,51 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.core.identity.services.captcha;
+
+import java.awt.image.BufferedImage;
+
+import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
+import com.octo.captcha.service.image.ImageCaptchaService;
+
+/**
+ * @author <a href="mailto:emuckenh@redhat.com">Emanuel
Muckenhuber</a>
+ * @version $Revision$
+ */
+public class JCaptchaService
+{
+
+ private static ImageCaptchaService instance = new
DefaultManageableImageCaptchaService();
+
+ public static ImageCaptchaService getInstance()
+ {
+ return instance;
+ }
+
+ public BufferedImage challenge(String challengeId)
+ {
+ return instance.getImageChallengeForID(challengeId);
+ }
+
+
+}
+