diff --git a/httpclient5-cache/pom.xml b/httpclient5-cache/pom.xml index 8e1a202cf..e6f45f1a8 100644 --- a/httpclient5-cache/pom.xml +++ b/httpclient5-cache/pom.xml @@ -56,8 +56,8 @@ test - net.sf.ehcache - ehcache-core + org.ehcache.modules + ehcache-api compile true diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java index 304add1f7..d55883789 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java @@ -46,7 +46,7 @@ import org.apache.hc.core5.http.HttpVersion; import org.apache.hc.core5.http.message.BasicHeader; /** - * Rebuilds an {@link HttpResponse} from a {@link net.sf.ehcache.CacheEntry} + * Rebuilds an {@link HttpResponse} from a {@link HttpCacheEntry} * * @since 4.1 */ diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/NoopCacheEntrySerializer.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/NoopCacheEntrySerializer.java new file mode 100644 index 000000000..76f17162a --- /dev/null +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/NoopCacheEntrySerializer.java @@ -0,0 +1,53 @@ +/* + * ==================================================================== + * 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 + * . + * + */ +package org.apache.hc.client5.http.impl.cache; + +import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer; +import org.apache.hc.client5.http.cache.HttpCacheStorageEntry; +import org.apache.hc.client5.http.cache.ResourceIOException; +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.ThreadingBehavior; + +/** + * @since 5.0 + */ +@Contract(threading = ThreadingBehavior.IMMUTABLE) +public class NoopCacheEntrySerializer implements HttpCacheEntrySerializer { + + public static final NoopCacheEntrySerializer INSTANCE = new NoopCacheEntrySerializer(); + + @Override + public HttpCacheStorageEntry serialize(final HttpCacheStorageEntry cacheEntry) throws ResourceIOException { + return cacheEntry; + } + + @Override + public HttpCacheStorageEntry deserialize(final HttpCacheStorageEntry cacheEntry) throws ResourceIOException { + return cacheEntry; + } + +} diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ehcache/EhcacheHttpCacheStorage.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ehcache/EhcacheHttpCacheStorage.java index 36cc4c269..c3d7a38a8 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ehcache/EhcacheHttpCacheStorage.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ehcache/EhcacheHttpCacheStorage.java @@ -27,14 +27,14 @@ package org.apache.hc.client5.http.impl.cache.ehcache; import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer; +import org.apache.hc.client5.http.cache.HttpCacheStorageEntry; import org.apache.hc.client5.http.cache.ResourceIOException; -import org.apache.hc.client5.http.impl.cache.AbstractBinaryCacheStorage; -import org.apache.hc.client5.http.impl.cache.CacheConfig; +import org.apache.hc.client5.http.impl.cache.AbstractSerializingCacheStorage; import org.apache.hc.client5.http.impl.cache.ByteArrayCacheEntrySerializer; +import org.apache.hc.client5.http.impl.cache.CacheConfig; +import org.apache.hc.client5.http.impl.cache.NoopCacheEntrySerializer; import org.apache.hc.core5.util.Args; - -import net.sf.ehcache.Ehcache; -import net.sf.ehcache.Element; +import org.ehcache.Cache; /** *

This class is a storage backend for cache entries that uses the @@ -53,19 +53,30 @@ import net.sf.ehcache.Element; * itself.

* @since 4.1 */ -public class EhcacheHttpCacheStorage extends AbstractBinaryCacheStorage { - - private final Ehcache cache; +public class EhcacheHttpCacheStorage extends AbstractSerializingCacheStorage { /** - * Constructs a storage backend using the provided Ehcache - * with default configuration options. - * @param cache where to store cached origin responses + * Creates cache that stores {@link HttpCacheStorageEntry}s without direct serialization. + * + * @since 5.0 */ - public EhcacheHttpCacheStorage(final Ehcache cache){ - this(cache, CacheConfig.DEFAULT, ByteArrayCacheEntrySerializer.INSTANCE); + public static EhcacheHttpCacheStorage createObjectCache( + final Cache cache, final CacheConfig config) { + return new EhcacheHttpCacheStorage<>(cache, config, NoopCacheEntrySerializer.INSTANCE); } + /** + * Creates cache that stores serialized {@link HttpCacheStorageEntry}s. + * + * @since 5.0 + */ + public static EhcacheHttpCacheStorage createSerializedCache( + final Cache cache, final CacheConfig config) { + return new EhcacheHttpCacheStorage<>(cache, config, ByteArrayCacheEntrySerializer.INSTANCE); + } + + private final Cache cache; + /** * Constructs a storage backend using the provided Ehcache * with the given configuration options, but using an alternative @@ -77,9 +88,9 @@ public class EhcacheHttpCacheStorage extends AbstractBinaryCacheStorage * @param serializer alternative serialization mechanism */ public EhcacheHttpCacheStorage( - final Ehcache cache, + final Cache cache, final CacheConfig config, - final HttpCacheEntrySerializer serializer) { + final HttpCacheEntrySerializer serializer) { super((config != null ? config : CacheConfig.DEFAULT).getMaxUpdateRetries(), serializer); this.cache = Args.notNull(cache, "Ehcache"); } @@ -90,41 +101,29 @@ public class EhcacheHttpCacheStorage extends AbstractBinaryCacheStorage } @Override - protected void store(final String storageKey, final byte[] storageObject) throws ResourceIOException { - cache.put(new Element(storageKey, storageKey)); - } - - private byte[] castAsByteArray(final Object storageObject) throws ResourceIOException { - if (storageObject == null) { - return null; - } - if (storageObject instanceof byte[]) { - return (byte[]) storageObject; - } else { - throw new ResourceIOException("Unexpected cache content: " + storageObject.getClass()); - } + protected void store(final String storageKey, final T storageObject) throws ResourceIOException { + cache.put(storageKey, storageObject); } @Override - protected byte[] restore(final String storageKey) throws ResourceIOException { - final Element element = cache.get(storageKey); - return element != null ? castAsByteArray(element.getObjectValue()) : null; - } - - @Override - protected Element getForUpdateCAS(final String storageKey) throws ResourceIOException { + protected T restore(final String storageKey) throws ResourceIOException { return cache.get(storageKey); } @Override - protected byte[] getStorageObject(final Element element) throws ResourceIOException { - return castAsByteArray(element.getObjectValue()); + protected T getForUpdateCAS(final String storageKey) throws ResourceIOException { + return cache.get(storageKey); } @Override - protected boolean updateCAS(final String storageKey, final Element element, final byte[] storageObject) throws ResourceIOException { - final Element newElement = new Element(storageKey, storageObject); - return cache.replace(element, newElement); + protected T getStorageObject(final T element) throws ResourceIOException { + return element; + } + + @Override + protected boolean updateCAS( + final String storageKey, final T oldStorageObject, final T storageObject) throws ResourceIOException { + return cache.replace(storageKey, oldStorageObject, storageObject); } @Override diff --git a/httpclient5-osgi/pom.xml b/httpclient5-osgi/pom.xml index 2912a936c..54b33d99d 100644 --- a/httpclient5-osgi/pom.xml +++ b/httpclient5-osgi/pom.xml @@ -136,7 +136,7 @@ org.osgi.service.cm, org.apache.logging.log4j;version=${log4j.osgi.import.version}, org.apache.hc.core5.*;version=${httpcore.osgi.import.version}, - net.sf.ehcache.*;resolution:=optional, + org.ehcache.*;resolution:=optional, net.spy.memcached.*;resolution:=optional org.apache.hc.client5.http.osgi.impl.HttpProxyConfigurationActivator diff --git a/pom.xml b/pom.xml index 55161b402..dfbc3660e 100644 --- a/pom.xml +++ b/pom.xml @@ -71,7 +71,7 @@ 5.0-alpha4 2.8.2 1.10 - 2.6.11 + 3.4.0 2.12.0 1.7.13 4.12 @@ -121,8 +121,8 @@ ${commons-codec.version} - net.sf.ehcache - ehcache-core + org.ehcache.modules + ehcache-api ${ehcache.version}