Your import order is off -- org.apache.shindig should come before the third
party code.

What about honoring HTTP headers? Wasn't that supposed to be a special
value?

On Thu, Jul 17, 2008 at 2:58 PM, <[EMAIL PROTECTED]> wrote:

> Author: lryan
> Date: Thu Jul 17 14:58:34 2008
> New Revision: 677757
>
> URL: http://svn.apache.org/viewvc?rev=677757&view=rev
> Log:
> Added support for 'expires' param to rewriter to control what cache-headers
> the open-proxy should enforce.
>
> Modified:
>    incubator/shindig/trunk/java/gadgets/conf/gadgets.properties
>
>  
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeature.java
>
>  
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/DefaultContentRewriter.java
>
>  
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriter.java
>
>  
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BaseRewriterTestCase.java
>
>  
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeatureTestCase.java
>
>  
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriterTest.java
>
> Modified: incubator/shindig/trunk/java/gadgets/conf/gadgets.properties
> URL:
> http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/conf/gadgets.properties?rev=677757&r1=677756&r2=677757&view=diff
>
> ==============================================================================
> --- incubator/shindig/trunk/java/gadgets/conf/gadgets.properties (original)
> +++ incubator/shindig/trunk/java/gadgets/conf/gadgets.properties Thu Jul 17
> 14:58:34 2008
> @@ -10,6 +10,7 @@
>  content-rewrite.include-urls=.*
>  content-rewrite.exclude-urls=
>  content-rewrite.include-tags=link,script,embed,img,style
> +content-rewrite.expires=86400
>  cache.capacity=10000
>  gadget-spec.cache.capacity=0
>  message-bundle.cache.capacity=0
>
> Modified:
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeature.java
> URL:
> http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeature.java?rev=677757&r1=677756&r2=677757&view=diff
>
> ==============================================================================
> ---
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeature.java
> (original)
> +++
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeature.java
> Thu Jul 17 14:58:34 2008
> @@ -17,9 +17,12 @@
>  */
>  package org.apache.shindig.gadgets.rewrite;
>
> +import com.google.common.collect.Lists;
> +
>  import org.apache.shindig.gadgets.spec.Feature;
>  import org.apache.shindig.gadgets.spec.GadgetSpec;
>
> +import java.util.List;
>  import java.util.Set;
>  import java.util.TreeSet;
>  import java.util.regex.Pattern;
> @@ -33,6 +36,9 @@
>   private static final String INCLUDE_URLS = "include-urls";
>   private static final String EXCLUDE_URLS = "exclude-urls";
>   private static final String INCLUDE_TAGS = "include-tags";
> +  private static final String EXPIRES = "expires";
> +
> +  public static final String EXPIRES_DEFAULT = "HTTP";
>
>   // Use tree set to maintain order for fingerprint
>   private TreeSet<String> includeTags;
> @@ -43,6 +49,9 @@
>   private Pattern include;
>   private Pattern exclude;
>
> +  // If null then dont enforce a min TTL for proxied content. Use contents
> headers
> +  private Integer expires;
> +
>   private Integer fingerprint;
>
>   /**
> @@ -51,15 +60,20 @@
>    * @param spec
>    * @param defaultInclude As a regex
>    * @param defaultExclude As a regex
> +   * @param defaultExpires Either "HTTP" or a ttl in seconds
>    * @param defaultTags    Set of default tags that can be rewritten
>    */
>   public ContentRewriterFeature(GadgetSpec spec, String defaultInclude,
> -                                String defaultExclude, Set<String>
> defaultTags) {
> +                                String defaultExclude,
> +                                String defaultExpires,
> +      Set<String> defaultTags) {
>     Feature f = spec.getModulePrefs().getFeatures().get("content-rewrite");
>     String includeRegex = normalizeParam(defaultInclude, null);
>     String excludeRegex = normalizeParam(defaultExclude, null);
> +
>     this.includeTags = new TreeSet<String>(defaultTags);
>
> +    List<String> expiresOptions = Lists.newArrayListWithCapacity(3);
>     if (f != null) {
>       if (f.getParams().containsKey(INCLUDE_URLS)) {
>         includeRegex = normalizeParam(f.getParams().get(INCLUDE_URLS),
> includeRegex);
> @@ -80,6 +94,25 @@
>         }
>         includeTags = tags;
>       }
> +
> +      if (f.getParams().containsKey(EXPIRES)) {
> +        expiresOptions.add(normalizeParam(f.getParams().get(EXPIRES),
> null));
> +      }
> +    }
> +
> +    expiresOptions.add(defaultExpires);
> +    expiresOptions.add(EXPIRES_DEFAULT);
> +
> +    for (String expiryOption : expiresOptions) {
> +      try {
> +        expires = new Integer(expiryOption);
> +        break;
> +      } catch (NumberFormatException nfe) {
> +        // Not an integer
> +        if (EXPIRES_DEFAULT.equalsIgnoreCase(expiryOption)) {
> +          break;
> +        }
> +      }
>     }
>
>     if (".*".equals(includeRegex) && excludeRegex == null) {
> @@ -136,6 +169,13 @@
>   }
>
>   /**
> +   * @return the min TTL to enforce or null if proxy should respect
> headers
> +   */
> +  public Integer getExpires() {
> +    return expires;
> +  }
> +
> +  /**
>    * @return fingerprint of rewriting rule for cache-busting
>    */
>   public int getFingerprint() {
>
> Modified:
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/DefaultContentRewriter.java
> URL:
> http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/DefaultContentRewriter.java?rev=677757&r1=677756&r2=677757&view=diff
>
> ==============================================================================
> ---
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/DefaultContentRewriter.java
> (original)
> +++
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/DefaultContentRewriter.java
> Thu Jul 17 14:58:34 2008
> @@ -17,16 +17,16 @@
>  */
>  package org.apache.shindig.gadgets.rewrite;
>
> +import com.google.inject.Inject;
> +import com.google.inject.Singleton;
> +import com.google.inject.name.Named;
> +
>  import org.apache.shindig.gadgets.GadgetException;
>  import org.apache.shindig.gadgets.GadgetSpecFactory;
>  import org.apache.shindig.gadgets.http.HttpRequest;
>  import org.apache.shindig.gadgets.http.HttpResponse;
>  import org.apache.shindig.gadgets.spec.GadgetSpec;
>
> -import com.google.inject.Inject;
> -import com.google.inject.Singleton;
> -import com.google.inject.name.Named;
> -
>  import java.io.ByteArrayOutputStream;
>  import java.io.InputStreamReader;
>  import java.io.OutputStreamWriter;
> @@ -50,6 +50,7 @@
>   private final GadgetSpecFactory specFactory;
>   private final String includeUrls;
>   private final String excludeUrls;
> +  private final String expires;
>   private final Set<String> includeTags;
>
>   @Inject
> @@ -57,10 +58,12 @@
>       GadgetSpecFactory specFactory,
>       @Named("content-rewrite.include-urls")String includeUrls,
>       @Named("content-rewrite.exclude-urls")String excludeUrls,
> +      @Named("content-rewrite.expires")String expires,
>       @Named("content-rewrite.include-tags")String includeTags) {
>     this.specFactory = specFactory;
>     this.includeUrls = includeUrls;
>     this.excludeUrls = excludeUrls;
> +    this.expires = expires;
>     this.includeTags = new HashSet<String>();
>     for (String s : includeTags.split(",")) {
>       if (s != null && s.trim().length() > 0) {
> @@ -117,7 +120,8 @@
>     // Store the feature in the spec so we dont keep parsing it
>     ContentRewriterFeature rewriterFeature =
> (ContentRewriterFeature)spec.getAttribute("content-rewrite");
>     if (rewriterFeature == null) {
> -      rewriterFeature = new ContentRewriterFeature(spec, includeUrls,
> excludeUrls, includeTags);
> +      rewriterFeature = new ContentRewriterFeature(spec, includeUrls,
> excludeUrls, expires,
> +          includeTags);
>       spec.setAttribute("content-rewrite", rewriterFeature);
>     }
>
>
> Modified:
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriter.java
> URL:
> http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriter.java?rev=677757&r1=677756&r2=677757&view=diff
>
> ==============================================================================
> ---
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriter.java
> (original)
> +++
> incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriter.java
> Thu Jul 17 14:58:34 2008
> @@ -19,6 +19,7 @@
>  package org.apache.shindig.gadgets.rewrite;
>
>  import org.apache.shindig.common.util.Utf8UrlCoder;
> +import org.apache.shindig.gadgets.servlet.ProxyBase;
>
>  import java.net.URI;
>  import java.net.URISyntaxException;
> @@ -53,12 +54,16 @@
>       URI linkUri = new URI(link);
>       URI uri = context.resolve(linkUri);
>       if (rewriterFeature.shouldRewriteURL(uri.toString())) {
> -        return prefix
> +        String result = prefix
>             + Utf8UrlCoder.encode(uri.toString())
>             + "&gadget="
>             + Utf8UrlCoder.encode(gadgetUri.toString())
>             + "&fp="
>             + rewriterFeature.getFingerprint();
> +        if (rewriterFeature.getExpires() != null) {
> +          result += "&" + ProxyBase.REFRESH_PARAM  + "=" +
> rewriterFeature.getExpires().toString();
> +        }
> +        return result;
>       } else {
>         return uri.toString();
>       }
>
> Modified:
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BaseRewriterTestCase.java
> URL:
> http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BaseRewriterTestCase.java?rev=677757&r1=677756&r2=677757&view=diff
>
> ==============================================================================
> ---
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BaseRewriterTestCase.java
> (original)
> +++
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BaseRewriterTestCase.java
> Thu Jul 17 14:58:34 2008
> @@ -18,6 +18,7 @@
>  package org.apache.shindig.gadgets.rewrite;
>
>  import com.google.common.collect.Sets;
> +
>  import org.apache.commons.lang.StringUtils;
>  import org.apache.shindig.gadgets.EasyMockTestCase;
>  import org.apache.shindig.gadgets.GadgetException;
> @@ -39,17 +40,20 @@
>   protected void setUp() throws Exception {
>     super.setUp();
>     tags = Sets.newHashSet("embed", "img", "script", "link");
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithoutRewrite(), ".*", "", tags);
> +    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithoutRewrite(), ".*", "", "HTTP",
> +        tags);
>     defaultRewriter = new ProxyingLinkRewriter(
>       SPEC_URL,
>       contentRewriterFeature,
>       "http://www.test.com/proxy?url=";);
>   }
>
> -  protected GadgetSpec getSpecWithRewrite(String include, String exclude,
> Set<String> tags) throws GadgetException {
> +  protected GadgetSpec getSpecWithRewrite(String include, String exclude,
> String expires,
> +      Set<String> tags) throws GadgetException {
>     String xml = "<Module>" +
>                  "<ModulePrefs title=\"title\">" +
>                  "<Optional feature=\"content-rewrite\">\n" +
> +                 "      <Param name=\"expires\">" + expires + "</Param>\n"
> +
>                  "      <Param name=\"include-urls\">" + include +
> "</Param>\n" +
>                  "      <Param name=\"exclude-urls\">" + exclude +
> "</Param>\n" +
>                  "      <Param name=\"include-tags\">" +
> StringUtils.join(tags, ",") + "</Param>\n" +
>
> Modified:
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeatureTestCase.java
> URL:
> http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeatureTestCase.java?rev=677757&r1=677756&r2=677757&view=diff
>
> ==============================================================================
> ---
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeatureTestCase.java
> (original)
> +++
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ContentRewriterFeatureTestCase.java
> Thu Jul 17 14:58:34 2008
> @@ -27,50 +27,56 @@
>  public class ContentRewriterFeatureTestCase extends BaseRewriterTestCase {
>
>   public void testContainerDefaultIncludeAll() throws Exception {
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithoutRewrite(), ".*", "", tags);
> +    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithoutRewrite(), ".*", "", "0", tags);
>     assertTrue(contentRewriterFeature.isRewriteEnabled());
>     assertTrue(contentRewriterFeature.shouldRewriteURL("
> http://www.test.com";));
>   }
>
>   public void testContainerDefaultIncludeNone() throws Exception {
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithoutRewrite(), "", ".*", tags);
> +    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithoutRewrite(), "", ".*", "0", tags);
>     assertFalse(contentRewriterFeature.isRewriteEnabled());
>     assertFalse(contentRewriterFeature.shouldRewriteURL("
> http://www.test.com";));
>   }
>
>   public void testContainerDefaultExcludeOverridesInclude() throws
> Exception {
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithoutRewrite(), ".*", ".*", tags);
> +    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithoutRewrite(), ".*", ".*", "0",
> +        tags);
>     assertFalse(contentRewriterFeature.isRewriteEnabled());
>     assertFalse(contentRewriterFeature.shouldRewriteURL("
> http://www.test.com";));
>   }
>
>   public void testSpecExcludeOverridesContainerDefaultInclude() throws
> Exception {
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithRewrite("", ".*", tags), ".*", "", tags);
> +    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithRewrite("", ".*", "0", tags), ".*",
> +        "", "0", tags);
>     assertFalse(contentRewriterFeature.isRewriteEnabled());
>     assertFalse(contentRewriterFeature.shouldRewriteURL("
> http://www.test.com";));
>   }
>
>   public void testSpecIncludeOverridesContainerDefaultExclude() throws
> Exception {
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithRewrite(".*", "", tags), "", ".*", tags);
> +    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithRewrite(".*", "", "0", tags), "",
> +        ".*", "0", tags);
>     assertTrue(contentRewriterFeature.isRewriteEnabled());
>     assertTrue(contentRewriterFeature.shouldRewriteURL("
> http://www.test.com";));
>   }
>
>   public void testExcludeOverridesInclude() throws Exception {
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithRewrite("test\\.com", "test", tags), "",
> "", tags);
> +    contentRewriterFeature = new ContentRewriterFeature(
> +        getSpecWithRewrite("test\\.com", "test", "0", tags), "", "", "0",
> tags);
>     assertTrue(contentRewriterFeature.isRewriteEnabled());
>     assertFalse(contentRewriterFeature.shouldRewriteURL("
> http://www.test.com";));
>   }
>
>   public void testIncludeOnlyMatch() throws Exception {
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithRewrite("test\\.com", "testx", tags), "",
> "", tags);
> +    contentRewriterFeature = new ContentRewriterFeature(
> +        getSpecWithRewrite("test\\.com", "testx", "0", tags), "", "", "0",
> tags);
>     assertTrue(contentRewriterFeature.isRewriteEnabled());
>     assertTrue(contentRewriterFeature.shouldRewriteURL("
> http://www.test.com";));
>     assertFalse(contentRewriterFeature.shouldRewriteURL("
> http://testx.test.com";));
>   }
>
>   public void testTagRewrite() throws Exception {
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithRewrite("test\\.com", "testx", tags), "",
> "", tags);
> +    contentRewriterFeature = new ContentRewriterFeature(
> +        getSpecWithRewrite("test\\.com", "testx", "0", tags), "", "", "0",
> tags);
>     assertFalse(contentRewriterFeature.shouldRewriteTag("IFRAME"));
>     assertTrue(contentRewriterFeature.shouldRewriteTag("img"));
>     assertTrue(contentRewriterFeature.shouldRewriteTag("ScripT"));
> @@ -78,10 +84,32 @@
>
>   public void testOverrideTagRewrite() throws Exception {
>     Set<String> newTags = Sets.newHashSet("iframe");
> -    contentRewriterFeature = new
> ContentRewriterFeature(getSpecWithRewrite("test\\.com", "testx", newTags),
> "", "", tags);
> +    contentRewriterFeature = new ContentRewriterFeature(
> +        getSpecWithRewrite("test\\.com", "testx", "0", newTags), "", "",
> "0", tags);
>     assertTrue(contentRewriterFeature.shouldRewriteTag("IFRAME"));
>     assertFalse(contentRewriterFeature.shouldRewriteTag("img"));
>     assertFalse(contentRewriterFeature.shouldRewriteTag("ScripT"));
>     assertFalse(contentRewriterFeature.shouldRewriteTag("link"));
>   }
> +
> +  public void testExpiresTimeParse() throws Exception {
> +    contentRewriterFeature = new ContentRewriterFeature(
> +        getSpecWithRewrite("test\\.com", "testx", "12345", tags), "", "",
> "0", tags);
> +    assertNotNull(contentRewriterFeature.getExpires());
> +    assertNotNull(contentRewriterFeature.getExpires() == 12345);
> +  }
> +
> +  public void testExpiresHTTPParse() throws Exception {
> +    contentRewriterFeature = new ContentRewriterFeature(
> +        getSpecWithRewrite("test\\.com", "testx", "htTp ", tags), "", "",
> "12345", tags);
> +    assertNull(contentRewriterFeature.getExpires());
> +  }
> +
> +  public void testExpiresInvalidParse() throws Exception {
> +    contentRewriterFeature = new ContentRewriterFeature(
> +        getSpecWithRewrite("test\\.com", "testx", "junk", tags), "", "",
> "12345", tags);
> +    assertNotNull(contentRewriterFeature.getExpires());
> +    assertNotNull(contentRewriterFeature.getExpires() == 12345);
> +  }
> +
>  }
>
> Modified:
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriterTest.java
> URL:
> http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriterTest.java?rev=677757&r1=677756&r2=677757&view=diff
>
> ==============================================================================
> ---
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriterTest.java
> (original)
> +++
> incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingLinkRewriterTest.java
> Thu Jul 17 14:58:34 2008
> @@ -17,6 +17,8 @@
>  */
>  package org.apache.shindig.gadgets.rewrite;
>
> +import com.google.common.collect.Sets;
> +
>  /**
>  * Test of proxying rewriter
>  */
> @@ -50,9 +52,22 @@
>         rewrite(val));
>   }
>
> +  public void testWithRefresh() throws Exception {
> +    ContentRewriterFeature contentRewriterFeature = new
> ContentRewriterFeature(
> +        getSpecWithoutRewrite(), ".*", "", "86400",
> +        Sets.newHashSet("embed", "img", "script", "link"));
> +    ProxyingLinkRewriter rewriter = new ProxyingLinkRewriter(
> +      SPEC_URL,
> +      contentRewriterFeature,
> +      "http://www.test.com/proxy?url=";);
> +    String val = " test.gif ";
> +    assertEquals("
> http://www.test.com/proxy?url=http%3A%2F%2Fexample.org%2Ftest.gif&gadget=http%3A%2F%2Fexample.org%2Fg.xml&fp=-840722081&refresh=86400
> ",
> +        rewriter.rewrite(val, SPEC_URL));
> +  }
> +
>   public void testInvalidCharRewrite() {
>     String val = "/images/opensocial/movie_trivia/76/${quiz.picture_url}";
> -    assertEquals("/images/opensocial/movie_trivia/76/${quiz.picture_url}",
> +    assertEquals(val,
>         rewrite(val));
>   }
>
>
>
>

Reply via email to