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.Closeable;
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.concurrent.ScheduledExecutorService;
33 import java.util.concurrent.ScheduledThreadPoolExecutor;
34
35 import org.apache.hc.client5.http.async.AsyncExecChainHandler;
36 import org.apache.hc.client5.http.cache.HttpAsyncCacheInvalidator;
37 import org.apache.hc.client5.http.cache.HttpAsyncCacheStorage;
38 import org.apache.hc.client5.http.cache.HttpAsyncCacheStorageAdaptor;
39 import org.apache.hc.client5.http.cache.HttpCacheStorage;
40 import org.apache.hc.client5.http.cache.ResourceFactory;
41 import org.apache.hc.client5.http.impl.ChainElement;
42 import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
43 import org.apache.hc.client5.http.impl.schedule.ImmediateSchedulingStrategy;
44 import org.apache.hc.client5.http.schedule.SchedulingStrategy;
45 import org.apache.hc.core5.annotation.Experimental;
46 import org.apache.hc.core5.http.config.NamedElementChain;
47
48
49
50
51
52
53
54 @Experimental
55 public class CachingHttpAsyncClientBuilder extends HttpAsyncClientBuilder {
56
57 private ResourceFactory resourceFactory;
58 private HttpAsyncCacheStorage storage;
59 private File cacheDir;
60 private SchedulingStrategy schedulingStrategy;
61 private CacheConfig cacheConfig;
62 private HttpAsyncCacheInvalidator httpCacheInvalidator;
63 private boolean deleteCache;
64
65 public static CachingHttpAsyncClientBuilder create() {
66 return new CachingHttpAsyncClientBuilder();
67 }
68
69 protected CachingHttpAsyncClientBuilder() {
70 super();
71 this.deleteCache = true;
72 }
73
74 public final CachingHttpAsyncClientBuilder setResourceFactory(final ResourceFactory resourceFactory) {
75 this.resourceFactory = resourceFactory;
76 return this;
77 }
78
79 public final CachingHttpAsyncClientBuilder setHttpCacheStorage(final HttpCacheStorage storage) {
80 this.storage = storage != null ? new HttpAsyncCacheStorageAdaptor(storage) : null;
81 return this;
82 }
83
84 public final CachingHttpAsyncClientBuilder setHttpCacheStorage(final HttpAsyncCacheStorage storage) {
85 this.storage = storage;
86 return this;
87 }
88
89 public final CachingHttpAsyncClientBuilder setCacheDir(final File cacheDir) {
90 this.cacheDir = cacheDir;
91 return this;
92 }
93
94 public final CachingHttpAsyncClientBuilder setSchedulingStrategy(final SchedulingStrategy schedulingStrategy) {
95 this.schedulingStrategy = schedulingStrategy;
96 return this;
97 }
98
99 public final CachingHttpAsyncClientBuilder setCacheConfig(final CacheConfig cacheConfig) {
100 this.cacheConfig = cacheConfig;
101 return this;
102 }
103
104 public final CachingHttpAsyncClientBuilder setHttpCacheInvalidator(final HttpAsyncCacheInvalidator cacheInvalidator) {
105 this.httpCacheInvalidator = cacheInvalidator;
106 return this;
107 }
108
109 public CachingHttpAsyncClientBuilder setDeleteCache(final boolean deleteCache) {
110 this.deleteCache = deleteCache;
111 return this;
112 }
113
114 @Override
115 protected void customizeExecChain(final NamedElementChain<AsyncExecChainHandler> execChainDefinition) {
116 final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
117
118 ResourceFactory resourceFactoryCopy = this.resourceFactory;
119 if (resourceFactoryCopy == null) {
120 if (this.cacheDir == null) {
121 resourceFactoryCopy = new HeapResourceFactory();
122 } else {
123 resourceFactoryCopy = new FileResourceFactory(cacheDir);
124 }
125 }
126 HttpAsyncCacheStorage storageCopy = this.storage;
127 if (storageCopy == null) {
128 if (this.cacheDir == null) {
129 storageCopy = new HttpAsyncCacheStorageAdaptor(new BasicHttpCacheStorage(config));
130 } else {
131 final ManagedHttpCacheStoragedHttpCacheStorage.html#ManagedHttpCacheStorage">ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
132 if (this.deleteCache) {
133 addCloseable(new Closeable() {
134
135 @Override
136 public void close() throws IOException {
137 managedStorage.shutdown();
138 }
139
140 });
141 } else {
142 addCloseable(managedStorage);
143 }
144 storageCopy = new HttpAsyncCacheStorageAdaptor(managedStorage);
145 }
146 }
147 final HttpAsyncCache httpCache = new BasicHttpAsyncCache(
148 resourceFactoryCopy,
149 storageCopy,
150 CacheKeyGenerator.INSTANCE,
151 this.httpCacheInvalidator != null ? this.httpCacheInvalidator : new DefaultAsyncCacheInvalidator());
152
153 DefaultAsyncCacheRevalidator cacheRevalidator = null;
154 if (config.getAsynchronousWorkers() > 0) {
155 final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(config.getAsynchronousWorkers());
156 addCloseable(new Closeable() {
157
158 @Override
159 public void close() throws IOException {
160 executorService.shutdownNow();
161 }
162
163 });
164 cacheRevalidator = new DefaultAsyncCacheRevalidator(
165 executorService,
166 this.schedulingStrategy != null ? this.schedulingStrategy : ImmediateSchedulingStrategy.INSTANCE);
167 }
168
169 final AsyncCachingExecche/AsyncCachingExec.html#AsyncCachingExec">AsyncCachingExec cachingExec = new AsyncCachingExec(
170 httpCache,
171 cacheRevalidator,
172 config);
173 execChainDefinition.addBefore(ChainElement.PROTOCOL.name(), cachingExec, ChainElement.CACHING.name());
174 }
175
176 }