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.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   * Builder for {@link org.apache.hc.client5.http.impl.classic.CloseableHttpClient}
47   * instances capable of client-side caching.
48   *
49   * @since 4.3
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         // We copy the instance fields to avoid changing them, and rename to avoid accidental use of the wrong version
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 }