Hello there,
part of my application uses static (never-expiring) resources like
images, CSS stylesheets, etc. I'm currently serving those resources
using the Directory class, like this:
Directory dir = new Directory(getContext(), "clap://system/res");
router.attach("/res", dir);
However, this sets the Expires HTTP header to the current date,
essentialy making the representations of those resources
non-cacheable, so the client (web browser) has to request them over
and over again and they trip the network wire over and over again.
This is far from optimum, even for small-sized resource
representations.
So, after reading a bit some messages on this ML, I've decided to
extend both Directory and DirectoryServerResource like this:
public class ExpirableDirectoryResource extends Directory {
public ExpirableDirectoryResource(Context context, Reference
rootLocalReference) {
super(context, rootLocalReference);
setTargetClass(FarExpiringDirectoryResource.class);
}
public ExpirableDirectoryResource(Context context, String rootUri) {
this(context, new Reference(rootUri));
}
}
public class FarExpiringDirectoryResource extends DirectoryServerResource {
Calendar expiresOn;
public FarExpiringDirectoryResource() {
super();
expiresOn = Calendar.getInstance();
expiresOn.add(Calendar.YEAR, 1); // By default, expire 1 yr from now
getLogger().info(String.format("+++DBG: Expiration date set to
%s", expiresOn.getTime()));
}
@Override
public Representation get() throws ResourceException {
getLogger().info("+++DBG: GET on FarExpiringDirectoryResource...");
Representation rep = super.get();
getLogger().info(String.format("+++DBG: Should expire on %s",
expiresOn.getTime()));
rep.setExpirationDate(expiresOn.getTime());
return rep;
}
}
This should give my application the ability to serve static resources
set to expire in one year, which should be enough for caching
purposes, by simply replacing the Directory code above with:
ExpirableDirectoryResource dir = new
ExpirableDirectoryResource(getContext(), "clap://system/res");
router.attach("/res", dir);
in my application's createInboundRoot() method.
However, it's not working (Restlet 2.0M7). This is what I get in the logs:
02/02/2010 13:30:59 com.calenco.resource.FarExpiringDirectoryResource <init>
INFO: +++DBG: Expiration date set to Wed Feb 02 13:30:59 ARST 2011
02/02/2010 13:30:59 org.restlet.engine.local.DirectoryServerResource doInit
INFO: Converted target URI: clap://system/res/css/style.css
02/02/2010 13:30:59 org.restlet.engine.local.DirectoryServerResource getVariants
INFO: Getting variants for : clap://system/res/css/style.css
02/02/2010 13:30:59 org.restlet.engine.log.LogFilter afterHandle
INFO: 2010-02-02 13:30:59 0:0:0:0:0:0:0:1 [email protected]
- -1 GET /res/css/style.css - 200 - 0
16 http://localhost:9000 Mozilla/5.0
(X11; U; Linux x86_64; es-AR; rv:1.9.1.7) Gecko/20100106 Ubuntu/9.10
(karmic) Firefox/3.5.7 http://localhost:9000/
02/02/2010 13:30:59 com.calenco.resource.FarExpiringDirectoryResource <init>
INFO: +++DBG: Expiration date set to Wed Feb 02 13:30:59 ARST 2011
02/02/2010 13:30:59 org.restlet.engine.local.DirectoryServerResource doInit
INFO: Converted target URI: clap://system/res/img/header.jpg
02/02/2010 13:30:59 org.restlet.engine.local.DirectoryServerResource getVariants
INFO: Getting variants for : clap://system/res/img/header.jpg
02/02/2010 13:30:59 org.restlet.engine.log.LogFilter afterHandle
INFO: 2010-02-02 13:30:59 0:0:0:0:0:0:0:1 [email protected]
- -1 GET /res/img/header.jpg - 200 - 0
4http://localhost:9000 Mozilla/5.0
(X11; U; Linux x86_64; es-AR; rv:1.9.1.7) Gecko/20100106 Ubuntu/9.10
(karmic) Firefox/3.5.7 http://localhost:9000/res/css/style.css
A few things can be seen there:
- The new resources are being used, because the "Expiration date set
to..." message shows that the FarExpiringDirectoryResource is being
instantiated with the correct date set (1 year from the time of the
request)
- The get() handler for FarExpiringDirectoryResource is not being
called, only its parent (DirectoryServerResource) one is, thus giving
you instant-expiring representations instead of far-expiring ones
So, why isn't the FarExpiringDirectoryResource#get being called
instead of DirectoryServerResource#get?
Thanks for reading up to here and thanks in advance for your help.
--
Fabián Mandelbaum
IS Engineer
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2444198