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