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.cache.HttpCacheInvalidator;
36 import org.apache.hc.client5.http.cache.HttpCacheStorage;
37 import org.apache.hc.client5.http.cache.ResourceFactory;
38 import org.apache.hc.client5.http.classic.ExecChainHandler;
39 import org.apache.hc.client5.http.impl.ChainElement;
40 import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
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 CachingHttpClientBuilder extends HttpClientBuilder {
52
53 private ResourceFactory resourceFactory;
54 private HttpCacheStorage storage;
55 private File cacheDir;
56 private SchedulingStrategy schedulingStrategy;
57 private CacheConfig cacheConfig;
58 private HttpCacheInvalidator httpCacheInvalidator;
59 private boolean deleteCache;
60
61 public static CachingHttpClientBuilder create() {
62 return new CachingHttpClientBuilder();
63 }
64
65 protected CachingHttpClientBuilder() {
66 super();
67 this.deleteCache = true;
68 }
69
70 public final CachingHttpClientBuilder setResourceFactory(
71 final ResourceFactory resourceFactory) {
72 this.resourceFactory = resourceFactory;
73 return this;
74 }
75
76 public final CachingHttpClientBuilder setHttpCacheStorage(final HttpCacheStorage storage) {
77 this.storage = storage;
78 return this;
79 }
80
81 public final CachingHttpClientBuilder setCacheDir(final File cacheDir) {
82 this.cacheDir = cacheDir;
83 return this;
84 }
85
86 public final CachingHttpClientBuilder setSchedulingStrategy(final SchedulingStrategy schedulingStrategy) {
87 this.schedulingStrategy = schedulingStrategy;
88 return this;
89 }
90
91 public final CachingHttpClientBuilder setCacheConfig(final CacheConfig cacheConfig) {
92 this.cacheConfig = cacheConfig;
93 return this;
94 }
95
96 public final CachingHttpClientBuilder setHttpCacheInvalidator(final HttpCacheInvalidator cacheInvalidator) {
97 this.httpCacheInvalidator = cacheInvalidator;
98 return this;
99 }
100
101 public final CachingHttpClientBuilder setDeleteCache(final boolean deleteCache) {
102 this.deleteCache = deleteCache;
103 return this;
104 }
105
106 @Override
107 protected void customizeExecChain(final NamedElementChain<ExecChainHandler> execChainDefinition) {
108 final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
109
110 ResourceFactory resourceFactoryCopy = this.resourceFactory;
111 if (resourceFactoryCopy == null) {
112 if (this.cacheDir == null) {
113 resourceFactoryCopy = new HeapResourceFactory();
114 } else {
115 resourceFactoryCopy = new FileResourceFactory(cacheDir);
116 }
117 }
118 HttpCacheStorage storageCopy = this.storage;
119 if (storageCopy == null) {
120 if (this.cacheDir == null) {
121 storageCopy = new BasicHttpCacheStorage(config);
122 } else {
123 final ManagedHttpCacheStoragedHttpCacheStorage.html#ManagedHttpCacheStorage">ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
124 if (this.deleteCache) {
125 addCloseable(new Closeable() {
126
127 @Override
128 public void close() throws IOException {
129 managedStorage.shutdown();
130 }
131
132 });
133 } else {
134 addCloseable(managedStorage);
135 }
136 storageCopy = managedStorage;
137 }
138 }
139 final HttpCache httpCache = new BasicHttpCache(
140 resourceFactoryCopy,
141 storageCopy,
142 CacheKeyGenerator.INSTANCE,
143 this.httpCacheInvalidator != null ? this.httpCacheInvalidator : new DefaultCacheInvalidator());
144
145 DefaultCacheRevalidator cacheRevalidator = null;
146 if (config.getAsynchronousWorkers() > 0) {
147 final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(config.getAsynchronousWorkers());
148 addCloseable(new Closeable() {
149
150 @Override
151 public void close() throws IOException {
152 executorService.shutdownNow();
153 }
154
155 });
156 cacheRevalidator = new DefaultCacheRevalidator(
157 executorService,
158 this.schedulingStrategy != null ? this.schedulingStrategy : ImmediateSchedulingStrategy.INSTANCE);
159 }
160 final CachingExecpl/cache/CachingExec.html#CachingExec">CachingExec cachingExec = new CachingExec(
161 httpCache,
162 cacheRevalidator,
163 config);
164 execChainDefinition.addBefore(ChainElement.PROTOCOL.name(), cachingExec, ChainElement.CACHING.name());
165 }
166
167 }