Browse files

added log interceptor

  • Loading branch information...
1 parent d3d4c5d commit 1bb04ad5b53911ec7d0114e5e878ae382848d1dc Archimedes Trajano committed Jun 21, 2011
View
14 src/main/java/net/trajano/berkeleydbhttpcachestorage/BerkeleyDBCachingHttpClient.java
@@ -1,6 +1,7 @@
package net.trajano.berkeleydbhttpcachestorage;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.cache.ResourceFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.cache.CacheConfig;
import org.apache.http.impl.client.cache.CachingHttpClient;
@@ -26,6 +27,12 @@ public BerkeleyDBCachingHttpClient(final Database database) {
new BerkeleyDBHttpCacheStorage(database), new CacheConfig());
}
+ public BerkeleyDBCachingHttpClient(final Database database,
+ final CacheConfig cacheConfig) {
+ super(new DefaultHttpClient(),
+ new BerkeleyDBHttpCacheStorage(database), cacheConfig);
+ }
+
public BerkeleyDBCachingHttpClient(final HttpClient client,
final Database database) {
super(client, new BerkeleyDBHttpCacheStorage(database),
@@ -36,4 +43,11 @@ public BerkeleyDBCachingHttpClient(final HttpClient client,
final Database database, final CacheConfig cacheConfig) {
super(client, new BerkeleyDBHttpCacheStorage(database), cacheConfig);
}
+
+ public BerkeleyDBCachingHttpClient(final HttpClient client,
+ final ResourceFactory resourceFactory, final Database database,
+ final CacheConfig cacheConfig) {
+ super(client, resourceFactory,
+ new BerkeleyDBHttpCacheStorage(database), cacheConfig);
+ }
}
View
47 ...in/java/net/trajano/berkeleydbhttpcachestorage/LogCacheStatusHttpResponseInterceptor.java
@@ -0,0 +1,47 @@
+package net.trajano.berkeleydbhttpcachestorage;
+
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.client.cache.CacheResponseStatus;
+import org.apache.http.impl.client.cache.CachingHttpClient;
+import org.apache.http.protocol.HttpContext;
+
+/**
+ * Logs the cache status. Note this not log
+ * {@link CacheResponseStatus#CACHE_HIT} as this interceptor will not be called
+ * in that scenario.
+ *
+ * @author trajano
+ *
+ */
+public class LogCacheStatusHttpResponseInterceptor implements
+ HttpResponseInterceptor {
+
+ private final Level level;
+ private final Logger logger;
+
+ public LogCacheStatusHttpResponseInterceptor() {
+ this(Logger.getAnonymousLogger(), Level.FINE);
+ }
+
+ public LogCacheStatusHttpResponseInterceptor(final Logger logger,
+ final Level level) {
+ this.logger = logger;
+ this.level = level;
+ }
+
+ public void process(final HttpResponse response, final HttpContext context)
+ throws HttpException, IOException {
+ final CacheResponseStatus status = (CacheResponseStatus) context
+ .getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS);
+ if (status != null) {
+ logger.log(level, status.toString());
+ }
+ }
+
+}
View
62 ...test/java/net/trajano/berkeleydbhttpcachestorage/test/BerkeleyDBHttpCacheStorageTest.java
@@ -2,12 +2,16 @@
import java.io.File;
import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import net.trajano.berkeleydbhttpcachestorage.BerkeleyDBHttpCacheStorage;
+import net.trajano.berkeleydbhttpcachestorage.LogCacheStatusHttpResponseInterceptor;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.client.cache.CacheResponseStatus;
import org.apache.http.client.methods.HttpGet;
@@ -56,8 +60,10 @@
private long doRequest(final HttpClient httpClient) throws IOException {
final long start = System.nanoTime();
final HttpResponse response = httpClient
- .execute(new HttpGet(
- "http://www.gravatar.com/avatar/a798a3d661375ece15776f83fbb80c2c.png"));
+ .execute(
+ new HttpGet(
+ "http://www.gravatar.com/avatar/a798a3d661375ece15776f83fbb80c2c.png"),
+ new BasicHttpContext());
Assert.assertNotNull(response);
final HttpEntity entity = response.getEntity();
Assert.assertNotNull(entity);
@@ -260,6 +266,58 @@ public void testDoMultipleRequestOverTwoByTime() throws Exception {
}
/**
+ * Tests multiple requests, the second request should take less time.
+ *
+ * @throws Exception
+ * error had occurred.
+ */
+ @Test
+ public void testDoMultipleRequestOverTwoWithLogging() throws Exception {
+ final Logger logger = Logger.getLogger(this.getClass().getName());
+ final long time1;
+ {
+ final EnvironmentConfig environmentConfig = new EnvironmentConfig();
+ environmentConfig.setAllowCreate(true);
+ final Environment env = new Environment(testDirectory,
+ environmentConfig);
+ final DatabaseConfig databaseConfig = new DatabaseConfig();
+ databaseConfig.setAllowCreate(true);
+ final Database db = env.openDatabase(null, "cache", databaseConfig);
+ final DefaultHttpClient client = new DefaultHttpClient();
+ final HttpResponseInterceptor interceptor = new LogCacheStatusHttpResponseInterceptor(
+ logger, Level.INFO);
+ client.addResponseInterceptor(interceptor);
+ final HttpClient httpClient = new CachingHttpClient(client,
+ new BerkeleyDBHttpCacheStorage(db), cacheConfig);
+
+ time1 = doRequest(httpClient);
+ db.close();
+ env.close();
+ }
+ final long time2;
+ {
+ final EnvironmentConfig environmentConfig = new EnvironmentConfig();
+ environmentConfig.setAllowCreate(true);
+ final Environment env = new Environment(testDirectory,
+ environmentConfig);
+ final DatabaseConfig databaseConfig = new DatabaseConfig();
+ databaseConfig.setAllowCreate(true);
+ final Database db = env.openDatabase(null, "cache", databaseConfig);
+ final DefaultHttpClient client = new DefaultHttpClient();
+ final HttpResponseInterceptor interceptor = new LogCacheStatusHttpResponseInterceptor(
+ logger, Level.INFO);
+ client.addResponseInterceptor(interceptor);
+ final HttpClient httpClient = new CachingHttpClient(client,
+ new BerkeleyDBHttpCacheStorage(db), cacheConfig);
+
+ time2 = doRequest(httpClient);
+ db.close();
+ env.close();
+ }
+ Assert.assertTrue(time2 <= time1);
+ }
+
+ /**
* Tests a single request.
*
* @throws Exception

0 comments on commit 1bb04ad

Please sign in to comment.