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.http.impl.client.cache;
28  
29  import java.io.File;
30  
31  import org.apache.http.client.cache.HttpCacheInvalidator;
32  import org.apache.http.client.cache.HttpCacheStorage;
33  import org.apache.http.client.cache.ResourceFactory;
34  import org.apache.http.impl.client.HttpClientBuilder;
35  import org.apache.http.impl.execchain.ClientExecChain;
36  
37  /**
38   * Builder for {@link org.apache.http.impl.client.CloseableHttpClient}
39   * instances capable of client-side caching.
40   *
41   * @since 4.3
42   */
43  public class CachingHttpClientBuilder extends HttpClientBuilder {
44  
45      private ResourceFactory resourceFactory;
46      private HttpCacheStorage storage;
47      private File cacheDir;
48      private CacheConfig cacheConfig;
49      private SchedulingStrategy schedulingStrategy;
50      private HttpCacheInvalidator httpCacheInvalidator;
51  
52      public static CachingHttpClientBuilder create() {
53          return new CachingHttpClientBuilder();
54      }
55  
56      protected CachingHttpClientBuilder() {
57          super();
58      }
59  
60      public final CachingHttpClientBuilder setResourceFactory(
61              final ResourceFactory resourceFactory) {
62          this.resourceFactory = resourceFactory;
63          return this;
64      }
65  
66      public final CachingHttpClientBuilder setHttpCacheStorage(
67              final HttpCacheStorage storage) {
68          this.storage = storage;
69          return this;
70      }
71  
72      public final CachingHttpClientBuilder setCacheDir(
73              final File cacheDir) {
74          this.cacheDir = cacheDir;
75          return this;
76      }
77  
78      public final CachingHttpClientBuilder setCacheConfig(
79              final CacheConfig cacheConfig) {
80          this.cacheConfig = cacheConfig;
81          return this;
82      }
83  
84      public final CachingHttpClientBuilder setSchedulingStrategy(
85              final SchedulingStrategy schedulingStrategy) {
86          this.schedulingStrategy = schedulingStrategy;
87          return this;
88      }
89  
90      public final CachingHttpClientBuilder setHttpCacheInvalidator(
91              final HttpCacheInvalidator cacheInvalidator) {
92          this.httpCacheInvalidator = cacheInvalidator;
93          return this;
94      }
95  
96      @Override
97      protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) {
98          final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
99          ResourceFactory resourceFactory = this.resourceFactory;
100         if (resourceFactory == null) {
101             if (this.cacheDir == null) {
102                 resourceFactory = new HeapResourceFactory();
103             } else {
104                 resourceFactory = new FileResourceFactory(cacheDir);
105             }
106         }
107         HttpCacheStorage storage = this.storage;
108         if (storage == null) {
109             if (this.cacheDir == null) {
110                 storage = new BasicHttpCacheStorage(config);
111             } else {
112                 final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
113                 addCloseable(managedStorage);
114                 storage = managedStorage;
115             }
116         }
117         final AsynchronousValidator revalidator = createAsynchronousRevalidator(config);
118         final CacheKeyGenerator uriExtractor = new CacheKeyGenerator();
119 
120         HttpCacheInvalidator cacheInvalidator = this.httpCacheInvalidator;
121         if (cacheInvalidator == null) {
122             cacheInvalidator = new CacheInvalidator(uriExtractor, storage);
123         }
124 
125         return new CachingExec(mainExec,
126                 new BasicHttpCache(
127                         resourceFactory,
128                         storage, config,
129                         uriExtractor,
130                         cacheInvalidator), config, revalidator);
131     }
132 
133     private AsynchronousValidator createAsynchronousRevalidator(final CacheConfig config) {
134         if (config.getAsynchronousWorkersMax() > 0) {
135             final SchedulingStrategy configuredSchedulingStrategy = createSchedulingStrategy(config);
136             final AsynchronousValidator revalidator = new AsynchronousValidator(
137                     configuredSchedulingStrategy);
138             addCloseable(revalidator);
139             return revalidator;
140         }
141         return null;
142     }
143 
144     @SuppressWarnings("resource")
145     private SchedulingStrategy createSchedulingStrategy(final CacheConfig config) {
146         return schedulingStrategy != null ? schedulingStrategy : new ImmediateSchedulingStrategy(config);
147     }
148 
149 }