View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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  import java.util.function.Function;
33  
34  import org.apache.hc.client5.http.cache.HttpCacheContext;
35  import org.apache.hc.client5.http.cache.HttpCacheEntryFactory;
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.protocol.HttpClientContext;
43  import org.apache.hc.client5.http.schedule.SchedulingStrategy;
44  import org.apache.hc.core5.http.config.NamedElementChain;
45  import org.apache.hc.core5.http.protocol.HttpContext;
46  
47  /**
48   * Builder for {@link org.apache.hc.client5.http.impl.classic.CloseableHttpClient}
49   * instances capable of client-side caching.
50   *
51   * @since 4.3
52   */
53  public class CachingHttpClientBuilder extends HttpClientBuilder {
54  
55      private ResourceFactory resourceFactory;
56      private HttpCacheStorage storage;
57      private File cacheDir;
58      private SchedulingStrategy schedulingStrategy;
59      private CacheConfig cacheConfig;
60      private boolean deleteCache;
61  
62      public static CachingHttpClientBuilder create() {
63          return new CachingHttpClientBuilder();
64      }
65  
66      protected CachingHttpClientBuilder() {
67          super();
68          addResponseInterceptorFirst(ResponseCacheConformance.INSTANCE);
69          addResponseInterceptorLast(ResponseViaCache.INSTANCE);
70          addRequestInterceptorLast(RequestViaCache.INSTANCE);
71          this.deleteCache = true;
72      }
73  
74      public final CachingHttpClientBuilder setResourceFactory(
75              final ResourceFactory resourceFactory) {
76          this.resourceFactory = resourceFactory;
77          return this;
78      }
79  
80      public final CachingHttpClientBuilder setHttpCacheStorage(final HttpCacheStorage storage) {
81          this.storage = storage;
82          return this;
83      }
84  
85      public final CachingHttpClientBuilder setCacheDir(final File cacheDir) {
86          this.cacheDir = cacheDir;
87          return this;
88      }
89  
90      public final CachingHttpClientBuilder setSchedulingStrategy(final SchedulingStrategy schedulingStrategy) {
91          this.schedulingStrategy = schedulingStrategy;
92          return this;
93      }
94  
95      public final CachingHttpClientBuilder setCacheConfig(final CacheConfig cacheConfig) {
96          this.cacheConfig = cacheConfig;
97          return this;
98      }
99  
100     /**
101      * @deprecated Do not use.
102      */
103     @Deprecated
104     public final CachingHttpClientBuilder setHttpCacheInvalidator(final org.apache.hc.client5.http.cache.HttpCacheInvalidator cacheInvalidator) {
105         return this;
106     }
107 
108     public final CachingHttpClientBuilder setDeleteCache(final boolean deleteCache) {
109         this.deleteCache = deleteCache;
110         return this;
111     }
112 
113     @Override
114     protected void customizeExecChain(final NamedElementChain<ExecChainHandler> execChainDefinition) {
115         final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
116         // We copy the instance fields to avoid changing them, and rename to avoid accidental use of the wrong version
117         ResourceFactory resourceFactoryCopy = this.resourceFactory;
118         if (resourceFactoryCopy == null) {
119             if (this.cacheDir == null) {
120                 resourceFactoryCopy = new HeapResourceFactory();
121             } else {
122                 resourceFactoryCopy = new FileResourceFactory(cacheDir);
123             }
124         }
125         HttpCacheStorage storageCopy = this.storage;
126         if (storageCopy == null) {
127             if (this.cacheDir == null) {
128                 storageCopy = new BasicHttpCacheStorage(config);
129             } else {
130                 final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
131                 if (this.deleteCache) {
132                     addCloseable(managedStorage::shutdown);
133                 } else {
134                     addCloseable(managedStorage);
135                 }
136                 storageCopy = managedStorage;
137             }
138         }
139         final HttpCache httpCache = new BasicHttpCache(
140                 resourceFactoryCopy,
141                 HttpCacheEntryFactory.INSTANCE,
142                 storageCopy,
143                 CacheKeyGenerator.INSTANCE);
144 
145         DefaultCacheRevalidator cacheRevalidator = null;
146         if (config.getAsynchronousWorkers() > 0) {
147             final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(config.getAsynchronousWorkers());
148             addCloseable(executorService::shutdownNow);
149             cacheRevalidator = new DefaultCacheRevalidator(
150                     executorService,
151                     this.schedulingStrategy != null ? this.schedulingStrategy : ImmediateSchedulingStrategy.INSTANCE);
152         }
153         final CachingExec cachingExec = new CachingExec(
154                 httpCache,
155                 cacheRevalidator,
156                 config);
157         execChainDefinition.addBefore(ChainElement.PROTOCOL.name(), cachingExec, ChainElement.CACHING.name());
158     }
159 
160     @Override
161     protected Function<HttpContext, HttpClientContext> contextAdaptor() {
162         return HttpCacheContext::castOrCreate;
163     }
164 
165 }