1 package de.juplo.accelerator.examples.spring;
3 import de.juplo.accelerator.annotations.CacheSeconds;
4 import de.juplo.accelerator.annotations.Cacheable;
5 import de.juplo.accelerator.annotations.LastModified;
7 import java.util.HashMap;
9 import java.util.TreeMap;
10 import javax.servlet.http.HttpServletRequest;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.servlet.ModelAndView;
17 * Simple Spring-MVC Controller
21 @Cacheable(eager=true)
22 public class SpringController
24 public static final String ACCESS_TIME = SpringController.class.getCanonicalName() + ".ACCESS_TIME";
25 public static final Integer DEFAULT_MAX_AGE = 60;
27 private Date lastModified = new Date();
28 private Map<Date,String> requests = new TreeMap<Date,String>();
29 private Map<String,Date> accessTimes = new HashMap<String,Date>();
33 public int cacheSeconds(HttpServletRequest request) {
34 String maxAge = request.getParameter("max-age");
35 if (maxAge != null && maxAge.matches("^\\s*\\d+\\s*$"))
36 return Integer.parseInt(maxAge);
38 return DEFAULT_MAX_AGE;
42 public long lastModified(HttpServletRequest request) {
43 String query = request.getQueryString();
44 query = query == null ? "" : query;
45 Date accessTime = accessTimes.get(query);
46 if (accessTime == null || !accessTime.equals(lastModified))
48 accessTime = new Date();
49 request.setAttribute(ACCESS_TIME, accessTime);
50 return accessTime.getTime();
53 @RequestMapping("/spring-controller.html")
54 public ModelAndView process(HttpServletRequest request)
56 lastModified = (Date)request.getAttribute(ACCESS_TIME);
57 String query = request.getQueryString();
58 query = query == null ? "" : query;
59 requests.put(lastModified, query);
60 accessTimes.put(query, lastModified);
61 ModelAndView mav = new ModelAndView("controller-view");
62 mav.addObject("requests", requests);