You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Magnús Þór Torfason <ma...@handtolvur.is> on 2001/03/20 14:29:12 UTC

HTTP cache headers from within Turbine

Hope this is not considered to offtopic or otherwise idiotic.

I need to set the correct headers so that my screens are not cached.

My proposal is to set

Pragma: no-cache
Cache-Control: no-cache
Expires: Fri, 30 Oct 1998 14:19:41 GMT

(this is offtopic I realize)

What I am wondering is how best to achieve it in Turbine.  I guess it can be
done by adding the following line to a build() method in one of the base
screens that all my screens extend:

data.getResponse().setHeader("Pragma", "no-cache");
data.getResponse().setHeader("Cache-Control", "no-cache");
data.getResponse().setHeader("Expires", HtmlDate.toString(new Date()) );

1) Is this the best way to do this?  This requires me to use the servlet
methods directly, which is normally not recommended.

2) Is there a utility function in turbine that handles HTTP dates correctly,
so that I can avoid doing the formatting myself.



Magnus


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: HTTP cache headers from within Turbine

Posted by Daniel Rall <dl...@collab.net>.
Magnús Þór Torfason <ma...@handtolvur.is> writes:

> Hope this is not considered to offtopic or otherwise idiotic.
> 
> I need to set the correct headers so that my screens are not cached.
> 
> My proposal is to set
> 
> Pragma: no-cache
> Cache-Control: no-cache
> Expires: Fri, 30 Oct 1998 14:19:41 GMT

This is good functionality, +1.

> 2) Is there a utility function in turbine that handles HTTP dates correctly,
> so that I can avoid doing the formatting myself.

This is only two lines of code with the JDK's SimpleDataFormat class.

Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: HTTP cache headers from within Turbine

Posted by Daniel Rall <dl...@collab.net>.
Rafal Krzewski <Ra...@e-point.pl> writes:

> Magnús ?ór Torfason wrote:
> > public class org.apache.turbine.util.HttpCacheUtils
> 
> Maybe we should have a broader class HttpUtils instead.
> HTTP date can have more uses than just Cache expiry, don't
> you think?

I agree.  Such a class could offer a lot of value.

Daniel Rall

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: HTTP cache headers from within Turbine

Posted by Daniel Rall <dl...@collab.net>.
Magn~s ?~r Torfason <ma...@handtolvur.is> writes:

> DLR, http date formatting is not many lines but they have to be well chosen
> so that the formatting will work regardless of default Locale and TimeSone.
> I just grabbed the code from Tomcat to minimize my efforts.

Looks like you did a great job keeping it down to two lines.  Anyone
know if SimpleDateFormat instances are thread-safe?  Judging from the
API is exposes, I'm betting it is mostly thread-safe by accident.  If
it turns out it's not thread-safe, formatHttpDate() should be
synchornized.  Nice work!

Dan



> /**
>  * This class provides utilities for handling some semi-trivial
>  * HTTP stuff that would othterwize be handled elsewhere.
>  *
>  * @author <a href="mailto:magnus@handpoint.com">Magnus Thor Torfason</a>
>  */
> public class HttpUtils
> {
> 	/**
> 	 * The date format to use for HTTP Dates.
> 	 */
> 	private static SimpleDateFormat httpDateFormat;
> 
> 	static
> 	{
> 		httpDateFormat = new SimpleDateFormat(
> 				"EEE, dd MMM yyyyy HH:mm:ss z", Locale.US  );
> 		httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
> 	}
> 
> 	/**
> 	 * Formats a java Date according to rfc 1123, the rfc
> 	 * standard for dates in http.
> 	 *
> 	 * @param date The Date to format
> 	 * @return A String represeentation of the date
> 	 */
>     public static String formatHttpDate(Date date)
>     {
>         return httpDateFormat.format(date);
>     }
> 
> 	/**
> 	 * This method sets the required expiration headers in the response
> 	 * for a given RunData object.  This method attempts to set all
> 	 * relevant headers, both for HTTP 1.0 and HTTP 1.1.
> 	 *
> 	 * @param data The RunData object we are setting cache information for.
> 	 * @param expiry The number of seconds untill the document should expire.
> 	 */
>     public static void setCacheHeaders(RunData data, int expiry)
>     {
> 		if ( expiry == 0 )
> 		{
> 			data.getResponse().setHeader("Pragma", "no-cache");
> 			data.getResponse().setHeader("Cache-Control", "no-cache");
> 			data.getResponse().setHeader(
> 					"Expires", formatHttpDate(new Date()));
> 		}
> 		else
> 		{
> 			Date expiryDate = new Date( System.currentTimeMillis() + expiry );
> 			data.getResponse().setHeader(
> 					"Expires", formatHttpDate(expiryDate));
> 		}
>     }
> }

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


RE: HTTP cache headers from within Turbine

Posted by Magnús ?ór Torfason <ma...@handtolvur.is>.

> > public class org.apache.turbine.util.HttpCacheUtils
> >
> > Magnus
>
> Maybe we should have a broader class HttpUtils instead.
> HTTP date can have more uses than just Cache expiry, don't
> you think?
>
> Rafal

Just what I thought myself, when I started implementing it.  Here it comes,
the HttpUtils class!  It is just a new file so I won't send a diff, just the
file itself.

DLR, http date formatting is not many lines but they have to be well chosen
so that the formatting will work regardless of default Locale and TimeSone.
I just grabbed the code from Tomcat to minimize my efforts.

Magnus

Re: HTTP cache headers from within Turbine

Posted by Rafal Krzewski <Ra...@e-point.pl>.
Magnús ?ór Torfason wrote:
> public class org.apache.turbine.util.HttpCacheUtils

Maybe we should have a broader class HttpUtils instead.
HTTP date can have more uses than just Cache expiry, don't
you think?

Rafal

--
Rafal Krzewski
Senior Internet Developer
mailto:Rafal.Krzewski@e-point.pl
+48 22 8534830 http://e-point.pl

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: HTTP cache headers from within Turbine

Posted by Daniel Rall <dl...@collab.net>.
Magn~s ?~r Torfason <ma...@handtolvur.is> writes:

> > I don't know of one, maybe your class should have a new home in
> > o.a.t.util?
> 
> Will do, I'll send a proposal after creating and testing this class.  I am
> thinking:
> 
> public class org.apache.turbine.util.HttpCacheUtils
> {
>     public static String getHttpDate(java.util.Date date)
>     {
>         ...
>     }
> 
>     public static void setCacheHeaders(RunData data, int expiry)
>     {
>         ...
>     }
> }

+1

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


RE: HTTP cache headers from within Turbine

Posted by Magnús ?ór Torfason <ma...@handtolvur.is>.
> I don't know of one, maybe your class should have a new home in
> o.a.t.util?

Will do, I'll send a proposal after creating and testing this class.  I am
thinking:

public class org.apache.turbine.util.HttpCacheUtils
{
    public static String getHttpDate(java.util.Date date)
    {
        ...
    }

    public static void setCacheHeaders(RunData data, int expiry)
    {
        ...
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: HTTP cache headers from within Turbine

Posted by Rafal Krzewski <Ra...@e-point.pl>.
Magnús Ţór Torfason wrote:

> I need to set the correct headers so that my screens are not cached.
<snip>
> (this is offtopic I realize)

No worries, this is important in many applications.
 
> What I am wondering is how best to achieve it in Turbine.  I guess it can be
> done by adding the following line to a build() method in one of the base
> screens that all my screens extend:
<snip>
> 1) Is this the best way to do this?  This requires me to use the servlet
> methods directly, which is normally not recommended.

In my opinon it is perfectly valid to call these methods to manipulate
headers.
I don't see any other way of doing this.
 
> 2) Is there a utility function in turbine that handles HTTP dates correctly,
> so that I can avoid doing the formatting myself.

I don't know of one, maybe your class should have a new home in
o.a.t.util?

Rafal

--
Rafal Krzewski
Senior Internet Developer
mailto:Rafal.Krzewski@e-point.pl
+48 22 8534830 http://e-point.pl

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: HTTP cache headers from within Turbine

Posted by Jon Stevens <jo...@latchkey.com>.
on 3/20/01 11:29 AM, "Magnús Þór Torfason" <ma...@handtolvur.is> wrote:

>>> This requires me to use the servlet
>>> methods directly, which is normally not recommended.
>>> 
>>> Magnus
>> 
>> Not true at all.
>> 
>> -jon
>> 
> 
> That it requires me to use the servlet methods, or that it it is not
> recommended?

That it is not recommended.

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


RE: HTTP cache headers from within Turbine

Posted by Magnús Þór Torfason <ma...@handtolvur.is>.
> > This requires me to use the servlet
> > methods directly, which is normally not recommended.
> >
> > Magnus
>
> Not true at all.
>
> -jon
>

That it requires me to use the servlet methods, or that it it is not
recommended?


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: HTTP cache headers from within Turbine

Posted by Jon Stevens <jo...@latchkey.com>.
on 3/20/01 5:29 AM, "Magnús Þór Torfason" <ma...@handtolvur.is> wrote:

> This requires me to use the servlet
> methods directly, which is normally not recommended.

Not true at all.

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org