tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject git commit: TAP5-1934: Kaptcha Component throws NPE after session is expired
Date Mon, 01 Sep 2014 23:44:51 GMT
Repository: tapestry-5
Updated Branches:
  refs/heads/master df93764ad -> 268f06c89


TAP5-1934: Kaptcha Component throws NPE after session is expired


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/268f06c8
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/268f06c8
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/268f06c8

Branch: refs/heads/master
Commit: 268f06c89ac02f4a4f771ced23699f3de74edfd0
Parents: df93764
Author: Howard M. Lewis Ship <hlship@apache.org>
Authored: Mon Sep 1 16:45:01 2014 -0700
Committer: Howard M. Lewis Ship <hlship@apache.org>
Committed: Mon Sep 1 16:45:01 2014 -0700

----------------------------------------------------------------------
 .../kaptcha/components/KaptchaImage.java        | 64 ++++++++++++++------
 1 file changed, 44 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/268f06c8/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/components/KaptchaImage.java
----------------------------------------------------------------------
diff --git a/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/components/KaptchaImage.java
b/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/components/KaptchaImage.java
index cca4bfb..973a801 100644
--- a/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/components/KaptchaImage.java
+++ b/tapestry-kaptcha/src/main/java/org/apache/tapestry5/kaptcha/components/KaptchaImage.java
@@ -1,5 +1,3 @@
-// Copyright 2011, 2012 The Apache Software Foundation
-//
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
@@ -17,16 +15,21 @@ package org.apache.tapestry5.kaptcha.components;
 import org.apache.tapestry5.ComponentResources;
 import org.apache.tapestry5.Link;
 import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.StreamResponse;
 import org.apache.tapestry5.annotations.Persist;
 import org.apache.tapestry5.annotations.SupportsInformalParameters;
 import org.apache.tapestry5.ioc.annotations.Inject;
 import org.apache.tapestry5.kaptcha.services.KaptchaProducer;
+import org.apache.tapestry5.services.HttpError;
 import org.apache.tapestry5.services.Response;
 
 import javax.imageio.ImageIO;
+import javax.servlet.http.HttpServletResponse;
 import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
+import java.io.InputStream;
 
 /**
  * Part of a Captcha based authentication scheme; a KaptchaImage generates a new
@@ -84,24 +87,45 @@ public class KaptchaImage
         return false;
     }
 
-    void onImage() throws IOException
+    Object onImage() throws IOException
     {
-        BufferedImage image = producer.createImage(captchaText);
-
-        response.setDateHeader("Expires", 0);
-        // Set standard HTTP/1.1 no-cache headers.
-        response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
-        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
-        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
-        // Set standard HTTP/1.0 no-cache header.
-        response.setHeader("Pragma", "no-cache");
-
-        OutputStream stream = response.getOutputStream("image/jpeg");
-
-        ImageIO.write(image, "jpg", stream);
-
-        stream.flush();
+        if (captchaText == null)
+        {
+            return new HttpError(HttpServletResponse.SC_NOT_FOUND, "Session expired.");
+        }
+
+        return new StreamResponse()
+        {
+            @Override
+            public String getContentType()
+            {
+                return "image/jpeg";
+            }
+
+            @Override
+            public InputStream getStream() throws IOException
+            {
+                BufferedImage image = producer.createImage(captchaText);
+
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+                ImageIO.write(image, "jpg", baos);
+
+                return new ByteArrayInputStream(baos.toByteArray());
+            }
+
+            @Override
+            public void prepareResponse(Response response)
+            {
+                response.setDateHeader("Expires", 0);
+                // Set standard HTTP/1.1 no-cache headers.
+                response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
+                // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
+                response.addHeader("Cache-Control", "post-check=0, pre-check=0");
+                // Set standard HTTP/1.0 no-cache header.
+                response.setHeader("Pragma", "no-cache");
+            }
+        };
 
-        stream.close();
     }
 }


Mime
View raw message