You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by jo...@apache.org on 2010/08/12 00:01:21 UTC

svn commit: r984602 - in /shindig/trunk/java/gadgets/src: main/java/org/apache/shindig/gadgets/rewrite/ test/java/org/apache/shindig/gadgets/rewrite/

Author: johnh
Date: Wed Aug 11 22:01:20 2010
New Revision: 984602

URL: http://svn.apache.org/viewvc?rev=984602&view=rev
Log:
Make ProxyVisitor configurable as to which tags it rewrites and which it doesn't.

Patch provided by Gagan Singh. Thanks!


Modified:
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingContentRewriter.java
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitor.java
    shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitorTest.java

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingContentRewriter.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingContentRewriter.java?rev=984602&r1=984601&r2=984602&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingContentRewriter.java (original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingContentRewriter.java Wed Aug 11 22:01:20 2010
@@ -19,7 +19,6 @@
 package org.apache.shindig.gadgets.rewrite;
 
 import com.google.inject.Inject;
-
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.gadgets.Gadget;
 import org.apache.shindig.gadgets.rewrite.DomWalker.Visitor;
@@ -51,6 +50,9 @@ public class ProxyingContentRewriter ext
     return Arrays.<Visitor>asList(
         new ConcatVisitor.Js(config, concatUriManager),
         new ConcatVisitor.Css(config, concatUriManager),
-        new ProxyingVisitor(config, proxyUriManager));
+        new ProxyingVisitor(config, proxyUriManager,
+                            ProxyingVisitor.Tags.SCRIPT,
+                            ProxyingVisitor.Tags.STYLESHEET,
+                            ProxyingVisitor.Tags.EMBEDDED_IMAGES));
   }
 }

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitor.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitor.java?rev=984602&r1=984601&r2=984602&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitor.java (original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitor.java Wed Aug 11 22:01:20 2010
@@ -30,6 +30,7 @@ import org.w3c.dom.Attr;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -44,27 +45,60 @@ public class ProxyingVisitor implements 
   private static final Logger logger = Logger.getLogger(
       ProxyUriManager.class.getName());
 
-  public final static Map<String, String> RESOURCE_TAGS =
-    ImmutableMap.of(
-        "body", "background",
-        "img", "src",
-        "input", "src",
-        "link", "href",
-        "script", "src");
+  /**
+   * Enum for resource tags and associated attributes that should be
+   * proxied through shindig.
+   */
+  public enum Tags {
+    // Javascript resources requested by the current page.
+    SCRIPT(ImmutableMap.of("script", "src")),
+
+    // Css stylesheet resources requested by the current page.
+    STYLESHEET(ImmutableMap.of("link", "href")),
+
+    // Other embedded resources requested on the same page.
+    EMBEDDED_IMAGES(ImmutableMap.of("body", "background",
+                                    "img", "src",
+                                    "input", "src")),
+
+    // All resources that possibly be rewritten. Useful for testing.
+    ALL_RESOURCES(ImmutableMap.<String, String>builder()
+        .putAll(SCRIPT.getResourceTags())
+        .putAll(STYLESHEET.getResourceTags())
+        .putAll(EMBEDDED_IMAGES.getResourceTags())
+        .build());
+
+    private Map<String, String> resourceTags;
+    private Tags(Map<String, String> resourceTags) {
+      this.resourceTags = resourceTags;
+    }
+
+    public Map<String, String> getResourceTags() {
+      return resourceTags;
+    }
+  }
 
+  // Map of tag name to attribute of resources to rewrite.
+  private final Map<String, String> resourceTags;
   private final ContentRewriterFeature.Config featureConfig;
   private final ProxyUriManager uriManager;
 
   public ProxyingVisitor(ContentRewriterFeature.Config featureConfig,
-                              ProxyUriManager uriManager) {
+                         ProxyUriManager uriManager,
+                         Tags... resourceTags) {
     this.featureConfig = featureConfig;
     this.uriManager = uriManager;
+
+    this.resourceTags = new HashMap<String, String>();
+    for (Tags r : resourceTags) {
+      this.resourceTags.putAll(r.getResourceTags());
+    }
   }
 
   public VisitStatus visit(Gadget gadget, Node node) throws RewritingException {
     String nodeName = node.getNodeName().toLowerCase();
     if (node.getNodeType() == Node.ELEMENT_NODE &&
-        RESOURCE_TAGS.containsKey(nodeName) &&
+        resourceTags.containsKey(nodeName) &&
         featureConfig.shouldRewriteTag(nodeName)) {
       if (nodeName.equals("link")) {
         // Rewrite link only when it is for css.
@@ -76,7 +110,7 @@ public class ProxyingVisitor implements 
       }
 
       Attr attr = (Attr)node.getAttributes().getNamedItem(
-          RESOURCE_TAGS.get(nodeName));
+          resourceTags.get(nodeName));
       if (attr != null) {
         String urlValue = attr.getValue();
         if (!StringUtils.isEmpty(urlValue) && featureConfig.shouldRewriteURL(urlValue)) {
@@ -97,7 +131,7 @@ public class ProxyingVisitor implements 
       }
       Element element = (Element)proxyPair.one;
       String nodeName = element.getNodeName().toLowerCase();
-      element.setAttribute(RESOURCE_TAGS.get(nodeName), proxyPair.two.toString());
+      element.setAttribute(resourceTags.get(nodeName), proxyPair.two.toString());
       mutated = true;
     }
     
@@ -113,7 +147,7 @@ public class ProxyingVisitor implements 
     for (Node node : nodes) {
       Element element = (Element)node;
       String nodeName = node.getNodeName().toLowerCase();
-      String uriStr = element.getAttribute(RESOURCE_TAGS.get(nodeName)).trim();
+      String uriStr = element.getAttribute(resourceTags.get(nodeName)).trim();
       try {
         ProxyUriManager.ProxyUri proxiedUri = new ProxyUriManager.ProxyUri(
             gadget, Uri.parse(uriStr));

Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitorTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitorTest.java?rev=984602&r1=984601&r2=984602&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitorTest.java (original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitorTest.java Wed Aug 11 22:01:20 2010
@@ -17,18 +17,8 @@
  */
 package org.apache.shindig.gadgets.rewrite;
 
-import static org.easymock.EasyMock.capture;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.gadgets.Gadget;
 import org.apache.shindig.gadgets.rewrite.DomWalker.Visitor.VisitStatus;
@@ -38,18 +28,21 @@ import org.junit.Test;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
-
-import java.util.List;
 import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
 
 /**
  * Test of proxying rewriter
  */
 public class ProxyingVisitorTest extends DomWalkerTestBase {
   private static final String URL_STRING = "http://www.foo.com/";
-  
+  private static final Map<String, String> ALL_RESOURCES = ProxyingVisitor.Tags
+      .ALL_RESOURCES.getResourceTags();
+
   @Test
   public void imgVisitReserved() throws Exception {
     checkVisitReserved("img", true);
@@ -69,7 +62,7 @@ public class ProxyingVisitorTest extends
   public void embedVisitReserved() throws Exception {
     checkVisitReserved("embed", false);
   }
-  
+
   @Test
   public void csslinkVisitReserved() throws Exception {
     checkVisitReserved("link", true, "rel", "stylesheet", "type", "text/css");
@@ -94,12 +87,12 @@ public class ProxyingVisitorTest extends
   public void scriptVisitReserved() throws Exception {
     checkVisitReserved("script", true);
   }
-  
+
   @Test
   public void objectVisitReserved() throws Exception {
     checkVisitReserved("object", false);
   }
-  
+
   @Test
   public void otherVisitNotReserved() throws Exception {
     checkVisitReserved("other", false);
@@ -113,7 +106,10 @@ public class ProxyingVisitorTest extends
     expect(config.shouldRewriteTag("img")).andReturn(true).anyTimes();
     replay(config);
 
-    ProxyingVisitor rewriter = new ProxyingVisitor(config, null);
+    ProxyingVisitor rewriter = new ProxyingVisitor(config, null,
+        ProxyingVisitor.Tags.SCRIPT,
+        ProxyingVisitor.Tags.STYLESHEET,
+        ProxyingVisitor.Tags.EMBEDDED_IMAGES);
     VisitStatus status = rewriter.visit(null, node);
     verify(config);
 
@@ -131,7 +127,7 @@ public class ProxyingVisitorTest extends
 
   private boolean getVisitReserved(String tag, boolean resUrl, boolean resTag, String ... attrs) throws Exception {
     // Reserved when lower-case and both URL and Tag reserved.
-    String attrName = ProxyingVisitor.RESOURCE_TAGS.get(tag.toLowerCase());
+    String attrName = ALL_RESOURCES.get(tag.toLowerCase());
     attrName = attrName != null ? attrName : "src";
 
     ArrayList <String> attrsList = Lists.newArrayList(attrs);
@@ -144,7 +140,10 @@ public class ProxyingVisitorTest extends
     expect(config.shouldRewriteTag(tag.toLowerCase())).andReturn(resTag).anyTimes();
     replay(config);
 
-    ProxyingVisitor rewriter = new ProxyingVisitor(config, null);
+    ProxyingVisitor rewriter = new ProxyingVisitor(config, null,
+        ProxyingVisitor.Tags.SCRIPT,
+        ProxyingVisitor.Tags.STYLESHEET,
+        ProxyingVisitor.Tags.EMBEDDED_IMAGES);
     VisitStatus status = rewriter.visit(null, node);
     verify(config);
 
@@ -176,7 +175,10 @@ public class ProxyingVisitorTest extends
     replay(config, uriManager);
     Gadget gadget = gadget();
 
-    ProxyingVisitor rewriter = new ProxyingVisitor(config, uriManager);
+    ProxyingVisitor rewriter = new ProxyingVisitor(config, uriManager,
+        ProxyingVisitor.Tags.SCRIPT,
+        ProxyingVisitor.Tags.STYLESHEET,
+        ProxyingVisitor.Tags.EMBEDDED_IMAGES);
     assertTrue(rewriter.revisit(gadget, nodes));
     verify(config, uriManager);