Author: gagan
Date: Fri Mar 11 17:08:56 2011
New Revision: 1080673

URL: http://svn.apache.org/viewvc?rev=1080673&view=rev
Log:
Patch by atulvasu | Issue 4260053: Fixing CacheEnforcementVisitor broken in 
1076489 | http://codereview.appspot.com/4260053/

Modified:
    shindig/trunk/java/common/conf/shindig.properties
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/AbstractHttpCache.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponse.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponseBuilder.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/RequestPipeline.java
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitor.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpResponseTest.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitorTest.java

Modified: shindig/trunk/java/common/conf/shindig.properties
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/common/conf/shindig.properties?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- shindig/trunk/java/common/conf/shindig.properties (original)
+++ shindig/trunk/java/common/conf/shindig.properties Fri Mar 11 17:08:56 2011
@@ -88,9 +88,9 @@ shindig.template-rewrite.extension-tag-n
 shindig.cache.http.defaultTtl=3600000
 shindig.cache.http.negativeCacheTtl=60000
 
-# The max-age with which strict no-cache resources should be stored in cache. 
The default value is
-# -1, which indicates that strict no-cache resources should never be stored in 
cache.
-shindig.cache.http.strict-no-cache-resource.max-age=-1
+# Amount of time after which the entry in cache should be considered for a 
refetch for a
+# non-userfacing internal fetch when the response is strict-no-cache. 
+shindig.cache.http.strict-no-cache-resource.refetch-after-ms=-1
 
 # A default refresh interval for XML files, since there is no natural way for 
developers to
 # specify this value, and most HTTP responses don't include good cache control 
headers.

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultGuiceModule.java
 Fri Mar 11 17:08:56 2011
@@ -31,6 +31,7 @@ import com.google.inject.name.Names;
 
 import org.apache.shindig.common.servlet.GuiceServletContextListener;
 import org.apache.shindig.gadgets.config.DefaultConfigContributorModule;
+import org.apache.shindig.gadgets.http.AbstractHttpCache;
 import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.http.InvalidationHandler;
 import org.apache.shindig.gadgets.js.JavascriptModule;
@@ -81,6 +82,7 @@ public class DefaultGuiceModule extends 
 
     // We perform static injection on HttpResponse for cache TTLs.
     requestStaticInjection(HttpResponse.class);
+    requestStaticInjection(AbstractHttpCache.class);
     requestStaticInjection(ProxyUriBase.class);
     registerGadgetHandlers();
     registerFeatureHandlers();

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/AbstractHttpCache.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/AbstractHttpCache.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/AbstractHttpCache.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/AbstractHttpCache.java
 Fri Mar 11 17:08:56 2011
@@ -31,10 +31,43 @@ import org.apache.shindig.gadgets.uri.Ur
  * Base class for content caches. Defines cache expiration rules and
  * and restrictions on allowed content.
  *
- * Note that in the case where strictNoCacheResourceTtlInSeconds is 
non-negative, strict no-cache
+ * Note that in the case where refetchStrictNoCacheAfterMs is non-negative, 
strict no-cache
  * resources are stored in the cache. In this case, only the 
Cache-Control/Pragma headers are stored
  * and not the original content or other headers.
  *
+ * This is used primarily for automatic fetches internal to shindig from 
triggering lots of
+ * back end fetches. Especially comes to play for fetch in 
CacheEnforcementVisitor. Now since this
+ * response is not usable for serving the content, we need to explicitly check 
if the content is
+ * strictNoCache. DefaultRequestPipeline does this correctly, and any 
implementation of
+ * RequestPipeline should do this as well. To prevent breakages for existing 
implementations, we
+ * are keeping the default value to -1.
+ *
+ * Example:
+ * GET /private.html \r\n
+ * Host: www.example.com \r\n
+ * Cache-Control: private, max-age=1000 \r\n
+ * \r\n
+ * My credit card number is 1234-5678-1234-5678
+ *
+ * And with refetch-after=3000, then we store the response as:
+ * GET /private.html \r\n
+ * Host: www.example.com \r\n
+ * Cache-Control: private, max-age=1000 \r\n
+ * \r\n
+ *
+ * For non user facing requests, www.example.com/private.html is considered as 
private and will not
+ * be refetched before 3000ms.
+ *
+ * For user facing requests, response.isStale() is always true, and will be 
fetched even before
+ * 1000ms. The max-age=1000 is completely ignored by shindig, because that 
value is for
+ * the client browser and not for proxies.
+ *
+ * isStale() = false always, for all time >= 0
+ * shouldRefetch() = false when time < date + 3000ms
+ * shouldRefetch() = true when time >= date + 3000ms
+ *
+ * Note that error cases are handled differently. (Even for strict no cache) 
+ *  
  * Implementations that override this are discouraged from using custom cache 
keys unless there is
  * actually customization in the request object itself. It is highly 
recommended that you still
  * use {@link #createKey} in the base class and append any custom data to the 
end of the key instead
@@ -45,12 +78,12 @@ public abstract class AbstractHttpCache 
   private static final String RESIZE_WIDTH = 
UriCommon.Param.RESIZE_WIDTH.getKey();
   private static final String RESIZE_QUALITY = 
UriCommon.Param.RESIZE_QUALITY.getKey();
 
-  // TTL to use for strict no-cache response. A value of -1 indicates that a 
strict no-cache
-  // resource is never cached.
-  static final long DEFAULT_STRICT_NO_CACHE_RESOURCE_TTL_SEC = -1;
+  // Amount of time after which the entry in cache should be considered for a 
refetch for a
+  // non-userfacing internal fetch when the response is strict-no-cache. 
+  @Inject(optional = true) 
@Named("shindig.cache.http.strict-no-cache-resource.refetch-after-ms")
+  public static long REFETCH_STRICT_NO_CACHE_AFTER_MS_DEFAULT = -1L;
 
-  @Inject(optional = true) 
@Named("shindig.cache.http.strict-no-cache-resource.max-age")
-  private long strictNoCacheResourceTtlInSeconds = 
DEFAULT_STRICT_NO_CACHE_RESOURCE_TTL_SEC;
+  private long refetchStrictNoCacheAfterMs = 
REFETCH_STRICT_NO_CACHE_AFTER_MS_DEFAULT;
 
   // Implement these methods to create a concrete HttpCache class.
   protected abstract HttpResponse getResponseImpl(String key);
@@ -62,7 +95,7 @@ public abstract class AbstractHttpCache 
       String keyString = createKey(request);
       HttpResponse cached = getResponseImpl(keyString);
       if (responseStillUsable(cached) &&
-          (!cached.isStrictNoCache() || strictNoCacheResourceTtlInSeconds > 
0)) {
+          (!cached.isStrictNoCache() || refetchStrictNoCacheAfterMs >= 0)) {
         return cached;
       }
     }
@@ -71,10 +104,10 @@ public abstract class AbstractHttpCache 
 
   public boolean addResponse(HttpRequest request, HttpResponse response) {
     HttpResponseBuilder responseBuilder;
-    boolean storeStrictNoCacheResources = (strictNoCacheResourceTtlInSeconds 
>= 0);
+    boolean storeStrictNoCacheResources = (refetchStrictNoCacheAfterMs >= 0);
     if (isCacheable(request, response, storeStrictNoCacheResources)) {
       if (storeStrictNoCacheResources && response.isStrictNoCache()) {
-        responseBuilder = buildStrictNoCacheHttpResponse(request, response);
+        responseBuilder = buildStrictNoCacheHttpResponse(response);
       } else {
         responseBuilder = new HttpResponseBuilder(response);
       }
@@ -92,11 +125,16 @@ public abstract class AbstractHttpCache 
   }
 
   @VisibleForTesting
-  HttpResponseBuilder buildStrictNoCacheHttpResponse(HttpRequest request, 
HttpResponse response) {
+  public void setRefetchStrictNoCacheAfterMs(long refetchStrictNoCacheAfterMs) 
{
+    this.refetchStrictNoCacheAfterMs = refetchStrictNoCacheAfterMs;
+  }
+
+  @VisibleForTesting
+  HttpResponseBuilder buildStrictNoCacheHttpResponse(HttpResponse response) {
     HttpResponseBuilder responseBuilder = new HttpResponseBuilder();
     copyHeaderIfPresent("Cache-Control", response, responseBuilder);
     copyHeaderIfPresent("Pragma", response, responseBuilder);
-    responseBuilder.setCacheControlMaxAge(strictNoCacheResourceTtlInSeconds);
+    
responseBuilder.setRefetchStrictNoCacheAfterMs(refetchStrictNoCacheAfterMs);
     return responseBuilder;
   }
 

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponse.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponse.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponse.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponse.java
 Fri Mar 11 17:08:56 2011
@@ -18,7 +18,14 @@
 package org.apache.shindig.gadgets.http;
 
 import com.google.common.base.Supplier;
-import com.google.common.collect.*;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.MapMaker;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
 import com.google.inject.Inject;
 import com.google.inject.name.Named;
 
@@ -121,26 +128,19 @@ public final class HttpResponse implemen
   // Default TTL for an entry in the cache that does not have any cache 
control headers.
   static final long DEFAULT_TTL = 5L * 60L * 1000L;
 
-  // TTL to use for strict no-cache response. A value of -1 indicates that a 
strict no-cache
-  // resource is never cached. This is also used to indicate if strict 
no-cache responses should
-  // be considered as expired. A non-negative value indicates that such 
responses should not be
-  // considered as expired as long as they are haven't expired otherwise.
-  static final long DEFAULT_STRICT_NO_CACHE_RESOURCE_TTL = -1;
-
   static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
 
   @Inject(optional = true) @Named("shindig.cache.http.negativeCacheTtl")
   private static long negativeCacheTtl = DEFAULT_NEGATIVE_CACHE_TTL;
 
+  // Default TTL for resources that are public and has no explicit 
Cache-Control max-age
+  // and expires headers. Resources without cache-control are considered 
public by default.
   @Inject(optional = true) @Named("shindig.cache.http.defaultTtl")
   private static long defaultTtl = DEFAULT_TTL;
 
   @Inject(optional = true) @Named("shindig.http.fast-encoding-detection")
   private static boolean fastEncodingDetection = true;
 
-  @Inject(optional = true) 
@Named("shindig.cache.http.strict-no-cache-resource.max-age")
-  private long strictNoCacheResourceTtl = DEFAULT_STRICT_NO_CACHE_RESOURCE_TTL;
-
   // Support injection of smarter encoding detection
   @Inject(optional = true)
   private static EncodingDetector.FallbackEncodingDetector 
customEncodingDetector =
@@ -166,6 +166,8 @@ public final class HttpResponse implemen
   private Multimap<String, String> headers;
   private byte[] responseBytes;
 
+  private long refetchStrictNoCacheAfterMs;
+
   /**
    * Needed for serialization. Do not use this for any other purpose.
    */
@@ -192,6 +194,7 @@ public final class HttpResponse implemen
     date = getAndUpdateDate(headerCopy);
     encoding = getAndUpdateEncoding(headerCopy, responseBytes);
     headers = Multimaps.unmodifiableMultimap(headerCopy);
+    refetchStrictNoCacheAfterMs = builder.getRefetchStrictNoCacheAfterMs();
   }
 
   private HttpResponse(int httpStatusCode, String body) {
@@ -345,7 +348,7 @@ public final class HttpResponse implemen
       return date + negativeCacheTtl;
     }
 
-    if (isStrictNoCache() && strictNoCacheResourceTtl < 0) {
+    if (isStrictNoCache()) {
       return -1;
     }
     long maxAge = getCacheControlMaxAge();
@@ -359,6 +362,17 @@ public final class HttpResponse implemen
     return date + defaultTtl;
   }
 
+  public long getRefetchStrictNoCacheAfterMs() {
+    return refetchStrictNoCacheAfterMs;
+  }
+
+  public boolean shouldRefetch() {
+    // Time after which resource should be refetched.
+    long refetchExpiration = isStrictNoCache() ?
+        date + getRefetchStrictNoCacheAfterMs() : getCacheExpiration();
+    return refetchExpiration <= getTimeSource().currentTimeMillis();
+  }
+
   /**
    * @return Consolidated ttl in milliseconds or -1.
    */

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponseBuilder.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponseBuilder.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponseBuilder.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpResponseBuilder.java
 Fri Mar 11 17:08:56 2011
@@ -34,9 +34,7 @@ import org.w3c.dom.Document;
 import org.w3c.dom.DocumentFragment;
 
 import java.nio.charset.Charset;
-import java.util.Arrays;
 import java.util.Collection;
-import java.util.ListIterator;
 import java.util.List;
 import java.util.Map;
 
@@ -48,19 +46,25 @@ public class HttpResponseBuilder extends
   private int httpStatusCode = HttpResponse.SC_OK;
   private final Multimap<String, String> headers = 
HttpResponse.newHeaderMultimap();
   private final Map<String, String> metadata = Maps.newHashMap();
-  
+
   // Stores the HttpResponse object, if any, from which this Builder is 
constructed.
   // This allows us to avoid creating a new HttpResponse in create() when no 
changes
   // have been made.
   private HttpResponse responseObj;
   private int responseObjNumChanges;
-  
+
+  /**
+   * @see {AbstractHttpCache.refetchStrictNoCacheAfterMs}
+   */
+  private long refetchStrictNoCacheAfterMs = -1;
+
   public HttpResponseBuilder(GadgetHtmlParser parser, HttpResponse response) {
     super(parser, response);
     if (response != null) {
       httpStatusCode = response.getHttpStatusCode();
       headers.putAll(response.getHeaders());
       metadata.putAll(response.getMetadata());
+      refetchStrictNoCacheAfterMs = response.getRefetchStrictNoCacheAfterMs();
     } else {
       setResponse(null);
     }
@@ -233,6 +237,12 @@ public class HttpResponseBuilder extends
     return this;
   }
 
+  public HttpResponseBuilder setRefetchStrictNoCacheAfterMs(long 
refetchStrictNoCacheAfterMs) {
+    this.refetchStrictNoCacheAfterMs = refetchStrictNoCacheAfterMs;
+    incrementNumChanges();
+    return this;
+  }
+
   public HttpResponseBuilder setCacheControlMaxAge(long expirationTime) {
     String cacheControl = getHeader("Cache-Control");
     List<String> directives = Lists.newArrayList();
@@ -246,6 +256,7 @@ public class HttpResponseBuilder extends
     }
     directives.add("max-age=" + expirationTime);
     setHeader("Cache-Control", StringUtils.join(directives, ','));
+    incrementNumChanges();
     return this;
   }
 
@@ -293,7 +304,11 @@ public class HttpResponseBuilder extends
   public int getHttpStatusCode() {
     return httpStatusCode;
   }
-  
+
+  public long getRefetchStrictNoCacheAfterMs() {
+    return refetchStrictNoCacheAfterMs;
+  }
+
   /**
    * Ensures that, when setting content bytes, the bytes' encoding is reflected
    * in the current Content-Type header.

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/RequestPipeline.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/RequestPipeline.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/RequestPipeline.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/RequestPipeline.java
 Fri Mar 11 17:08:56 2011
@@ -25,6 +25,10 @@ import com.google.inject.ImplementedBy;
 /**
  * Implements a complete HTTP request pipeline. Performs caching, 
authentication, and serves as an
  * injection point for any custom request pipeline injection.
+ *
+ * NOTE: When using cache, please ensure that you are checking 
response.isStrictNoCache() before
+ * serving out. Because cache may have private contents, even though marked 
stale.
+ * @see {AbstractHttpCache} for details. 
  */
 @ImplementedBy(DefaultRequestPipeline.class)
 public interface RequestPipeline {

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitor.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitor.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitor.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitor.java
 Fri Mar 11 17:08:56 2011
@@ -101,8 +101,8 @@ public class CacheEnforcementVisitor ext
    * @return The visit status of the node.
    */
   protected VisitStatus handleResponseInCache(String uri, HttpResponse 
response) {
-    if (response.isStale()) {
-      // Reserve the node if the response is stale.
+    if (response.shouldRefetch()) {
+      // Reserve the node if the response should be refetched.
       if (response.getCacheControlMaxAge() != 0) {
         // If the cache-control max-age of the original response is zero, it 
doesn't make sense to
         // trigger a pre-fetch for it, since by the time the request for it 
comes in, it will

Modified: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java
 Fri Mar 11 17:08:56 2011
@@ -26,19 +26,11 @@ import static org.junit.Assert.assertTru
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Maps;
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.name.Names;
-import com.google.inject.util.Modules;
 
 import org.apache.shindig.auth.BasicSecurityToken;
 import org.apache.shindig.auth.SecurityToken;
 import org.apache.shindig.common.uri.Uri;
-import org.apache.shindig.common.PropertiesModule;
 import org.apache.shindig.gadgets.AuthType;
-import org.apache.shindig.gadgets.parse.ParseModule;
 import org.apache.shindig.gadgets.oauth.OAuthArguments;
 import org.apache.shindig.gadgets.spec.RequestAuthenticationInfo;
 import org.apache.shindig.gadgets.uri.UriCommon.Param;
@@ -58,21 +50,13 @@ public class AbstractHttpCacheTest {
   private static final String CONTAINER_NAME = "container";
 
   private final TestHttpCache cache = new TestHttpCache();
-  // Cache designed to return 86400 for strictNoCacheResourceTtl.
+  // Cache designed to return 86400ms for refetchStrictNoCacheAfterMs.
   private TestHttpCache extendedStrictNoCacheTtlCache;
 
   @Before
   public void setUp() {
-    Module module = new AbstractModule() {
-      @Override
-      public void configure() {
-        binder().bindConstant()
-            
.annotatedWith(Names.named("shindig.cache.http.strict-no-cache-resource.max-age"))
-            .to(86400L);
-      }
-    };
-    Injector injector = Guice.createInjector(module);
-    extendedStrictNoCacheTtlCache = injector.getInstance(TestHttpCache.class);
+    extendedStrictNoCacheTtlCache = new TestHttpCache();
+    extendedStrictNoCacheTtlCache.setRefetchStrictNoCacheAfterMs(86400L);
   }
 
   @Test
@@ -357,7 +341,7 @@ public class AbstractHttpCacheTest {
     assertTrue("response should have been cached",
                extendedStrictNoCacheTtlCache.addResponse(request, response));
     assertEquals(
-        extendedStrictNoCacheTtlCache.buildStrictNoCacheHttpResponse(request, 
response).create(),
+        
extendedStrictNoCacheTtlCache.buildStrictNoCacheHttpResponse(response).create(),
         extendedStrictNoCacheTtlCache.map.get(key));
   }
 
@@ -457,16 +441,17 @@ public class AbstractHttpCacheTest {
   public void buildStrictNoCacheHttpResponse() {
     HttpResponse response = new HttpResponseBuilder()
         .setResponseString("result")
-        .addHeader("Cache-Control", "private")
+        .addHeader("Cache-Control", "private, max-age=1000")
         .addHeader("X-Method-Override", "GET")
         .create();
     assertTrue(response.isStrictNoCache());
     HttpResponse builtResponse = extendedStrictNoCacheTtlCache
-        .buildStrictNoCacheHttpResponse(new HttpRequest(DEFAULT_URI), 
response).create();
+        .buildStrictNoCacheHttpResponse(response).create();
 
     assertTrue(builtResponse.isStrictNoCache());
     assertEquals("", builtResponse.getResponseAsString());
-    assertEquals("private,max-age=86400", 
builtResponse.getHeader("Cache-Control"));
+    assertEquals("private, max-age=1000", 
builtResponse.getHeader("Cache-Control"));
+    assertEquals(86400, builtResponse.getRefetchStrictNoCacheAfterMs());
     assertFalse(builtResponse.getHeaders().containsKey("Pragma"));
     assertNull(builtResponse.getHeader("X-Method-Override"));
   }
@@ -479,11 +464,11 @@ public class AbstractHttpCacheTest {
         .create();
     assertTrue(response.isStrictNoCache());
     HttpResponse builtResponse = cache
-        .buildStrictNoCacheHttpResponse(new HttpRequest(DEFAULT_URI), 
response).create();
+        .buildStrictNoCacheHttpResponse(response).create();
 
     assertTrue(builtResponse.isStrictNoCache());
     assertEquals("", builtResponse.getResponseAsString());
-    assertEquals("max-age=-1", builtResponse.getHeader("Cache-Control"));
+    assertNull(builtResponse.getHeader("Cache-Control"));
     assertEquals("no-cache", builtResponse.getHeader("Pragma"));
   }
 

Modified: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpResponseTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpResponseTest.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpResponseTest.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpResponseTest.java
 Fri Mar 11 17:08:56 2011
@@ -17,10 +17,10 @@
  */
 package org.apache.shindig.gadgets.http;
 
+import org.apache.commons.io.IOUtils;
+import org.apache.shindig.common.servlet.HttpUtil;
 import org.apache.shindig.common.util.DateUtil;
 import org.apache.shindig.common.util.FakeTimeSource;
-
-import org.apache.commons.io.IOUtils;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -529,4 +529,89 @@ public class HttpResponseTest extends As
 
     assertEquals(expectedResponse, deserialized);
   }
+
+  @Test
+  public void testCacheExpirationForStrictNoCacheResponse() throws Exception {
+    assertEquals(-1,
+        new HttpResponseBuilder()
+            .setStrictNoCache()
+            .setRefetchStrictNoCacheAfterMs(10000)
+            .create()
+            .getCacheExpiration());
+  }
+
+  @Test
+  public void testCacheExpirationForStrictNoCacheResponsePrivateLowMaxAge() 
throws Exception {
+    assertEquals(-1,
+        new HttpResponseBuilder()
+            .addHeader("Cache-Control", "private, max-age=5000")
+            .setRefetchStrictNoCacheAfterMs(10000)
+            .create()
+            .getCacheExpiration());
+  }
+
+  @Test
+  public void testCacheExpirationForStrictNoCacheResponsePrivateHighMaxAge() 
throws Exception {
+    assertEquals(-1,
+        new HttpResponseBuilder()
+            .addHeader("Cache-Control", "private, max-age=20000")
+            .setRefetchStrictNoCacheAfterMs(10000)
+            .create()
+            .getCacheExpiration());
+  }
+
+  @Test
+  public void testShouldRefetchForStrictNoCacheResponseCurrentTime() throws 
Exception {
+    assertEquals(false,
+        new HttpResponseBuilder()
+            .setStrictNoCache()
+            .setRefetchStrictNoCacheAfterMs(10000)
+            .create()
+            .shouldRefetch());
+  }
+
+  @Test
+  public void 
testShouldRefetchForStrictNoCacheResponseCurrentTimePrivateLowMaxAge() throws 
Exception {
+    assertEquals(false,
+        new HttpResponseBuilder()
+            .addHeader("Cache-Control", "private, max-age=5000")
+            .setRefetchStrictNoCacheAfterMs(10000)
+            .create()
+            .shouldRefetch());
+  }
+
+  @Test
+  public void 
testShouldRefetchForStrictNoCacheResponseCurrentTimePrivateHighMaxAge() throws 
Exception {
+    assertEquals(false,
+        new HttpResponseBuilder()
+            .addHeader("Cache-Control", "private, max-age=20000")
+            .setRefetchStrictNoCacheAfterMs(10000)
+            .create()
+            .shouldRefetch());
+  }
+
+  @Test
+  public void testShouldRefetchForStrictNoCacheResponsePastShouldRefetch() 
throws Exception {
+    assertEquals(true, new HttpResponseBuilder().setStrictNoCache()
+        .setHeader("Date",
+            
DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 
20000))
+        .setRefetchStrictNoCacheAfterMs(10000)
+        .create()
+        .shouldRefetch());
+  }
+
+  @Test
+  public void testShouldRefetchForStrictNoCacheResponsePastShouldNotRefetch() 
throws Exception {
+    assertEquals(false, new HttpResponseBuilder().setStrictNoCache()
+        .setHeader("Date",
+            
DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 5000))
+        .setRefetchStrictNoCacheAfterMs(10000)
+        .create()
+        .shouldRefetch());
+  }
+
+  @Test
+  public void testCacheExpirationForStrictNoCacheResponseWithoutOverride() 
throws Exception {
+    assertEquals(-1, new 
HttpResponseBuilder().setStrictNoCache().create().getCacheExpiration());
+  }
 }

Modified: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitorTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitorTest.java?rev=1080673&r1=1080672&r2=1080673&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitorTest.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CacheEnforcementVisitorTest.java
 Fri Mar 11 17:08:56 2011
@@ -26,21 +26,15 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertTrue;
 
 import com.google.common.collect.Maps;
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.name.Names;
+import com.google.common.util.concurrent.MoreExecutors;
 
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.gadgets.http.AbstractHttpCache;
-import org.apache.shindig.gadgets.http.HttpCache;
 import org.apache.shindig.gadgets.http.HttpRequest;
 import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.http.HttpResponseBuilder;
 import org.apache.shindig.gadgets.http.RequestPipeline;
 import org.apache.shindig.gadgets.parse.ParseModule.DOMImplementationProvider;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.w3c.dom.Attr;
@@ -49,7 +43,6 @@ import org.w3c.dom.Element;
 
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -57,7 +50,7 @@ import java.util.concurrent.TimeUnit;
  */
 public class CacheEnforcementVisitorTest extends DomWalkerTestBase {
   private ExecutorService executor;
-  private HttpCache cache;
+  private TestHttpCache cache;
   protected Document doc;
   private static final Map<String, String> ALL_RESOURCES =
       CacheEnforcementVisitor.Tags.ALL_RESOURCES.getResourceTags();
@@ -65,20 +58,12 @@ public class CacheEnforcementVisitorTest
 
   @Before
   public void setUp() {
-    executor = Executors.newSingleThreadExecutor();
+    executor = MoreExecutors.sameThreadExecutor();
     DOMImplementationProvider domImpl = new DOMImplementationProvider();
     doc = domImpl.get().createDocument(null, null, null);
-    Module module = new AbstractModule() {
-      public void configure() {
-        binder().bindConstant()
-            
.annotatedWith(Names.named("shindig.cache.http.strict-no-cache-resource.max-age"))
-            .to(86400L);
-        requestStaticInjection(HttpResponse.class);
-      }
-    };
-    Injector injector = Guice.createInjector(module);
-    cache = injector.getInstance(TestHttpCache.class);
- }
+    cache = new TestHttpCache();
+    cache.setRefetchStrictNoCacheAfterMs(86400L);
+  }
 
   @Test
   public void testStaleImgWithNegativeTtlReservedAndFetchTriggered() throws 
Exception {


Reply via email to