Added caching specific HttpClient builder

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1420121 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-12-11 12:20:08 +00:00
parent b257aeec70
commit 034805c3af
6 changed files with 160 additions and 3 deletions

View File

@ -118,7 +118,7 @@ import org.apache.http.util.VersionInfo;
* </p>
* @since 4.1
*
* @deprecated (4.3)
* @deprecated (4.3) use {@link CachingHttpClientBuilder} or {@link CachingHttpClients}.
*/
@Deprecated
@ThreadSafe // So long as the responseCache implementation is threadsafe

View File

@ -0,0 +1,97 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.client.cache;
import java.io.File;
import org.apache.http.client.cache.HttpCacheStorage;
import org.apache.http.client.cache.ResourceFactory;
import org.apache.http.impl.client.builder.HttpClientBuilder;
import org.apache.http.impl.client.execchain.ClientExecChain;
/**
* @since (4.3)
*/
public class CachingHttpClientBuilder extends HttpClientBuilder {
private ResourceFactory resourceFactory;
private HttpCacheStorage storage;
private File cacheDir;
private CacheConfig cacheConfig;
public static CachingHttpClientBuilder create() {
return new CachingHttpClientBuilder();
}
protected CachingHttpClientBuilder() {
super();
}
public final CachingHttpClientBuilder setResourceFactory(
final ResourceFactory resourceFactory) {
this.resourceFactory = resourceFactory;
return this;
}
public final CachingHttpClientBuilder setHttpCacheStorage(
final HttpCacheStorage storage) {
this.storage = storage;
return this;
}
public final CachingHttpClientBuilder setCacheDir(
final File cacheDir) {
this.cacheDir = cacheDir;
return this;
}
public final CachingHttpClientBuilder setCacheConfig(
final CacheConfig cacheConfig) {
this.cacheConfig = cacheConfig;
return this;
}
@Override
protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) {
CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
ResourceFactory resourceFactory = this.resourceFactory;
if (resourceFactory == null) {
if (this.cacheDir == null) {
resourceFactory = new HeapResourceFactory();
} else {
resourceFactory = new FileResourceFactory(cacheDir);
}
}
HttpCacheStorage storage = this.storage;
if (storage == null) {
storage = new BasicHttpCacheStorage(cacheConfig);
}
return new CachingExec(mainExec,
new BasicHttpCache(resourceFactory, storage, config), config);
}
}

View File

@ -0,0 +1,57 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.client.cache;
import java.io.File;
import org.apache.http.annotation.Immutable;
import org.apache.http.impl.client.CloseableHttpClient;
/**
* @since 4.3
*/
@Immutable
public class CachingHttpClients {
private CachingHttpClients() {
super();
}
public static CachingHttpClientBuilder custom() {
return CachingHttpClientBuilder.create();
}
public static CloseableHttpClient createMemoryBound() {
return CachingHttpClientBuilder.create().build();
}
public static CloseableHttpClient createFileBound(final File cacheDir) {
return CachingHttpClientBuilder.create().setCacheDir(cacheDir).build();
}
}

View File

@ -32,7 +32,6 @@ import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.util.EncodingUtils;

View File

@ -48,4 +48,8 @@ public class HttpClients {
return HttpClientBuilder.create().build();
}
public static CloseableHttpClient createSystem() {
return HttpClientBuilder.create().useSystemProperties().build();
}
}

View File

@ -191,7 +191,7 @@ public class HttpClientBuilder {
return new HttpClientBuilder();
}
HttpClientBuilder() {
protected HttpClientBuilder() {
super();
}