Using Captcha as a separate servlet is very un-Tapestry like.  The way
to do it in Tapestry is to setup a Hivemind service. My app uses it
like this (hivemodule.xml):

        <service-point id="CaptchaService"
        interface="org.apache.tapestry.engine.IEngineService">
                <invoke-factory>
                        <construct 
class="org.opendating.tapestry.services.CaptchaService">
                                <set-object
                                property="linkFactory"
                                value="service:tapestry.url.LinkFactory"/>
                        </construct>
                </invoke-factory>
        </service-point>
        
        <contribution configuration-id="tapestry.services.ApplicationServices">
                <service name="captcha" object="service:CaptchaService"/>
        </contribution>

You can then inject the service into the Captcha component you can
build.  I posted links to the opensource app which uses this stuff.
Have a look at several interesting classes which do it 100% within
Tapestry, no external servlet.

Here are some:  CaptchaService, OpenDatingCaptchaEngine, CaptchaComp,
RegistrationCaptchaPage.

The code is out there in the open. You could easily take pieces you
need and fit it right into your project.

Adam

On 17 Apr 2006 18:57:58 -0000, kelson tran
<[EMAIL PROTECTED]> wrote:
> I've been reading this tread, and I found it very useful.  I'm still new
> to Tapestry.  How do you use the servlet in your page?  What is needed
> to set it up?
>
> Thx in advance
>
>
> On Sunday, April 16, 2006, at 7:50 PM, Konstantin Ignatyev wrote:
> >--0-1251190441-1145242250=:93503
> >Content-Type: text/plain; charset=iso-8859-1
> >Content-Transfer-Encoding: 8bit
> >
> >Just did the following and it seems to work beautifully
> >
> >
> >
> >public class JCaptchaServlet extends HttpServlet {
> >
> >
> >  protected void doGet(HttpServletRequest request,
> >HttpServletResponse response) throws ServletException, IOException {
> >
> >    String captchaID = request.getSession(true).getId();
> >
> >    // call the ManageableImageCaptchaService methods
> >    byte[] captchaChallengeAsJpeg = null;
> >    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
> >    try {
> >      ImageCaptchaService srv = App.getCaptchaService();
> >      BufferedImage challenge =
> >srv.getImageChallengeForID(captchaID, request.getLocale());
> >      Object challengeString = srv.getQuestionForID(captchaID,
> >request.getLocale());
> >      // the output stream to render the captcha image as jpeg into
> >
> >      // a jpeg encoder
> >      JPEGImageEncoder jpegEncoder =
> >JPEGCodec.createJPEGEncoder(jpegOutputStream);
> >      jpegEncoder.encode(challenge);
> >    } catch (IllegalArgumentException e) {
> >      // log a security warning and return a 404...
> >//            if (log.isWarnEnabled())
> >//            {
> >//                log.warn(
> >//                    "There was a try from "
> >//                        + request.getRemoteAddr()
> >//                        + " to render an URL without ID"
> >//                        + " or with a too long one");
> >//                theResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
> >      return;
> >//            }
> >    } catch (CaptchaServiceException e) {
> >      // log and return a 404 instead of an image...
> >//            log.warn(
> >//                "Error trying to generate a captcha and "
> >//                    + "render its challenge as JPEG",
> >//                e);
> >      response.sendError(HttpServletResponse.SC_NOT_FOUND);
> >      return;
> >    }
> >
> >    captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
> >
> >    // render the captcha challenge as a JPEG image in the response
> >    response.setHeader("Cache-Control", "no-store");
> >    response.setHeader("Pragma", "no-cache");
> >    response.setDateHeader("Expires", 0);
> >
> >    response.setContentType("image/jpeg");
> >    ServletOutputStream responseOutputStream =
> >        response.getOutputStream();
> >    responseOutputStream.write(captchaChallengeAsJpeg);
> >  }
> >
> >
> >}
> >
> >------------
> >private static ImageCaptchaService captchaService;
> >  private static Object captchaGuard = new Object();
> >
> >  public static ImageCaptchaService getCaptchaService() {
> >    synchronized (captchaGuard) {
> >      if (captchaService == null) {
> >        captchaService = new EhcacheManageableImageCaptchaService(
> >new SimpleListImageCaptchaEngine() , 180, 100000){
> >
> >        };
> >      }
> >      return captchaService;
> >    }
> >  }
> >
> >----------------------
> >
> >
> >
> >Validation
> >
> >@InjectObject( "service:tapestry.globals.HttpServletRequest")
> >  public abstract HttpServletRequest getHttpRequest();
> >
> >............
> >if( App.getCaptchaService().validateResponseForID(
> >getHttpRequest().getSession(true).getId(), getCaptchaAnswer() )){
> >        System.out.println("CAPTCHA - Pass");
> >      }else{
> >
> >
> >
> >Oscar Picasso <[EMAIL PROTECTED]> wrote: Do you have a code
> >example of such an ImageCaptchaServlet?
> >
> >Daniel Lydiard  wrote: I'm using Jcaptcha with my tapestry application.
> >
> >1. Load the ImageCaptchaServlet in your web.xml.
> >
> >
> >jcaptcha
> >
> >com.foo.ImageCaptchaServlet
> >
> >1
> >
> >
> >
> >
> >
> >
> >jcaptcha
> >
> >/jcaptcha
> >
> >
> >
> >
> >2. Do something like this in a component class method to get the captcha
> >question.
> >captchaId = getPage().getRequest().getSession(true).getId();
> >return CaptchaServiceSingleton.getInstance().getQuestionForID(captchaId);
> >
> >3. Point to the servlet for the captchaimage in your component .
> >
> >4.  To validate the user response
> >captchaId = getRequest().getSession(true).getId();
> >
> >return
> >CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,
> >userResponse);
> >
> >
> >Hope this helps.
> >
> >
> >----- Original Message -----
> >From: "Oscar Picasso"
> >To: "Tapestry users"
> >Sent: Sunday, April 16, 2006 11:47 AM
> >Subject: Re: Looking for a CATCHPA
> >
> >
> >>I have taken a look. But I have no idea where to start. The api
> >>javadoc has
> >>only one class:  JCaptchaCommentAuthenticator. It seems there is a
> >>problem
> >>with the site.
> >>
> >> Have you worked with JCaptcha? And do you know where to start?
> >>
> >> Oscar
> >>
> >>
> >>
> >> J�r�me BERNARD  wrote: Have a loot at
> >> http://jcaptcha.sourceforge.net/
> >>
> >> Regards,
> >> J�r�me.
> >>
> >> On 4/16/06, Oscar Picasso  wrote:
> >>> Hi,
> >>>
> >>> I'm looking for a CATCHPA library to use with my Tapestry application.
> >>> I'm looking for something easy to use and efficient.
> >>>
> >>> I welcome any advice.
> >>>
> >>> Oscar
> >>>
> >>>
> >>> ---------------------------------
> >>> Blab-away for as little as 1�/min. Make  PC-to-Phone Calls
> >>>using Yahoo!
> >>> Messenger with Voice.
> >>>
> >>
> >>
> >> --
> >> J�r�me BERNARD,
> >> Kalixia, SARL.
> >> http://weblog.kalixia.com
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [EMAIL PROTECTED]
> >> For additional commands, e-mail: [EMAIL PROTECTED]
> >>
> >>
> >>
> >>
> >> ---------------------------------
> >> Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously
> >>low rates.
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >
> >__________________________________________________
> >Do You Yahoo!?
> >Tired of spam?  Yahoo! Mail has the best spam protection around
> >http://mail.yahoo.com
> >
> >
> >Konstantin Ignatyev
> >
> >
> >
> >
> >PS: If this is a typical day on planet earth, humans will add
> >fifteen million tons of carbon to the atmosphere, destroy 115 square
> >miles of tropical rainforest, create seventy-two miles of desert,
> >eliminate between forty to one hundred species, erode seventy-one
> >million tons of topsoil, add 2,700 tons of CFCs to the stratosphere,
> >and increase their population by 263,000
> >
> >Bowers, C.A.  The Culture of Denial:  Why the Environmental Movement
> >Needs a Strategy for Reforming Universities and Public Schools.  New
> >York:  State University of New York Press, 1997: (4) (5) (p.206)
> >--0-1251190441-1145242250=:93503--
>
>
>
>
> --
> Posted with http://DevLists.com.  Sign up and save your mailbox.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

Reply via email to