You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Lester Ward <lw...@tagaudit.com> on 2003/01/13 22:45:01 UTC

Chokepoint to no-cache pages?

I'm looking to make all of my pages such that the browser never caches them.
I'd like to do this in a single place, so that each page does not have to
include the correct code on its own (too easy to forget a page).

Due to browser and proxy server bugs, embedding meta-tags into the pages
themselves is not reliable. Traditionally, the way you do this is to add the
following code somewhere:

   response.setHeader("Cache-Control", "no-cache");
   response.setHeader("Pragma", "no-cache");
   response.setHeader("Expires", <some time>);

The logical place for this is the location through which all requests come:
the doGet method of the main servlet. Assuming this, the proper thing to do
in Turbine (I think) would be to declare a servlet like so:

public class MyServlet extends org.apache.turbine.Turbine
{
    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException
    {
        res.setHeader("Cache-Control", "no-cache");
        res.setHeader("Pragma", "no-cache");
        res.setHeader("Expires", <some time>);
	  super.doGet(req,res);
    }
}

The problem is that this doesn't work. The Turbine servlet is declared as
final, so cannot be subclassed.

So, how have people handled this problem? Does Turbine have some other way
to keep pages un-cached?

Wordman

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Chokepoint to no-cache pages?

Posted by Scott Eade <se...@backstagetech.com.au>.
Lester,

You can put the code your layout templates thus:

#set ($cache = $data.Parameters.getBoolean("cache", false))
#if (!$cache)
  $myPullTool.setExpiryHeaders()
#end

Where the pull tool myPullTool defines setExpiryHeaders() as:

    public void setExpiryHeaders()
    {
        // HTTP 1.1
        data.getResponse().setHeader("Cache-Control", "no-cache");
        // HTTP 1.0
        data.getResponse().setHeader("Pragma", "no-cache");
        // Prevent caching at the proxy server
        data.getResponse().setDateHeader("Expires", 0);
    }

Note that the processing performed by setExpiryHeaders() can be done
directly in the template (or put it all in a macro if you have a number of
layout templates) - I put it in a pull tool because I was mucking around
with expiry times (which is easier in Java than in VTL).

I have also run into situations where a particular proxy server (M$) ignored
the above settings.  To ensure the pages were not cached it was necessary to
extend TemplatePageLink as follows so that every url that is generated is
unique.  Don't forget to update TR.props to refer to this new class.

package com.backstagetech.turbine.util.template;

import java.util.Date;
import org.apache.turbine.util.template.*;

/**
 * Extension to TurbineLink that provides for unique URLs to force caches to
 * retrieve the page every time.  This is implemented in a very bad way, but
 * then TemplateLink itself is pretty shocking in that it breaks the
 * toString() contract by altering the value of the object.
 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
 */
public class TemplateLinkEx extends TemplateLink
{

//    public TemplateLink setPage(java.lang.String t)
//    {
//        super.setPage(t);
//        super.addPathInfo("nocacheid", new java.util.Date().getTime());
//        return this;
//    }
    private static final String NOCACHE = "nocacheid";

    public String getURI()
    {
        super.addPathInfo(NOCACHE, new java.util.Date().getTime());
        String result = super.getURI();
        super.removePathInfo(NOCACHE);
        return result;
    }

    public String toString()
    {
        super.addPathInfo(NOCACHE, new java.util.Date().getTime());
        return super.toString();
    }

}

HTH,

Scott
-- 
Scott Eade
Backstage Technologies Pty. Ltd.
http://www.backstagetech.com.au
.Mac Chat/AIM: seade at mac dot com


On 14/01/2003 8:45 AM, "Lester Ward" <lw...@tagaudit.com> wrote:

> I'm looking to make all of my pages such that the browser never caches them.
> I'd like to do this in a single place, so that each page does not have to
> include the correct code on its own (too easy to forget a page).
> 
> Due to browser and proxy server bugs, embedding meta-tags into the pages
> themselves is not reliable. Traditionally, the way you do this is to add the
> following code somewhere:
> 
>  response.setHeader("Cache-Control", "no-cache");
>  response.setHeader("Pragma", "no-cache");
>  response.setHeader("Expires", <some time>);
> 
> The logical place for this is the location through which all requests come:
> the doGet method of the main servlet. Assuming this, the proper thing to do
> in Turbine (I think) would be to declare a servlet like so:
> 
> public class MyServlet extends org.apache.turbine.Turbine
> {
>   public void doGet(HttpServletRequest req, HttpServletResponse res)
>       throws IOException, ServletException
>   {
>       res.setHeader("Cache-Control", "no-cache");
>       res.setHeader("Pragma", "no-cache");
>       res.setHeader("Expires", <some time>);
>  super.doGet(req,res);
>   }
> }
> 
> The problem is that this doesn't work. The Turbine servlet is declared as
> final, so cannot be subclassed.
> 
> So, how have people handled this problem? Does Turbine have some other way
> to keep pages un-cached?
> 
> Wordman
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Chokepoint to no-cache pages?

Posted by Gonzalo Diethelm <go...@aditiva.com>.
> Due to browser and proxy server bugs, embedding meta-tags into the pages
> themselves is not reliable. Traditionally, the way you do this is 
> to add the
> following code somewhere:
> 
>    response.setHeader("Cache-Control", "no-cache");
>    response.setHeader("Pragma", "no-cache");
>    response.setHeader("Expires", <some time>);
> 
> The logical place for this is the location through which all 
> requests come:
> the doGet method of the main servlet.

If you are not using the pull model, you can have your screens
inherit from a common project-wise screen that adds these headers
all the time.

If you are using the pull model, your best bet is probably to
use a pull tool, as someone else suggested.


-- 
Gonzalo A. Diethelm
gonzalo.diethelm@aditiva.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>