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