• Programming
    • Ramblings
    • ORCiD & Datacite
  • What I’m up to
  • Custom Currie Grid Glaze Calculator
  • Wanna buy a mug?
Programming and pottery

Programming and pottery - they're sort of related, right?

caching

Controlling the cache headers for a RESTlet directory

January 18, 2014 by Tom Leave a Comment

My previous post described how to serve webjars with RESTlet.  This post will describe how to add caching so that users don’t swamp your servers with requests. Put simply, you put a filter in the chain before the directory and modify the HTTP headers of successful requests.  Like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//add a webjars listener.
final Directory dir = new Directory(getContext(), "clap://class/META-INF/resources/webjars");
 
//add cache headers to the webjars so we're not swamped by requests, set things to expire in a year.
Filter cache = new Filter(getContext(),dir){
protected void afterHandle(Request request, Response response) {
        super.afterHandle(request, response);
        if (response!= null && response.getEntity() != null) {
         if (response.getStatus().equals(Status.SUCCESS_OK)){
final Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, 1);
response.getEntity().setExpirationDate(calendar.getTime());
response.setCacheDirectives(new ArrayList<CacheDirective>());
response.getCacheDirectives().add(CacheDirective.maxAge(31536000));
}
        }
    }
};
 
this.attach("/webjars", cache);

 

Posted in: Programming Tagged: caching, filters, restlet

Login with ORCID

Recent Posts

  • How to load the ORCID data dump into mongo, without dying of old age March 20, 2017
  • Did I mention I’m a potter? March 14, 2017
  • Getting started with the public ORCID API using swagger – quickstart guide September 28, 2015
  • Differences between ORCID and DataCite Metadata September 18, 2015
  • C# FluentValidation – why we’re using it April 29, 2015
  • ORCiD Java Client now supports schema version 1.2! March 2, 2015
  • Delphi isn’t quite dead yet. December 30, 2014
  • Goodbye to the British Library, hello corporate life. December 16, 2014
  • Making acceptance testing easy, useful and fun with BDD – enter cucumber November 10, 2014
  • Writing CSV using Jackson CSVMapper & Mixin annotations October 31, 2014
  • How to set up method level caching with annotations using javax.cache / JCache / JSR-107 & Guice October 23, 2014
  • ETD2014 slides July 25, 2014
  • ELAG2014 – Slides June 16, 2014
  • A different view of the British Library – photos May 6, 2014
  • Generating POJOs from XML schemas using JAXB XJC March 6, 2014
  • The age of geocities – Bubba says HOWDY!!! February 11, 2014
  • ORCiD tools – who’s claiming what? February 10, 2014
  • I didn’t go to university to get myself a job February 7, 2014
  • ORCID Open Source Java Client – I made this! January 21, 2014
  • Controlling the cache headers for a RESTlet directory January 18, 2014

Links

My StackOverflow account

My Mugs!

Copyright © 2019 Programming and pottery.

Omega WordPress Theme by ThemeHall