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.HttpAsyncCacheInvalidator;
35 import org.apache.hc.client5.http.cache.HttpAsyncCacheStorage;
36 import org.apache.hc.client5.http.cache.HttpAsyncCacheStorageAdaptor;
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.annotation.Experimental;
44 import org.apache.hc.core5.http.config.NamedElementChain;
45
46
47
48
49
50
51
52 @Experimental
53 public class CachingH2AsyncClientBuilder extends H2AsyncClientBuilder {
54
55 private ResourceFactory resourceFactory;
56 private HttpAsyncCacheStorage storage;
57 private File cacheDir;
58 private SchedulingStrategy schedulingStrategy;
59 private CacheConfig cacheConfig;
60 private HttpAsyncCacheInvalidator httpCacheInvalidator;
61 private boolean deleteCache;
62
63 public static CachingH2AsyncClientBuilder create() {
64 return new CachingH2AsyncClientBuilder();
65 }
66
67 protected CachingH2AsyncClientBuilder() {
68 super();
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 public final CachingH2AsyncClientBuilder setHttpCacheInvalidator(final HttpAsyncCacheInvalidator cacheInvalidator) {
103 this.httpCacheInvalidator = cacheInvalidator;
104 return this;
105 }
106
107 public CachingH2AsyncClientBuilder setDeleteCache(final boolean deleteCache) {
108 this.deleteCache = deleteCache;
109 return this;
110 }
111
112 @Override
113 protected void customizeExecChain(final NamedElementChain<AsyncExecChainHandler> execChainDefinition) {
114 final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
115
116 ResourceFactory resourceFactoryCopy = this.resourceFactory;
117 if (resourceFactoryCopy == null) {
118 if (this.cacheDir == null) {
119 resourceFactoryCopy = new HeapResourceFactory();
120 } else {
121 resourceFactoryCopy = new FileResourceFactory(cacheDir);
122 }
123 }
124 HttpAsyncCacheStorage storageCopy = this.storage;
125 if (storageCopy == null) {
126 if (this.cacheDir == null) {
127 storageCopy = new HttpAsyncCacheStorageAdaptor(new BasicHttpCacheStorage(config));
128 } else {
129 final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
130 if (this.deleteCache) {
131 addCloseable(managedStorage::shutdown);
132 } else {
133 addCloseable(managedStorage);
134 }
135 storageCopy = new HttpAsyncCacheStorageAdaptor(managedStorage);
136 }
137 }
138 final HttpAsyncCache httpCache = new BasicHttpAsyncCache(
139 resourceFactoryCopy,
140 storageCopy,
141 CacheKeyGenerator.INSTANCE,
142 this.httpCacheInvalidator != null ? this.httpCacheInvalidator : new DefaultAsyncCacheInvalidator());
143
144 DefaultAsyncCacheRevalidator cacheRevalidator = null;
145 if (config.getAsynchronousWorkers() > 0) {
146 final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(config.getAsynchronousWorkers());
147 addCloseable(executorService::shutdownNow);
148 cacheRevalidator = new DefaultAsyncCacheRevalidator(
149 executorService,
150 this.schedulingStrategy != null ? this.schedulingStrategy : ImmediateSchedulingStrategy.INSTANCE);
151 }
152
153 final AsyncCachingExec cachingExec = new AsyncCachingExec(
154 httpCache,
155 cacheRevalidator,
156 config);
157 execChainDefinition.addBefore(ChainElement.PROTOCOL.name(), cachingExec, ChainElement.CACHING.name());
158 }
159
160 }