1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.client.webdav.caching;
17
18 import java.io.IOException;
19
20 import nl.hippo.client.api.ClientException;
21 import nl.hippo.client.api.caching.CacheKey;
22 import nl.hippo.client.api.caching.CachedResponse;
23 import nl.hippo.client.api.caching.CachedResponseImpl;
24 import nl.hippo.client.api.content.DocumentPath;
25 import nl.hippo.client.api.event.EventValidity;
26 import nl.hippo.client.api.service.CachingService;
27 import nl.hippo.client.api.service.NOPCachingService;
28 import nl.hippo.client.webdav.WebdavLogger;
29 import nl.hippo.client.webdav.WebdavRequest;
30 import nl.hippo.client.webdav.WebdavResponse;
31
32 public class WebdavCacheHandler {
33
34 public static final WebdavCacheHandler NULLCACHE = new WebdavCacheHandler(new NOPCachingService());
35
36 private CachingService cache;
37
38 public WebdavCacheHandler(CachingService cache) {
39 this.cache = cache;
40 }
41
42 public WebdavResponse getWebdavResponse(WebdavRequest request) {
43 WebdavCacheKey cacheKey = new WebdavCacheKey(request);
44 Object obj = cache.get(cacheKey);
45 if (obj != null) {
46 if (obj instanceof WebdavResponse) {
47 return (WebdavResponse) obj;
48 } else {
49 warnTypeMismatch(cacheKey, WebdavResponse.class, obj.getClass());
50 cache.remove(cacheKey);
51 }
52 }
53 return null;
54 }
55
56 public DocumentPath getDocumentPath(WebdavRequest request) {
57 WebdavCacheKey cacheKey = new WebdavCacheKey(request);
58 Object obj = cache.get(cacheKey);
59 if (obj != null) {
60 if (obj instanceof DocumentPath) {
61 return (DocumentPath) obj;
62 } else {
63 warnTypeMismatch(cacheKey, DocumentPath.class, obj.getClass());
64 cache.remove(cacheKey);
65 }
66 }
67 return null;
68 }
69
70 public void store(WebdavRequest request, EventValidity validity, Object object) throws ClientException {
71 try {
72 EventValidity[] validities = new EventValidity[] { validity };
73 CachedResponse cr = new CachedResponseImpl(validities, object);
74 WebdavCacheKey cacheKey = new WebdavCacheKey(request);
75 cache.store(cacheKey, cr);
76 } catch (IOException e) {
77 throw new ClientException("Exception while caching the object", e);
78 }
79 }
80
81 public void store(WebdavResponse response) throws ClientException {
82 if (response == null || response.getRequest() == null) {
83
84 return;
85 }
86 try {
87 WebdavRequest request = response.getRequest();
88 EventValidity[] validities = EventValidityCalculator.calculate(request);
89 CachedResponse cr = new CachedResponseImpl(validities, response);
90 CacheKey cacheKey = new WebdavCacheKey(request);
91 cache.store(cacheKey, cr);
92 } catch (IOException e) {
93 throw new ClientException("Exception while caching the response", e);
94 }
95 }
96
97 private void warnTypeMismatch(WebdavCacheKey cacheKey, Class expectedClass, Class actualClass) {
98 if (WebdavLogger.log.isWarnEnabled()) {
99 StringBuffer msg = new StringBuffer().append("Expected cache entry stored under ").append(
100 cacheKey.toString()).append(" to be of type ").append(expectedClass.getName()).append(", but was ")
101 .append(actualClass.getName());
102 WebdavLogger.log.warn(msg.toString());
103 }
104 }
105 }