You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/12/10 14:40:26 UTC

svn commit: r1419448 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/client/cache/ main/java/org/apache/http/impl/client/cache/ test/java/org/apache/http/impl/client/cache/

Author: olegk
Date: Mon Dec 10 13:40:25 2012
New Revision: 1419448

URL: http://svn.apache.org/viewvc?rev=1419448&view=rev
Log:
Added cache specific HttpContext adaptor

Added:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheContext.java   (with props)
Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java

Added: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheContext.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheContext.java?rev=1419448&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheContext.java (added)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheContext.java Mon Dec 10 13:40:25 2012
@@ -0,0 +1,71 @@
+/*
+ * ====================================================================
+ * 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.client.cache;
+
+import org.apache.http.annotation.NotThreadSafe;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+
+/**
+ * @since 4.3
+ */
+@NotThreadSafe
+public class HttpCacheContext extends HttpClientContext {
+
+    /**
+     * This is the name under which the {@link CacheResponseStatus} of a request
+     * (for example, whether it resulted in a cache hit) will be recorded if an
+     * {@link HttpContext} is provided during execution.
+     */
+    public static final String CACHE_RESPONSE_STATUS = "http.cache.response.status";
+
+    public static HttpCacheContext adapt(final HttpContext context) {
+        if (context instanceof HttpCacheContext) {
+            return (HttpCacheContext) context;
+        } else {
+            return new HttpCacheContext(context);
+        }
+    }
+
+    public static HttpCacheContext create() {
+        return new HttpCacheContext(new BasicHttpContext());
+    }
+
+    public HttpCacheContext(final HttpContext context) {
+        super(context);
+    }
+
+    public HttpCacheContext() {
+        super();
+    }
+
+    public CacheResponseStatus getCacheResponseStatus() {
+        return getAttribute(CACHE_RESPONSE_STATUS, CacheResponseStatus.class);
+    }
+
+}

Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheContext.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java?rev=1419448&r1=1419447&r2=1419448&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java Mon Dec 10 13:40:25 2012
@@ -49,6 +49,7 @@ import org.apache.http.RequestLine;
 import org.apache.http.annotation.ThreadSafe;
 import org.apache.http.client.cache.CacheResponseStatus;
 import org.apache.http.client.cache.HeaderConstants;
+import org.apache.http.client.cache.HttpCacheContext;
 import org.apache.http.client.cache.HttpCacheEntry;
 import org.apache.http.client.cache.HttpCacheStorage;
 import org.apache.http.client.cache.ResourceFactory;
@@ -89,14 +90,6 @@ import org.apache.http.util.VersionInfo;
 @ThreadSafe // So long as the responseCache implementation is threadsafe
 public class CachingExec implements ClientExecChain {
 
-    /**
-     * This is the name under which the {@link
-     * org.apache.http.client.cache.CacheResponseStatus} of a request
-     * (for example, whether it resulted in a cache hit) will be recorded if an
-     * {@link HttpContext} is provided during execution.
-     */
-    public static final String CACHE_RESPONSE_STATUS = "http.cache.response.status";
-
     private final static boolean SUPPORTS_RANGE_AND_CONTENT_RANGE_HEADERS = false;
 
     private final AtomicLong cacheHits = new AtomicLong();
@@ -532,7 +525,7 @@ public class CachingExec implements Clie
 
     private void setResponseStatus(final HttpContext context, final CacheResponseStatus value) {
         if (context != null) {
-            context.setAttribute(CACHE_RESPONSE_STATUS, value);
+            context.setAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS, value);
         }
     }
 

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java?rev=1419448&r1=1419447&r2=1419448&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java Mon Dec 10 13:40:25 2012
@@ -59,6 +59,7 @@ import org.apache.http.StatusLine;
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.ResponseHandler;
 import org.apache.http.client.cache.CacheResponseStatus;
+import org.apache.http.client.cache.HttpCacheContext;
 import org.apache.http.client.cache.HttpCacheEntry;
 import org.apache.http.client.cache.HttpCacheStorage;
 import org.apache.http.client.methods.CloseableHttpResponse;
@@ -73,7 +74,6 @@ import org.apache.http.impl.cookie.DateU
 import org.apache.http.message.BasicHeader;
 import org.apache.http.message.BasicHttpRequest;
 import org.apache.http.message.BasicHttpResponse;
-import org.apache.http.protocol.ExecutionContext;
 import org.easymock.Capture;
 import org.easymock.IExpectationSetters;
 import org.easymock.classextension.EasyMock;
@@ -119,7 +119,7 @@ public class TestCachingExec {
     private HttpRoute route;
     private HttpHost host;
     private HttpRequestWrapper request;
-    private HttpClientContext context;
+    private HttpCacheContext context;
     private HttpCacheEntry entry;
 
     @SuppressWarnings("unchecked")
@@ -151,7 +151,7 @@ public class TestCachingExec {
         route = new HttpRoute(host);
         request = HttpRequestWrapper.wrap(
                 new BasicHttpRequest("GET", "/stuff", HttpVersion.HTTP_1_1));
-        context = HttpClientContext.create();
+        context = HttpCacheContext.create();
         entry = HttpTestUtils.makeCacheEntry();
         impl = new CachingExec(
                 mockBackend,
@@ -569,7 +569,7 @@ public class TestCachingExec {
 
         impl.execute(route, req, context);
         Assert.assertEquals(CacheResponseStatus.CACHE_MODULE_RESPONSE,
-                context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS));
+                context.getCacheResponseStatus());
     }
 
     @Test
@@ -582,7 +582,7 @@ public class TestCachingExec {
 
         impl.execute(route, req, context);
         Assert.assertEquals(CacheResponseStatus.CACHE_MODULE_RESPONSE,
-                context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS));
+                context.getCacheResponseStatus());
     }
 
     @Test
@@ -622,7 +622,7 @@ public class TestCachingExec {
         impl.execute(route, req, context);
         verifyMocks();
         Assert.assertEquals(CacheResponseStatus.CACHE_MISS,
-                context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS));
+                context.getCacheResponseStatus());
     }
 
     @Test
@@ -681,7 +681,7 @@ public class TestCachingExec {
         impl.execute(route, req2, context);
         verifyMocks();
         Assert.assertEquals(CacheResponseStatus.CACHE_HIT,
-                context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS));
+                context.getCacheResponseStatus());
     }
 
     @Test
@@ -947,7 +947,7 @@ public class TestCachingExec {
         impl.execute(route, req2, context);
         verifyMocks();
         Assert.assertEquals(CacheResponseStatus.VALIDATED,
-                context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS));
+                context.getCacheResponseStatus());
     }
 
     @Test
@@ -1010,7 +1010,7 @@ public class TestCachingExec {
         impl.execute(route, req2, context);
         verifyMocks();
         Assert.assertEquals(CacheResponseStatus.CACHE_MODULE_RESPONSE,
-                context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS));
+                context.getCacheResponseStatus());
     }
 
     @Test
@@ -1038,7 +1038,7 @@ public class TestCachingExec {
         impl.execute(route, req2, context);
         verifyMocks();
         Assert.assertEquals(CacheResponseStatus.CACHE_HIT,
-                context.getAttribute(CachingHttpClient.CACHE_RESPONSE_STATUS));
+                context.getCacheResponseStatus());
     }
 
     @Test
@@ -1446,11 +1446,11 @@ public class TestCachingExec {
         HttpClientContext ctx = HttpClientContext.create();
         impl.execute(route, request);
         impl.execute(route, request, ctx);
-        assertNull(ctx.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        assertNull(ctx.getConnection());
     }
 
     @Test
-    public void testDoesNotSetProxyHostInContextOnCacheHit() throws Exception {
+    public void testSetsTargetHostInContextOnCacheHit() throws Exception {
         DummyBackend backend = new DummyBackend();
         HttpResponse response = HttpTestUtils.make200Response();
         response.setHeader("Cache-Control", "max-age=3600");
@@ -1459,11 +1459,11 @@ public class TestCachingExec {
         HttpClientContext ctx = HttpClientContext.create();
         impl.execute(route, request);
         impl.execute(route, request, ctx);
-        assertNull(ctx.getAttribute(ExecutionContext.HTTP_PROXY_HOST));
+        assertSame(host, ctx.getTargetHost());
     }
 
     @Test
-    public void testSetsTargetHostInContextOnCacheHit() throws Exception {
+    public void testSetsRouteInContextOnCacheHit() throws Exception {
         DummyBackend backend = new DummyBackend();
         HttpResponse response = HttpTestUtils.make200Response();
         response.setHeader("Cache-Control", "max-age=3600");
@@ -1472,7 +1472,7 @@ public class TestCachingExec {
         HttpClientContext ctx = HttpClientContext.create();
         impl.execute(route, request);
         impl.execute(route, request, ctx);
-        assertSame(host, ctx.getAttribute(ExecutionContext.HTTP_TARGET_HOST));
+        assertSame(route, ctx.getHttpRoute());
     }
 
     @Test
@@ -1485,7 +1485,7 @@ public class TestCachingExec {
         HttpClientContext ctx = HttpClientContext.create();
         impl.execute(route, request);
         impl.execute(route, request, ctx);
-        assertSame(request, ctx.getAttribute(ExecutionContext.HTTP_REQUEST));
+        assertSame(request, ctx.getRequest());
     }
 
     @Test
@@ -1498,7 +1498,7 @@ public class TestCachingExec {
         HttpClientContext ctx = HttpClientContext.create();
         impl.execute(route, request);
         HttpResponse result = impl.execute(route, request, ctx);
-        assertSame(result, ctx.getAttribute(ExecutionContext.HTTP_RESPONSE));
+        assertSame(result, ctx.getResponse());
     }
 
     @Test
@@ -1511,7 +1511,7 @@ public class TestCachingExec {
         HttpClientContext ctx = HttpClientContext.create();
         impl.execute(route, request);
         impl.execute(route, request, ctx);
-        assertEquals(Boolean.TRUE, ctx.getAttribute(ExecutionContext.HTTP_REQ_SENT));
+        assertTrue(ctx.isRequestSent());
     }
 
     @Test