1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl.cache;
28
29 import java.io.File;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.ScheduledThreadPoolExecutor;
32
33 import org.apache.hc.client5.http.async.AsyncExecChainHandler;
34 import org.apache.hc.client5.http.cache.HttpAsyncCacheStorage;
35 import org.apache.hc.client5.http.cache.HttpAsyncCacheStorageAdaptor;
36 import org.apache.hc.client5.http.cache.HttpCacheEntryFactory;
37 import org.apache.hc.client5.http.cache.HttpCacheStorage;
38 import org.apache.hc.client5.http.cache.ResourceFactory;
39 import org.apache.hc.client5.http.impl.ChainElement;
40 import org.apache.hc.client5.http.impl.async.H2AsyncClientBuilder;
41 import org.apache.hc.client5.http.impl.schedule.ImmediateSchedulingStrategy;
42 import org.apache.hc.client5.http.schedule.SchedulingStrategy;
43 import org.apache.hc.core5.http.config.NamedElementChain;
44
45
46
47
48
49
50
51 public class CachingH2AsyncClientBuilder extends H2AsyncClientBuilder {
52
53 private ResourceFactory resourceFactory;
54 private HttpAsyncCacheStorage storage;
55 private File cacheDir;
56 private SchedulingStrategy schedulingStrategy;
57 private CacheConfig cacheConfig;
58 private boolean deleteCache;
59
60 public static CachingH2AsyncClientBuilder create() {
61 return new CachingH2AsyncClientBuilder();
62 }
63
64 protected CachingH2AsyncClientBuilder() {
65 super();
66 addResponseInterceptorFirst(ResponseCacheConformance.INSTANCE);
67 addResponseInterceptorLast(ResponseViaCache.INSTANCE);
68 addRequestInterceptorLast(RequestViaCache.INSTANCE);
69 this.deleteCache = true;
70 }
71
72 public final CachingH2AsyncClientBuilder setResourceFactory(final ResourceFactory resourceFactory) {
73 this.resourceFactory = resourceFactory;
74 return this;
75 }
76
77 public final CachingH2AsyncClientBuilder setHttpCacheStorage(final HttpCacheStorage storage) {
78 this.storage = storage != null ? new HttpAsyncCacheStorageAdaptor(storage) : null;
79 return this;
80 }
81
82 public final CachingH2AsyncClientBuilder setHttpCacheStorage(final HttpAsyncCacheStorage storage) {
83 this.storage = storage;
84 return this;
85 }
86
87 public final CachingH2AsyncClientBuilder setCacheDir(final File cacheDir) {
88 this.cacheDir = cacheDir;
89 return this;
90 }
91
92 public final CachingH2AsyncClientBuilder setSchedulingStrategy(final SchedulingStrategy schedulingStrategy) {
93 this.schedulingStrategy = schedulingStrategy;
94 return this;
95 }
96
97 public final CachingH2AsyncClientBuilder setCacheConfig(final CacheConfig cacheConfig) {
98 this.cacheConfig = cacheConfig;
99 return this;
100 }
101
102
103
104
105 @Deprecated
106 public final CachingH2AsyncClientBuilder setHttpCacheInvalidator(final org.apache.hc.client5.http.cache.HttpAsyncCacheInvalidator cacheInvalidator) {
107 return this;
108 }
109
110 public CachingH2AsyncClientBuilder setDeleteCache(final boolean deleteCache) {
111 this.deleteCache = deleteCache;
112 return this;
113 }
114
115 @Override
116 protected void customizeExecChain(final NamedElementChain<AsyncExecChainHandler> execChainDefinition) {
117 final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
118
119 ResourceFactory resourceFactoryCopy = this.resourceFactory;
120 if (resourceFactoryCopy == null) {
121 if (this.cacheDir == null) {
122 resourceFactoryCopy = new HeapResourceFactory();
123 } else {
124 resourceFactoryCopy = new FileResourceFactory(cacheDir);
125 }
126 }
127 HttpAsyncCacheStorage storageCopy = this.storage;
128 if (storageCopy == null) {
129 if (this.cacheDir == null) {
130 storageCopy = new HttpAsyncCacheStorageAdaptor(new BasicHttpCacheStorage(config));
131 } else {
132 final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
133 if (this.deleteCache) {
134 addCloseable(managedStorage::shutdown);
135 } else {
136 addCloseable(managedStorage);
137 }
138 storageCopy = new HttpAsyncCacheStorageAdaptor(managedStorage);
139 }
140 }
141 final HttpAsyncCache httpCache = new BasicHttpAsyncCache(
142 resourceFactoryCopy,
143 HttpCacheEntryFactory.INSTANCE,
144 storageCopy,
145 CacheKeyGenerator.INSTANCE);
146
147 DefaultAsyncCacheRevalidator cacheRevalidator = null;
148 if (config.getAsynchronousWorkers() > 0) {
149 final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(config.getAsynchronousWorkers());
150 addCloseable(executorService::shutdownNow);
151 cacheRevalidator = new DefaultAsyncCacheRevalidator(
152 executorService,
153 this.schedulingStrategy != null ? this.schedulingStrategy : ImmediateSchedulingStrategy.INSTANCE);
154 }
155
156 final AsyncCachingExec cachingExec = new AsyncCachingExec(
157 httpCache,
158 cacheRevalidator,
159 config);
160 execChainDefinition.addBefore(ChainElement.PROTOCOL.name(), cachingExec, ChainElement.CACHING.name());
161 }
162
163 }