Search in sources :

Example 1 with StreamResponse

use of org.apache.tapestry5.StreamResponse in project tapestry-5 by apache.

the class KaptchaImage method onImage.

Object onImage() throws IOException {
    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");
        }
    };
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.apache.tapestry5.http.services.Response) StreamResponse(org.apache.tapestry5.StreamResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamResponse(org.apache.tapestry5.StreamResponse) HttpError(org.apache.tapestry5.services.HttpError) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 StreamResponse (org.apache.tapestry5.StreamResponse)1 Response (org.apache.tapestry5.http.services.Response)1 HttpError (org.apache.tapestry5.services.HttpError)1