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.async.AsyncExecChainHandler;
36  import org.apache.hc.client5.http.cache.HttpAsyncCacheInvalidator;
37  import org.apache.hc.client5.http.cache.HttpAsyncCacheStorage;
38  import org.apache.hc.client5.http.cache.HttpAsyncCacheStorageAdaptor;
39  import org.apache.hc.client5.http.cache.HttpCacheStorage;
40  import org.apache.hc.client5.http.cache.ResourceFactory;
41  import org.apache.hc.client5.http.impl.ChainElement;
42  import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
43  import org.apache.hc.client5.http.impl.schedule.ImmediateSchedulingStrategy;
44  import org.apache.hc.client5.http.schedule.SchedulingStrategy;
45  import org.apache.hc.core5.annotation.Experimental;
46  import org.apache.hc.core5.http.config.NamedElementChain;
47  
48  /**
49   * Builder for {@link org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient}
50   * instances capable of client-side caching.
51   *
52   * @since 5.0
53   */
54  @Experimental
55  public class CachingHttpAsyncClientBuilder extends HttpAsyncClientBuilder {
56  
57      private ResourceFactory resourceFactory;
58      private HttpAsyncCacheStorage storage;
59      private File cacheDir;
60      private SchedulingStrategy schedulingStrategy;
61      private CacheConfig cacheConfig;
62      private HttpAsyncCacheInvalidator httpCacheInvalidator;
63      private boolean deleteCache;
64  
65      public static CachingHttpAsyncClientBuilder create() {
66          return new CachingHttpAsyncClientBuilder();
67      }
68  
69      protected CachingHttpAsyncClientBuilder() {
70          super();
71          this.deleteCache = true;
72      }
73  
74      public final CachingHttpAsyncClientBuilder setResourceFactory(final ResourceFactory resourceFactory) {
75          this.resourceFactory = resourceFactory;
76          return this;
77      }
78  
79      public final CachingHttpAsyncClientBuilder setHttpCacheStorage(final HttpCacheStorage storage) {
80          this.storage = storage != null ? new HttpAsyncCacheStorageAdaptor(storage) : null;
81          return this;
82      }
83  
84      public final CachingHttpAsyncClientBuilder setHttpCacheStorage(final HttpAsyncCacheStorage storage) {
85          this.storage = storage;
86          return this;
87      }
88  
89      public final CachingHttpAsyncClientBuilder setCacheDir(final File cacheDir) {
90          this.cacheDir = cacheDir;
91          return this;
92      }
93  
94      public final CachingHttpAsyncClientBuilder setSchedulingStrategy(final SchedulingStrategy schedulingStrategy) {
95          this.schedulingStrategy = schedulingStrategy;
96          return this;
97      }
98  
99      public final CachingHttpAsyncClientBuilder setCacheConfig(final CacheConfig cacheConfig) {
100         this.cacheConfig = cacheConfig;
101         return this;
102     }
103 
104     public final CachingHttpAsyncClientBuilder setHttpCacheInvalidator(final HttpAsyncCacheInvalidator cacheInvalidator) {
105         this.httpCacheInvalidator = cacheInvalidator;
106         return this;
107     }
108 
109     public CachingHttpAsyncClientBuilder setDeleteCache(final boolean deleteCache) {
110         this.deleteCache = deleteCache;
111         return this;
112     }
113 
114     @Override
115     protected void customizeExecChain(final NamedElementChain<AsyncExecChainHandler> execChainDefinition) {
116         final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
117         // We copy the instance fields to avoid changing them, and rename to avoid accidental use of the wrong version
118         ResourceFactory resourceFactoryCopy = this.resourceFactory;
119         if (resourceFactoryCopy == null) {
120             if (this.cacheDir == null) {
121                 resourceFactoryCopy = new HeapResourceFactory();
122             } else {
123                 resourceFactoryCopy = new FileResourceFactory(cacheDir);
124             }
125         }
126         HttpAsyncCacheStorage storageCopy = this.storage;
127         if (storageCopy == null) {
128             if (this.cacheDir == null) {
129                 storageCopy = new HttpAsyncCacheStorageAdaptor(new BasicHttpCacheStorage(config));
130             } else {
131                 final ManagedHttpCacheStoragedHttpCacheStorage.html#ManagedHttpCacheStorage">ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
132                 if (this.deleteCache) {
133                     addCloseable(new Closeable() {
134 
135                         @Override
136                         public void close() throws IOException {
137                             managedStorage.shutdown();
138                         }
139 
140                     });
141                 } else {
142                     addCloseable(managedStorage);
143                 }
144                 storageCopy = new HttpAsyncCacheStorageAdaptor(managedStorage);
145             }
146         }
147         final HttpAsyncCache httpCache = new BasicHttpAsyncCache(
148                 resourceFactoryCopy,
149                 storageCopy,
150                 CacheKeyGenerator.INSTANCE,
151                 this.httpCacheInvalidator != null ? this.httpCacheInvalidator : new DefaultAsyncCacheInvalidator());
152 
153         DefaultAsyncCacheRevalidator cacheRevalidator = null;
154         if (config.getAsynchronousWorkers() > 0) {
155             final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(config.getAsynchronousWorkers());
156             addCloseable(new Closeable() {
157 
158                 @Override
159                 public void close() throws IOException {
160                     executorService.shutdownNow();
161                 }
162 
163             });
164             cacheRevalidator = new DefaultAsyncCacheRevalidator(
165                     executorService,
166                     this.schedulingStrategy != null ? this.schedulingStrategy : ImmediateSchedulingStrategy.INSTANCE);
167         }
168 
169         final AsyncCachingExecche/AsyncCachingExec.html#AsyncCachingExec">AsyncCachingExec cachingExec = new AsyncCachingExec(
170                 httpCache,
171                 cacheRevalidator,
172                 config);
173         execChainDefinition.addBefore(ChainElement.PROTOCOL.name(), cachingExec, ChainElement.CACHING.name());
174     }
175 
176 }