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.HttpAsyncCacheStorage;
35  import org.apache.hc.client5.http.cache.HttpAsyncCacheStorageAdaptor;
36  import org.apache.hc.client5.http.cache.HttpCacheEntryFactory;
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.http.config.NamedElementChain;
44  
45  /**
46   * Builder for HTTP/2 {@link org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient}
47   * instances capable of client-side caching.
48   *
49   * @since 5.0
50   */
51  public class CachingH2AsyncClientBuilder extends H2AsyncClientBuilder {
52  
53      private ResourceFactory resourceFactory;
54      private HttpAsyncCacheStorage storage;
55      private File cacheDir;
56      private SchedulingStrategy schedulingStrategy;
57      private CacheConfig cacheConfig;
58      private boolean deleteCache;
59  
60      public static CachingH2AsyncClientBuilder create() {
61          return new CachingH2AsyncClientBuilder();
62      }
63  
64      protected CachingH2AsyncClientBuilder() {
65          super();
66          addResponseInterceptorFirst(ResponseCacheConformance.INSTANCE);
67          addResponseInterceptorLast(ResponseViaCache.INSTANCE);
68          addRequestInterceptorLast(RequestViaCache.INSTANCE);
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     /**
103      * @deprecated Do not use.
104      */
105     @Deprecated
106     public final CachingH2AsyncClientBuilder setHttpCacheInvalidator(final org.apache.hc.client5.http.cache.HttpAsyncCacheInvalidator cacheInvalidator) {
107         return this;
108     }
109 
110     public CachingH2AsyncClientBuilder setDeleteCache(final boolean deleteCache) {
111         this.deleteCache = deleteCache;
112         return this;
113     }
114 
115     @Override
116     protected void customizeExecChain(final NamedElementChain<AsyncExecChainHandler> execChainDefinition) {
117         final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
118         // We copy the instance fields to avoid changing them, and rename to avoid accidental use of the wrong version
119         ResourceFactory resourceFactoryCopy = this.resourceFactory;
120         if (resourceFactoryCopy == null) {
121             if (this.cacheDir == null) {
122                 resourceFactoryCopy = new HeapResourceFactory();
123             } else {
124                 resourceFactoryCopy = new FileResourceFactory(cacheDir);
125             }
126         }
127         HttpAsyncCacheStorage storageCopy = this.storage;
128         if (storageCopy == null) {
129             if (this.cacheDir == null) {
130                 storageCopy = new HttpAsyncCacheStorageAdaptor(new BasicHttpCacheStorage(config));
131             } else {
132                 final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
133                 if (this.deleteCache) {
134                     addCloseable(managedStorage::shutdown);
135                 } else {
136                     addCloseable(managedStorage);
137                 }
138                 storageCopy = new HttpAsyncCacheStorageAdaptor(managedStorage);
139             }
140         }
141         final HttpAsyncCache httpCache = new BasicHttpAsyncCache(
142                 resourceFactoryCopy,
143                 HttpCacheEntryFactory.INSTANCE,
144                 storageCopy,
145                 CacheKeyGenerator.INSTANCE);
146 
147         DefaultAsyncCacheRevalidator cacheRevalidator = null;
148         if (config.getAsynchronousWorkers() > 0) {
149             final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(config.getAsynchronousWorkers());
150             addCloseable(executorService::shutdownNow);
151             cacheRevalidator = new DefaultAsyncCacheRevalidator(
152                     executorService,
153                     this.schedulingStrategy != null ? this.schedulingStrategy : ImmediateSchedulingStrategy.INSTANCE);
154         }
155 
156         final AsyncCachingExec cachingExec = new AsyncCachingExec(
157                 httpCache,
158                 cacheRevalidator,
159                 config);
160         execChainDefinition.addBefore(ChainElement.PROTOCOL.name(), cachingExec, ChainElement.CACHING.name());
161     }
162 
163 }