Revised HTTP cache exception handling

This commit is contained in:
Oleg Kalnichevski 2017-10-12 15:05:32 +02:00
parent b24aa01866
commit bb96781e5b
21 changed files with 136 additions and 192 deletions

View File

@ -1,48 +0,0 @@
/*
* ====================================================================
* 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.hc.client5.http.cache;
import java.io.IOException;
/**
* Thrown if serialization or deserialization of an {@link HttpCacheEntry}
* fails.
*/
public class HttpCacheEntrySerializationException extends IOException {
private static final long serialVersionUID = 9219188365878433519L;
public HttpCacheEntrySerializationException(final String message) {
super();
}
public HttpCacheEntrySerializationException(final String message, final Throwable cause) {
super(message);
initCause(cause);
}
}

View File

@ -26,7 +26,6 @@
*/
package org.apache.hc.client5.http.cache;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -40,15 +39,15 @@ public interface HttpCacheEntrySerializer {
/**
* Serializes the given entry to a byte representation on the
* given {@link OutputStream}.
* @throws IOException
* @throws ResourceIOException
*/
void writeTo(HttpCacheEntry entry, OutputStream os) throws IOException;
void writeTo(HttpCacheEntry entry, OutputStream os) throws ResourceIOException;
/**
* Deserializes a byte representation of a cache entry by reading
* from the given {@link InputStream}.
* @throws IOException
* @throws ResourceIOException
*/
HttpCacheEntry readFrom(InputStream is) throws IOException;
HttpCacheEntry readFrom(InputStream is) throws ResourceIOException;
}

View File

@ -26,8 +26,6 @@
*/
package org.apache.hc.client5.http.cache;
import java.io.IOException;
/**
* New storage backends should implement this {@link HttpCacheStorage}
* interface. They can then be plugged into the existing caching
@ -41,9 +39,9 @@ public interface HttpCacheStorage {
* Store a given cache entry under the given key.
* @param key where in the cache to store the entry
* @param entry cached response to store
* @throws IOException
* @throws ResourceIOException
*/
void putEntry(String key, HttpCacheEntry entry) throws IOException;
void putEntry(String key, HttpCacheEntry entry) throws ResourceIOException;
/**
* Retrieves the cache entry stored under the given key
@ -51,17 +49,17 @@ public interface HttpCacheStorage {
* @param key cache key
* @return an {@link HttpCacheEntry} or {@code null} if no
* entry exists
* @throws IOException
* @throws ResourceIOException
*/
HttpCacheEntry getEntry(String key) throws IOException;
HttpCacheEntry getEntry(String key) throws ResourceIOException;
/**
* Deletes/invalidates/removes any cache entries currently
* stored under the given key.
* @param key
* @throws IOException
* @throws ResourceIOException
*/
void removeEntry(String key) throws IOException;
void removeEntry(String key) throws ResourceIOException;
/**
* Atomically applies the given callback to processChallenge an existing cache
@ -71,10 +69,10 @@ public interface HttpCacheStorage {
* {@link HttpCacheUpdateCallback} for details, but roughly the
* callback expects to be handed the current entry and will return
* the new value for the entry.
* @throws IOException
* @throws ResourceIOException
* @throws HttpCacheUpdateException
*/
void updateEntry(
String key, HttpCacheUpdateCallback callback) throws IOException, HttpCacheUpdateException;
String key, HttpCacheUpdateCallback callback) throws ResourceIOException, HttpCacheUpdateException;
}

View File

@ -26,8 +26,6 @@
*/
package org.apache.hc.client5.http.cache;
import java.io.IOException;
/**
* Used for atomically updating entries in a {@link HttpCacheStorage}
* implementation. The current entry (if any) is fed into an implementation
@ -47,6 +45,6 @@ public interface HttpCacheUpdateCallback {
*
* @since 4.1
*/
HttpCacheEntry update(HttpCacheEntry existing) throws IOException;
HttpCacheEntry update(HttpCacheEntry existing) throws ResourceIOException;
}

View File

@ -26,7 +26,6 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
@ -42,6 +41,7 @@ import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
import org.apache.hc.client5.http.cache.HttpCacheUpdateException;
import org.apache.hc.client5.http.cache.Resource;
import org.apache.hc.client5.http.cache.ResourceFactory;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
@ -96,8 +96,7 @@ class BasicHttpCache implements HttpCache {
}
@Override
public void flushCacheEntriesFor(final HttpHost host, final HttpRequest request)
throws IOException {
public void flushCacheEntriesFor(final HttpHost host, final HttpRequest request) throws ResourceIOException {
if (!safeRequestMethods.contains(request.getMethod())) {
final String uri = uriExtractor.generateKey(host, request);
storage.removeEntry(uri);
@ -112,7 +111,7 @@ class BasicHttpCache implements HttpCache {
}
void storeInCache(
final HttpHost target, final HttpRequest request, final HttpCacheEntry entry) throws IOException {
final HttpHost target, final HttpRequest request, final HttpCacheEntry entry) throws ResourceIOException {
if (entry.hasVariants()) {
storeVariantEntry(target, request, entry);
} else {
@ -121,7 +120,7 @@ class BasicHttpCache implements HttpCache {
}
void storeNonVariantEntry(
final HttpHost target, final HttpRequest req, final HttpCacheEntry entry) throws IOException {
final HttpHost target, final HttpRequest req, final HttpCacheEntry entry) throws ResourceIOException {
final String uri = uriExtractor.generateKey(target, req);
storage.putEntry(uri, entry);
}
@ -129,7 +128,7 @@ class BasicHttpCache implements HttpCache {
void storeVariantEntry(
final HttpHost target,
final HttpRequest req,
final HttpCacheEntry entry) throws IOException {
final HttpCacheEntry entry) throws ResourceIOException {
final String parentURI = uriExtractor.generateKey(target, req);
final String variantURI = uriExtractor.generateVariantURI(target, req, entry);
storage.putEntry(variantURI, entry);
@ -137,7 +136,7 @@ class BasicHttpCache implements HttpCache {
final HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback() {
@Override
public HttpCacheEntry update(final HttpCacheEntry existing) throws IOException {
public HttpCacheEntry update(final HttpCacheEntry existing) throws ResourceIOException {
return doGetUpdatedParentEntry(
req.getRequestUri(), existing, entry,
uriExtractor.generateVariantKey(req, entry),
@ -154,8 +153,8 @@ class BasicHttpCache implements HttpCache {
}
@Override
public void reuseVariantEntryFor(final HttpHost target, final HttpRequest req,
final Variant variant) throws IOException {
public void reuseVariantEntryFor(
final HttpHost target, final HttpRequest req, final Variant variant) throws ResourceIOException {
final String parentCacheKey = uriExtractor.generateKey(target, req);
final HttpCacheEntry entry = variant.getEntry();
final String variantKey = uriExtractor.generateVariantKey(req, entry);
@ -164,7 +163,7 @@ class BasicHttpCache implements HttpCache {
final HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback() {
@Override
public HttpCacheEntry update(final HttpCacheEntry existing)
throws IOException {
throws ResourceIOException {
return doGetUpdatedParentEntry(req.getRequestUri(),
existing, entry, variantKey, variantCacheKey);
}
@ -182,7 +181,7 @@ class BasicHttpCache implements HttpCache {
final HttpCacheEntry existing,
final HttpCacheEntry entry,
final String variantKey,
final String variantCacheKey) throws IOException {
final String variantCacheKey) throws ResourceIOException {
HttpCacheEntry src = existing;
if (src == null) {
src = entry;
@ -204,9 +203,13 @@ class BasicHttpCache implements HttpCache {
}
@Override
public HttpCacheEntry updateCacheEntry(final HttpHost target, final HttpRequest request,
final HttpCacheEntry stale, final HttpResponse originResponse,
final Date requestSent, final Date responseReceived) throws IOException {
public HttpCacheEntry updateCacheEntry(
final HttpHost target,
final HttpRequest request,
final HttpCacheEntry stale,
final HttpResponse originResponse,
final Date requestSent,
final Date responseReceived) throws ResourceIOException {
final HttpCacheEntry updatedEntry = cacheEntryUpdater.updateCacheEntry(
request.getRequestUri(),
stale,
@ -220,7 +223,7 @@ class BasicHttpCache implements HttpCache {
@Override
public HttpCacheEntry updateVariantCacheEntry(final HttpHost target, final HttpRequest request,
final HttpCacheEntry stale, final HttpResponse originResponse,
final Date requestSent, final Date responseReceived, final String cacheKey) throws IOException {
final Date requestSent, final Date responseReceived, final String cacheKey) throws ResourceIOException {
final HttpCacheEntry updatedEntry = cacheEntryUpdater.updateCacheEntry(
request.getRequestUri(),
stale,
@ -237,7 +240,7 @@ class BasicHttpCache implements HttpCache {
final HttpResponse originResponse,
final ByteArrayBuffer content,
final Date requestSent,
final Date responseReceived) throws IOException {
final Date responseReceived) throws ResourceIOException {
final Resource resource;
if (content != null) {
resource = resourceFactory.generate(request.getRequestUri(), content.array(), 0, content.length());
@ -255,7 +258,7 @@ class BasicHttpCache implements HttpCache {
}
@Override
public HttpCacheEntry getCacheEntry(final HttpHost host, final HttpRequest request) throws IOException {
public HttpCacheEntry getCacheEntry(final HttpHost host, final HttpRequest request) throws ResourceIOException {
final HttpCacheEntry root = storage.getEntry(uriExtractor.generateKey(host, request));
if (root == null) {
return null;
@ -272,13 +275,13 @@ class BasicHttpCache implements HttpCache {
@Override
public void flushInvalidatedCacheEntriesFor(final HttpHost host,
final HttpRequest request) throws IOException {
final HttpRequest request) throws ResourceIOException {
cacheInvalidator.flushInvalidatedCacheEntries(host, request);
}
@Override
public Map<String, Variant> getVariantCacheEntriesWithEtags(final HttpHost host, final HttpRequest request)
throws IOException {
throws ResourceIOException {
final Map<String,Variant> variants = new HashMap<>();
final HttpCacheEntry root = storage.getEntry(uriExtractor.generateKey(host, request));
if (root == null || !root.hasVariants()) {
@ -294,7 +297,7 @@ class BasicHttpCache implements HttpCache {
private void addVariantWithEtag(final String variantKey,
final String variantCacheKey, final Map<String, Variant> variants)
throws IOException {
throws ResourceIOException {
final HttpCacheEntry entry = storage.getEntry(variantCacheKey);
if (entry == null) {
return;

View File

@ -26,11 +26,10 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.io.IOException;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.HttpCacheStorage;
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
@ -63,7 +62,8 @@ public class BasicHttpCacheStorage implements HttpCacheStorage {
* HttpCacheEntry to place in the cache
*/
@Override
public synchronized void putEntry(final String url, final HttpCacheEntry entry) throws IOException {
public synchronized void putEntry(
final String url, final HttpCacheEntry entry) throws ResourceIOException {
entries.put(url, entry);
}
@ -75,7 +75,7 @@ public class BasicHttpCacheStorage implements HttpCacheStorage {
* @return HttpCacheEntry if one exists, or null for cache miss
*/
@Override
public synchronized HttpCacheEntry getEntry(final String url) throws IOException {
public synchronized HttpCacheEntry getEntry(final String url) throws ResourceIOException {
return entries.get(url);
}
@ -86,14 +86,14 @@ public class BasicHttpCacheStorage implements HttpCacheStorage {
* Url that is the cache key
*/
@Override
public synchronized void removeEntry(final String url) throws IOException {
public synchronized void removeEntry(final String url) throws ResourceIOException {
entries.remove(url);
}
@Override
public synchronized void updateEntry(
final String url,
final HttpCacheUpdateCallback callback) throws IOException {
final HttpCacheUpdateCallback callback) throws ResourceIOException {
final HttpCacheEntry existingEntry = entries.get(url);
entries.put(url, callback.update(existingEntry));
}

View File

@ -26,7 +26,6 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
@ -37,6 +36,7 @@ import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.Resource;
import org.apache.hc.client5.http.cache.ResourceFactory;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
@ -77,14 +77,14 @@ class CacheEntryUpdater {
* @param responseDate When the response was gotten
* @param response The HttpResponse from the backend server call
* @return HttpCacheEntry an updated version of the cache entry
* @throws java.io.IOException if something bad happens while trying to read the body from the original entry
* @throws ResourceIOException if something bad happens while trying to read the body from the original entry
*/
public HttpCacheEntry updateCacheEntry(
final String requestId,
final HttpCacheEntry entry,
final Date requestDate,
final Date responseDate,
final HttpResponse response) throws IOException {
final HttpResponse response) throws ResourceIOException {
Args.check(response.getCode() == HttpStatus.SC_NOT_MODIFIED,
"Response must have 304 status code");
final Header[] mergedHeaders = mergeHeaders(entry, response);

View File

@ -26,13 +26,13 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.io.IOException;
import java.util.Date;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.Resource;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
@ -67,7 +67,7 @@ class CachedHttpResponseGenerator {
* @param entry {@link HttpCacheEntry} to transform into an {@link HttpResponse}
* @return {@link SimpleHttpResponse} constructed response
*/
SimpleHttpResponse generateResponse(final HttpRequest request, final HttpCacheEntry entry) throws IOException {
SimpleHttpResponse generateResponse(final HttpRequest request, final HttpCacheEntry entry) throws ResourceIOException {
final Date now = new Date();
final SimpleHttpResponse response = new SimpleHttpResponse(entry.getStatus());
response.setVersion(HttpVersion.DEFAULT);

View File

@ -33,8 +33,8 @@ import java.io.ObjectOutputStream;
import java.io.OutputStream;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.HttpCacheEntrySerializationException;
import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
@ -50,18 +50,20 @@ import org.apache.hc.core5.annotation.ThreadingBehavior;
public class DefaultHttpCacheEntrySerializer implements HttpCacheEntrySerializer {
@Override
public void writeTo(final HttpCacheEntry cacheEntry, final OutputStream os) throws IOException {
try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
public void writeTo(final HttpCacheEntry cacheEntry, final OutputStream os) throws ResourceIOException {
try (final ObjectOutputStream oos = new ObjectOutputStream(os)) {
oos.writeObject(cacheEntry);
} catch (final IOException ex) {
throw new ResourceIOException(ex.getMessage(), ex);
}
}
@Override
public HttpCacheEntry readFrom(final InputStream is) throws IOException {
try (ObjectInputStream ois = new ObjectInputStream(is)) {
public HttpCacheEntry readFrom(final InputStream is) throws ResourceIOException {
try (final ObjectInputStream ois = new ObjectInputStream(is)) {
return (HttpCacheEntry) ois.readObject();
} catch (final ClassNotFoundException ex) {
throw new HttpCacheEntrySerializationException("Class not found: " + ex.getMessage(), ex);
} catch (final IOException | ClassNotFoundException ex) {
throw new ResourceIOException(ex.getMessage(), ex);
}
}

View File

@ -26,11 +26,11 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
@ -45,19 +45,17 @@ interface HttpCache {
* Clear all matching {@link HttpCacheEntry}s.
* @param host
* @param request
* @throws IOException
* @throws ResourceIOException
*/
void flushCacheEntriesFor(HttpHost host, HttpRequest request)
throws IOException;
void flushCacheEntriesFor(HttpHost host, HttpRequest request) throws ResourceIOException;
/**
* Clear invalidated matching {@link HttpCacheEntry}s
* @param host
* @param request
* @throws IOException
* @throws ResourceIOException
*/
void flushInvalidatedCacheEntriesFor(HttpHost host, HttpRequest request)
throws IOException;
void flushInvalidatedCacheEntriesFor(HttpHost host, HttpRequest request) throws ResourceIOException;
/** Clear any entries that may be invalidated by the given response to
* a particular request.
@ -65,18 +63,16 @@ interface HttpCache {
* @param request
* @param response
*/
void flushInvalidatedCacheEntriesFor(HttpHost host, HttpRequest request,
HttpResponse response);
void flushInvalidatedCacheEntriesFor(HttpHost host, HttpRequest request, HttpResponse response);
/**
* Retrieve matching {@link HttpCacheEntry} from the cache if it exists
* @param host
* @param request
* @return the matching {@link HttpCacheEntry} or {@code null}
* @throws IOException
* @throws ResourceIOException
*/
HttpCacheEntry getCacheEntry(HttpHost host, HttpRequest request)
throws IOException;
HttpCacheEntry getCacheEntry(HttpHost host, HttpRequest request) throws ResourceIOException;
/**
* Retrieve all variants from the cache, if there are no variants then an empty
@ -84,10 +80,9 @@ interface HttpCache {
* @param host
* @param request
* @return a {@code Map} mapping Etags to variant cache entries
* @throws IOException
* @throws ResourceIOException
*/
Map<String,Variant> getVariantCacheEntriesWithEtags(HttpHost host, HttpRequest request)
throws IOException;
Map<String,Variant> getVariantCacheEntriesWithEtags(HttpHost host, HttpRequest request) throws ResourceIOException;
/**
* Store a {@link HttpResponse} in the cache if possible, and return
@ -98,7 +93,7 @@ interface HttpCache {
* @param requestSent
* @param responseReceived
* @return new {@link HttpCacheEntry}
* @throws IOException
* @throws ResourceIOException
*/
HttpCacheEntry createCacheEntry(
HttpHost host,
@ -106,8 +101,7 @@ interface HttpCache {
HttpResponse originResponse,
ByteArrayBuffer content,
Date requestSent,
Date responseReceived)
throws IOException;
Date responseReceived) throws ResourceIOException;
/**
* Update a {@link HttpCacheEntry} using a 304 {@link HttpResponse}.
@ -118,12 +112,15 @@ interface HttpCache {
* @param requestSent
* @param responseReceived
* @return the updated {@link HttpCacheEntry}
* @throws IOException
* @throws ResourceIOException
*/
HttpCacheEntry updateCacheEntry(
HttpHost target, HttpRequest request, HttpCacheEntry stale, HttpResponse originResponse,
Date requestSent, Date responseReceived)
throws IOException;
HttpHost target,
HttpRequest request,
HttpCacheEntry stale,
HttpResponse originResponse,
Date requestSent,
Date responseReceived) throws ResourceIOException;
/**
* Update a specific {@link HttpCacheEntry} representing a cached variant
@ -136,12 +133,16 @@ interface HttpCache {
* @param responseReceived when the validating response was received
* @param cacheKey where in the cache this entry is currently stored
* @return the updated {@link HttpCacheEntry}
* @throws IOException
* @throws ResourceIOException
*/
HttpCacheEntry updateVariantCacheEntry(HttpHost target, HttpRequest request,
HttpCacheEntry stale, HttpResponse originResponse, Date requestSent,
Date responseReceived, String cacheKey)
throws IOException;
HttpCacheEntry updateVariantCacheEntry(
HttpHost target,
HttpRequest request,
HttpCacheEntry stale,
HttpResponse originResponse,
Date requestSent,
Date responseReceived,
String cacheKey) throws ResourceIOException;
/**
* Specifies cache should reuse the given cached variant to satisfy
@ -149,8 +150,10 @@ interface HttpCache {
* @param target host of the upstream client request
* @param req request sent by upstream client
* @param variant variant cache entry to reuse
* @throws IOException may be thrown during cache processChallenge
* @throws ResourceIOException may be thrown during cache processChallenge
*/
void reuseVariantEntryFor(HttpHost target, final HttpRequest req,
final Variant variant) throws IOException;
void reuseVariantEntryFor(
HttpHost target,
HttpRequest req,
Variant variant) throws ResourceIOException;
}

View File

@ -27,7 +27,6 @@
package org.apache.hc.client5.http.impl.cache;
import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.util.HashSet;
import java.util.Set;
@ -37,6 +36,7 @@ import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.HttpCacheStorage;
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
import org.apache.hc.client5.http.cache.Resource;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.util.Args;
@ -103,7 +103,7 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
}
@Override
public void putEntry(final String url, final HttpCacheEntry entry) throws IOException {
public void putEntry(final String url, final HttpCacheEntry entry) throws ResourceIOException {
Args.notNull(url, "URL");
Args.notNull(entry, "Cache entry");
ensureValidState();
@ -114,7 +114,7 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
}
@Override
public HttpCacheEntry getEntry(final String url) throws IOException {
public HttpCacheEntry getEntry(final String url) throws ResourceIOException {
Args.notNull(url, "URL");
ensureValidState();
synchronized (this) {
@ -123,7 +123,7 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
}
@Override
public void removeEntry(final String url) throws IOException {
public void removeEntry(final String url) throws ResourceIOException {
Args.notNull(url, "URL");
ensureValidState();
synchronized (this) {
@ -136,7 +136,7 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
@Override
public void updateEntry(
final String url,
final HttpCacheUpdateCallback callback) throws IOException {
final HttpCacheUpdateCallback callback) throws ResourceIOException {
Args.notNull(url, "URL");
Args.notNull(callback, "Callback");
ensureValidState();

View File

@ -28,13 +28,13 @@ package org.apache.hc.client5.http.impl.cache.ehcache;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
import org.apache.hc.client5.http.cache.HttpCacheStorage;
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
import org.apache.hc.client5.http.cache.HttpCacheUpdateException;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.client5.http.impl.cache.CacheConfig;
import org.apache.hc.client5.http.impl.cache.DefaultHttpCacheEntrySerializer;
@ -102,14 +102,14 @@ public class EhcacheHttpCacheStorage implements HttpCacheStorage {
}
@Override
public synchronized void putEntry(final String key, final HttpCacheEntry entry) throws IOException {
public synchronized void putEntry(final String key, final HttpCacheEntry entry) throws ResourceIOException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
serializer.writeTo(entry, bos);
cache.put(new Element(key, bos.toByteArray()));
}
@Override
public synchronized HttpCacheEntry getEntry(final String key) throws IOException {
public synchronized HttpCacheEntry getEntry(final String key) throws ResourceIOException {
final Element e = cache.get(key);
if(e == null){
return null;
@ -125,8 +125,8 @@ public class EhcacheHttpCacheStorage implements HttpCacheStorage {
}
@Override
public synchronized void updateEntry(final String key, final HttpCacheUpdateCallback callback)
throws IOException, HttpCacheUpdateException {
public synchronized void updateEntry(
final String key, final HttpCacheUpdateCallback callback) throws ResourceIOException, HttpCacheUpdateException {
int numRetries = 0;
do{
final Element oldElement = cache.get(key);

View File

@ -39,8 +39,9 @@ public interface MemcachedCacheEntry {
/**
* Returns a serialized representation of the current cache entry.
*/
byte[] toByteArray();
* @throws MemcachedSerializationException if serialization fails.
* */
byte[] toByteArray() throws MemcachedSerializationException;
/**
* Returns the storage key associated with this entry. May return
@ -71,6 +72,6 @@ public interface MemcachedCacheEntry {
* fails. In this case, the prior values for {{@link #getStorageKey()}
* and {@link #getHttpCacheEntry()} should remain unchanged.
*/
void set(byte[] bytes);
void set(byte[] bytes) throws MemcachedSerializationException ;
}

View File

@ -56,7 +56,7 @@ public class MemcachedCacheEntryImpl implements MemcachedCacheEntry {
* @see org.apache.http.impl.client.cache.memcached.MemcachedCacheEntry#toByteArray()
*/
@Override
synchronized public byte[] toByteArray() {
synchronized public byte[] toByteArray() throws MemcachedSerializationException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ObjectOutputStream oos;
try {
@ -90,7 +90,7 @@ public class MemcachedCacheEntryImpl implements MemcachedCacheEntry {
* @see org.apache.http.impl.client.cache.memcached.MemcachedCacheEntry#set(byte[])
*/
@Override
synchronized public void set(final byte[] bytes) {
synchronized public void set(final byte[] bytes) throws MemcachedSerializationException {
final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
final ObjectInputStream ois;
final String s;

View File

@ -33,6 +33,7 @@ import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.HttpCacheStorage;
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
import org.apache.hc.client5.http.cache.HttpCacheUpdateException;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.client5.http.impl.cache.CacheConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -142,7 +143,7 @@ public class MemcachedHttpCacheStorage implements HttpCacheStorage {
}
@Override
public void putEntry(final String url, final HttpCacheEntry entry) throws IOException {
public void putEntry(final String url, final HttpCacheEntry entry) throws ResourceIOException {
final byte[] bytes = serializeEntry(url, entry);
final String key = getCacheKey(url);
if (key == null) {
@ -163,15 +164,9 @@ public class MemcachedHttpCacheStorage implements HttpCacheStorage {
}
}
private byte[] serializeEntry(final String url, final HttpCacheEntry hce) throws IOException {
private byte[] serializeEntry(final String url, final HttpCacheEntry hce) throws ResourceIOException {
final MemcachedCacheEntry mce = memcachedCacheEntryFactory.getMemcachedCacheEntry(url, hce);
try {
return mce.toByteArray();
} catch (final MemcachedSerializationException mse) {
final IOException ioe = new IOException();
ioe.initCause(mse);
throw ioe;
}
return mce.toByteArray();
}
private byte[] convertToByteArray(final Object o) {
@ -200,7 +195,7 @@ public class MemcachedHttpCacheStorage implements HttpCacheStorage {
}
@Override
public HttpCacheEntry getEntry(final String url) throws IOException {
public HttpCacheEntry getEntry(final String url) throws ResourceIOException {
final String key = getCacheKey(url);
if (key == null) {
return null;
@ -217,7 +212,7 @@ public class MemcachedHttpCacheStorage implements HttpCacheStorage {
}
@Override
public void removeEntry(final String url) throws IOException {
public void removeEntry(final String url) throws ResourceIOException {
final String key = getCacheKey(url);
if (key == null) {
return;
@ -231,7 +226,7 @@ public class MemcachedHttpCacheStorage implements HttpCacheStorage {
@Override
public void updateEntry(final String url, final HttpCacheUpdateCallback callback)
throws HttpCacheUpdateException, IOException {
throws HttpCacheUpdateException, ResourceIOException {
int numRetries = 0;
final String key = getCacheKey(url);
if (key == null) {

View File

@ -26,18 +26,15 @@
*/
package org.apache.hc.client5.http.impl.cache.memcached;
import java.io.IOException;
import org.apache.hc.client5.http.cache.ResourceIOException;
/**
* Raised when memcached times out on us.
*/
class MemcachedOperationTimeoutException extends IOException {
private static final long serialVersionUID = 1608334789051537010L;
class MemcachedOperationTimeoutException extends ResourceIOException {
public MemcachedOperationTimeoutException(final Throwable cause) {
super(cause.getMessage());
initCause(cause);
super(cause != null ? cause.getMessage() : null, cause);
}
}

View File

@ -26,16 +26,16 @@
*/
package org.apache.hc.client5.http.impl.cache.memcached;
import org.apache.hc.client5.http.cache.ResourceIOException;
/**
* Raised when there is a problem serializing or deserializing cache
* entries into a byte representation suitable for memcached storage.
*/
public class MemcachedSerializationException extends RuntimeException {
private static final long serialVersionUID = 2201652990656412236L;
public class MemcachedSerializationException extends ResourceIOException {
public MemcachedSerializationException(final Throwable cause) {
super(cause);
super(cause != null ? cause.getMessage() : null, cause);
}
}

View File

@ -26,13 +26,13 @@
*/
package org.apache.hc.client5.http.impl.cache;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.HttpCacheStorage;
import org.apache.hc.client5.http.cache.HttpCacheUpdateCallback;
import org.apache.hc.client5.http.cache.ResourceIOException;
class SimpleHttpCacheStorage implements HttpCacheStorage {
@ -43,23 +43,23 @@ class SimpleHttpCacheStorage implements HttpCacheStorage {
}
@Override
public void putEntry(final String key, final HttpCacheEntry entry) throws IOException {
public void putEntry(final String key, final HttpCacheEntry entry) throws ResourceIOException {
map.put(key, entry);
}
@Override
public HttpCacheEntry getEntry(final String key) throws IOException {
public HttpCacheEntry getEntry(final String key) throws ResourceIOException {
return map.get(key);
}
@Override
public void removeEntry(final String key) throws IOException {
public void removeEntry(final String key) throws ResourceIOException {
map.remove(key);
}
@Override
public void updateEntry(final String key, final HttpCacheUpdateCallback callback)
throws IOException {
public void updateEntry(
final String key, final HttpCacheUpdateCallback callback) throws ResourceIOException {
final HttpCacheEntry v1 = map.get(key);
final HttpCacheEntry v2 = callback.update(v1);
map.put(key,v2);

View File

@ -39,6 +39,7 @@ import java.util.Map;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.HttpCacheStorage;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
@ -667,7 +668,7 @@ public class TestCacheInvalidator {
private void cacheReturnsExceptionForUri(final String theUri) throws IOException {
when(mockStorage.getEntry(theUri)).thenThrow(
new IOException("TOTAL FAIL"));
new ResourceIOException("TOTAL FAIL"));
}
private void cacheEntryisForMethod(final String httpMethod) {

View File

@ -59,7 +59,7 @@ public class TestMemcachedCacheEntryImpl {
}
@Test
public void canBeSerialized() {
public void canBeSerialized() throws Exception {
final byte[] bytes = impl.toByteArray();
assertNotNull(bytes);
assertTrue(bytes.length > 0);
@ -88,7 +88,7 @@ public class TestMemcachedCacheEntryImpl {
}
@Test(expected=MemcachedSerializationException.class)
public void cannotReconstituteFromGarbage() {
public void cannotReconstituteFromGarbage() throws Exception {
impl = new MemcachedCacheEntryImpl();
final byte[] bytes = HttpTestUtils.getRandomBytes(128);
impl.set(bytes);

View File

@ -117,7 +117,7 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
verify(mockKeyHashingScheme).hash(url);
}
public void testThrowsIOExceptionWhenMemcachedPutTimesOut() {
public void testThrowsIOExceptionWhenMemcachedPutTimesOut() throws Exception {
final String url = "foo";
final String key = "key";
final HttpCacheEntry value = HttpTestUtils.makeCacheEntry();
@ -145,7 +145,7 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
}
@Test
public void testCachePutThrowsIOExceptionIfCannotSerializeEntry() {
public void testCachePutThrowsIOExceptionIfCannotSerializeEntry() throws Exception {
final String url = "foo";
final String key = "key";
final HttpCacheEntry value = HttpTestUtils.makeCacheEntry();
@ -167,8 +167,7 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
}
@Test
public void testSuccessfulCacheGet() throws
IOException {
public void testSuccessfulCacheGet() throws Exception {
final String url = "foo";
final String key = "key";
final byte[] serialized = HttpTestUtils.getRandomBytes(128);
@ -194,8 +193,7 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
}
@Test
public void testTreatsNoneByteArrayFromMemcachedAsCacheMiss() throws
IOException {
public void testTreatsNoneByteArrayFromMemcachedAsCacheMiss() throws Exception {
final String url = "foo";
final String key = "key";
@ -211,8 +209,7 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
}
@Test
public void testTreatsNullFromMemcachedAsCacheMiss() throws
IOException {
public void testTreatsNullFromMemcachedAsCacheMiss() throws Exception {
final String url = "foo";
final String key = "key";
@ -228,8 +225,7 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
}
@Test
public void testTreatsAsCacheMissIfCannotReconstituteEntry() throws
IOException {
public void testTreatsAsCacheMissIfCannotReconstituteEntry() throws Exception {
final String url = "foo";
final String key = "key";
final byte[] serialized = HttpTestUtils.getRandomBytes(128);
@ -249,8 +245,7 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
}
@Test
public void testTreatsAsCacheMissIfCantHashStorageKey() throws
IOException {
public void testTreatsAsCacheMissIfCantHashStorageKey() throws Exception {
final String url = "foo";
when(mockKeyHashingScheme.hash(url)).thenThrow(new MemcachedKeyHashingException(new Exception()));