You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Daniel Niklas <da...@continentale.de> on 2008/02/01 14:22:29 UTC

Need Filter that caches resources - a more general Extensions Filter?

Hi,

i need a filter that adds Cache-control to the response-header. I want to
configure this out for pictures, javascript files and other resources.

Do you know something like this in myfaces or somewhere else? I can't
believe, that i have to write my own implementation of such a filter!?

Best regards
Daniel Niklas
-- 
View this message in context: http://www.nabble.com/Need-Filter-that-caches-resources---a-more-general-Extensions-Filter--tp15226545p15226545.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: Need Filter that caches resources - a more general Extensions Filter?

Posted by Matthias Wessendorf <ma...@apache.org>.
hey,

did a quick search, and found this article:
http://www.infoq.com/articles/etags

I did something like that in the past, but I have no access
to the code.

-Matthias

On Feb 1, 2008 2:22 PM, Daniel Niklas <da...@continentale.de> wrote:
>
> Hi,
>
> i need a filter that adds Cache-control to the response-header. I want to
> configure this out for pictures, javascript files and other resources.
>
> Do you know something like this in myfaces or somewhere else? I can't
> believe, that i have to write my own implementation of such a filter!?
>
> Best regards
> Daniel Niklas
> --
> View this message in context: http://www.nabble.com/Need-Filter-that-caches-resources---a-more-general-Extensions-Filter--tp15226545p15226545.html
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>



-- 
Matthias Wessendorf

further stuff:
blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
mail: matzew-at-apache-dot-org

RE: Need Filter that caches resources - a more general Extensions Filter?

Posted by Michael Heinen <mh...@recommind.com>.
Very easy:

  <filter>
    <filter-name>BrowserCache</filter-name>
    <filter-class>com.bla.CacheFilter</filter-class>
    <init-param>
      <param-name>Cache-Control</param-name>
      <param-value>private,max-age=3600</param-value>
    </init-param>
    <init-param>
      <param-name>Pragma</param-name>
      <param-value>cache</param-value><!--store on proxies-->
    </init-param>
  </filter>
  
  <filter-mapping>
    <filter-name>BrowserCache</filter-name>
    <url-pattern>*.gif</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>BrowserCache</filter-name>
    <url-pattern>*.jpg</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>BrowserCache</filter-name>
    <url-pattern>*.js</url-pattern>
  </filter-mapping>
  

public class CacheFilter implements Filter {

	private FilterConfig filterConfig;

	public void init(FilterConfig filterConfig) {
		this.filterConfig = filterConfig;
	}

	public void doFilter(ServletRequest req, ServletResponse res,
FilterChain filterChain) throws IOException, ServletException {
	    HttpServletResponse response = (HttpServletResponse) res;

	    Enumeration enu = this.filterConfig.getInitParameterNames();
	    while ( enu.hasMoreElements() ) {
	      String headerName = (String) enu.nextElement();
	      response.setHeader(headerName,
filterConfig.getInitParameter(headerName));

	      //use addHeader so multiple headers can be added...
	      //BUT: Tomcat adds response no-cache headers to protected
pages to prevent them from being cached by proxy servers.
	      //--> Images arent cached anymore
	      //--> Alternative: Use WebServer to server static content
	      //response.addHeader(headerName,
filterConfig.getInitParameter(headerName));
	    }

	    // pass the request/response on to the rest of the filters
	    filterChain.doFilter(req, response);
	}
}


Michael

-----Original Message-----
From: Daniel Niklas [mailto:daniel.niklas@continentale.de] 
Sent: Freitag, 1. Februar 2008 14:22
To: users@myfaces.apache.org
Subject: Need Filter that caches resources - a more general Extensions
Filter?


Hi,

i need a filter that adds Cache-control to the response-header. I want
to
configure this out for pictures, javascript files and other resources.

Do you know something like this in myfaces or somewhere else? I can't
believe, that i have to write my own implementation of such a filter!?

Best regards
Daniel Niklas
-- 
View this message in context:
http://www.nabble.com/Need-Filter-that-caches-resources---a-more-general
-Extensions-Filter--tp15226545p15226545.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.



Re: Need Filter that caches resources - a more general Extensions Filter?

Posted by Daniel Niklas <da...@continentale.de>.
Hi Scott,



Scott O'Bryan wrote:
> 
> Hey Daniel, you may wish to stay away from Filters if you are going to 
> contribute this to Trinidad. It'll break portlet compatibility.
> 
You're right, this would be a Problem.



> That said, what is the problem you are trying to solve here? Is it that 
> resources are not currently being cached in your environment by the 
> browser...
> 
Yes, my resources are not beeing cached. But this problem is solved 
for me, because these resources are delivered by weblets and if you 
provide a version number in your weblet configuration, then weblets 
enables caching, too (similar to the trinidad ResourceServlet).



> I do know that I was having some issues with the cachability of static 
> resources on the Oracle Application Server because the jsessionId was 
> changing the url on me... 
> 
I think, there is no problem here with tomcat. But i don't know, if there
is a problem in a portal environment, e.g. Websphere?!


Daniel :-)

-- 
View this message in context: http://www.nabble.com/Need-Filter-that-caches-resources---a-more-general-Extensions-Filter--tp15226545p15306330.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: Need Filter that caches resources - a more general Extensions Filter?

Posted by Scott O'Bryan <da...@gmail.com>.
Hey Daniel, you may wish to stay away from Filters if you are going to 
contribute this to Trinidad. It'll break portlet compatibility.

That said, what is the problem you are trying to solve here? Is it that 
resources are not currently being cached in your environment by the 
browser, or that the Trinidad ResourceServlet seems to be too slow.

I do know that I was having some issues with the cachability of static 
resources on the Oracle Application Server because the jsessionId was 
changing the url on me. I was able to create a configuratior which 
disabled the encoding of resource urls when running in a servlet 
environment and the cachability returned. I didn't submit it to Trinidad 
because I feared it would break environments where the session wasn't 
tracked as a cookie.

Scott

Daniel Niklas wrote:
> Hi,
>
> Working on the caching feature, i found out the following:
>
> The TrinidadResourceServlet has already build in Cache-Control. It sets the 
> expires header to "+ 1 year". This is good in case of requesting
> js-libraries, 
> because theses generated files has the trinidad version as suffix in its 
> filename. (e.g. "adf/jsLibs/Common1_0_5.js")
>
> There isn't a suffix for image requests. This will be a problem, when images 
> changes in a future version. (e.g. "/adf/images/t.gif"). 
>
> The file "t.gif" seems to be a special case, because this is part of the 
> XhtmlRenderer and is used for generating gabs!? Other images, e.g. 
> "adf/images/dfb.gif", are part of the skin. There will be the same problem,
> when 
> using trinidad standard skin.
>
> TrinidadResourceServlet, Line 458:
>
>       // We set two headers: Cache-Control and Expires. // This combination
> lets 
>       browsers know that it is // okay to cache the resource indefinitely.
>
>       // Set Cache-Control to "Public". response.setHeader("Cache-Control", 
>       "Public");
>
>       // Set Expires to current time + one year. long currentTime = 
>       System.currentTimeMillis();
>
>       response.setDateHeader("Expires", currentTime + ONE_YEAR_MILLIS);
>
>       
> I'm using weblets, to load resources. You can enable caching for weblets,
> too. 
> The documentatin says:
>
> "Weblets use versioning to leverage the browser cache behavior so that
> packaged 
> resources can be downloaded and cached as efficiently as possible. The
> browser 
> only needs to check for new updates when the cache has been emptied or when
> the 
> component library has been upgraded at the web server."
>
> To enable caching, you have to provide a version number in your
> weblets.config. 
> When you provide a version, the generated urls are containing version 
> information, too. The version number could be the same as the version number
> of 
> the corresponding jar.
>
> WebletResponseImpl, Line 82 (weblets 0.4)
>
>     long now = System.currentTimeMillis(); long never = now + 1000 * 60 * 60
> * 
>     24 * 365; _httpResponse.setDateHeader("Expires", never);
>
> (see https://weblets.dev.java.net/trunk/shortdoc/introduction.html)
>
> I don't have other static resources. For this reason i don't need another 
> filter, that adds cache control headers or something similar.
>
> Best regards
> Daniel
>   


Re: Need Filter that caches resources - a more general Extensions Filter?

Posted by Daniel Niklas <da...@continentale.de>.
Hi,

Working on the caching feature, i found out the following:

The TrinidadResourceServlet has already build in Cache-Control. It sets the 
expires header to "+ 1 year". This is good in case of requesting
js-libraries, 
because theses generated files has the trinidad version as suffix in its 
filename. (e.g. "adf/jsLibs/Common1_0_5.js")

There isn't a suffix for image requests. This will be a problem, when images 
changes in a future version. (e.g. "/adf/images/t.gif"). 

The file "t.gif" seems to be a special case, because this is part of the 
XhtmlRenderer and is used for generating gabs!? Other images, e.g. 
"adf/images/dfb.gif", are part of the skin. There will be the same problem,
when 
using trinidad standard skin.

TrinidadResourceServlet, Line 458:

      // We set two headers: Cache-Control and Expires. // This combination
lets 
      browsers know that it is // okay to cache the resource indefinitely.

      // Set Cache-Control to "Public". response.setHeader("Cache-Control", 
      "Public");

      // Set Expires to current time + one year. long currentTime = 
      System.currentTimeMillis();

      response.setDateHeader("Expires", currentTime + ONE_YEAR_MILLIS);

      
I'm using weblets, to load resources. You can enable caching for weblets,
too. 
The documentatin says:

"Weblets use versioning to leverage the browser cache behavior so that
packaged 
resources can be downloaded and cached as efficiently as possible. The
browser 
only needs to check for new updates when the cache has been emptied or when
the 
component library has been upgraded at the web server."

To enable caching, you have to provide a version number in your
weblets.config. 
When you provide a version, the generated urls are containing version 
information, too. The version number could be the same as the version number
of 
the corresponding jar.

WebletResponseImpl, Line 82 (weblets 0.4)

    long now = System.currentTimeMillis(); long never = now + 1000 * 60 * 60
* 
    24 * 365; _httpResponse.setDateHeader("Expires", never);

(see https://weblets.dev.java.net/trunk/shortdoc/introduction.html)

I don't have other static resources. For this reason i don't need another 
filter, that adds cache control headers or something similar.

Best regards
Daniel
-- 
View this message in context: http://www.nabble.com/Need-Filter-that-caches-resources---a-more-general-Extensions-Filter--tp15226545p15291128.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: Need Filter that caches resources - a more general Extensions Filter?

Posted by Matthias Wessendorf <ma...@apache.org>.
Daniel,

feel free to add a filter.
I'd prefer a bit more complex one, that deals with ETag,
if-none-Match, ... as well

-M

On Feb 1, 2008 3:00 PM, Daniel Niklas <da...@continentale.de> wrote:
>
> Hi,
>
>
> Matthias Wessendorf-4 wrote:
> >
> > like almost every body :-)
> >
> Me too, that is why i'm asking ;-)
>
>
> Matthias Wessendorf-4 wrote:
> >
> >> It would be nice to add an implementation of this to one of the new
> >> myfaces-commons projects...but AFAIK there isn't like this anything
> >> available from the myfaces code at the moment.
> >
> +1
>
> Daniel
>
>
>
> --
> View this message in context: http://www.nabble.com/Need-Filter-that-caches-resources---a-more-general-Extensions-Filter--tp15226545p15227214.html
>
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>



-- 
Matthias Wessendorf

further stuff:
blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
mail: matzew-at-apache-dot-org

Re: Need Filter that caches resources - a more general Extensions Filter?

Posted by Daniel Niklas <da...@continentale.de>.
Hi,


Matthias Wessendorf-4 wrote:
> 
> like almost every body :-)
> 
Me too, that is why i'm asking ;-)


Matthias Wessendorf-4 wrote:
> 
>> It would be nice to add an implementation of this to one of the new
>> myfaces-commons projects...but AFAIK there isn't like this anything
>> available from the myfaces code at the moment.
> 
+1

Daniel



-- 
View this message in context: http://www.nabble.com/Need-Filter-that-caches-resources---a-more-general-Extensions-Filter--tp15226545p15227214.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.