Author: johnh
Date: Wed Aug 4 08:17:50 2010
New Revision: 982154
URL: http://svn.apache.org/viewvc?rev=982154&view=rev
Log:
Modify ProxyingVisitor to ignore malformed Uris -- do not pass them to the
underlying ProxyUriManager, and instead simply re-correlate with their source
nodes.
Patch provided by Gagan Singh. Thanks!
Modified:
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/ProxyingVisitor.java
URL:
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ProxyingVisitor.java?rev=982154&r1=982153&r2=982154&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 4 08:17:50 2010
@@ -20,7 +20,6 @@ package org.apache.shindig.gadgets.rewri
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
-
import org.apache.shindig.common.Pair;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.common.uri.Uri.UriException;
@@ -33,12 +32,17 @@ import org.w3c.dom.Node;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
/**
* Simple visitor that, when plugged into a DomWalker, rewrites
* resource links to proxied versions of the same.
*/
public class ProxyingVisitor implements DomWalker.Visitor {
+ private static final Logger logger = Logger.getLogger(
+ ProxyUriManager.class.getName());
+
public final static Map<String, String> RESOURCE_TAGS =
ImmutableMap.of(
"body", "background",
@@ -93,16 +97,21 @@ public class ProxyingVisitor implements
private List<Pair<Node, Uri>> getProxiedUris(Gadget gadget, List<Node>
nodes) {
List<ProxyUriManager.ProxyUri> reservedUris =
Lists.newArrayListWithCapacity(nodes.size());
+ List<Node> reservedNodes =
+ Lists.newArrayListWithCapacity(nodes.size());
for (Node node : nodes) {
Element element = (Element)node;
String nodeName = node.getNodeName().toLowerCase();
String uriStr = element.getAttribute(RESOURCE_TAGS.get(nodeName)).trim();
try {
- reservedUris.add(new ProxyUriManager.ProxyUri(gadget,
Uri.parse(uriStr)));
+ ProxyUriManager.ProxyUri proxiedUri = new ProxyUriManager.ProxyUri(
+ gadget, Uri.parse(uriStr));
+ reservedUris.add(proxiedUri);
+ reservedNodes.add(node);
} catch (UriException e) {
- // Uri parse exception, add null.
- reservedUris.add(null);
+ // Uri parse exception, ignore.
+ logger.log(Level.WARNING, "Uri exception when parsing: " + uriStr, e);
}
}
@@ -113,7 +122,7 @@ public class ProxyingVisitor implements
List<Pair<Node, Uri>> proxiedUris =
Lists.newArrayListWithCapacity(nodes.size());
Iterator<Uri> uriIt = resourceUris.iterator();
- for (Node node : nodes) {
+ for (Node node : reservedNodes) {
proxiedUris.add(Pair.of(node, uriIt.next()));
}
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=982154&r1=982153&r2=982154&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 4 08:17:50 2010
@@ -17,33 +17,22 @@
*/
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;
import org.apache.shindig.gadgets.uri.ProxyUriManager;
import org.easymock.Capture;
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 static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+
/**
* Test of proxying rewriter
*/
@@ -130,9 +119,9 @@ public class ProxyingVisitorTest extends
List<Node> nodes = ImmutableList.<Node>of(e1, e2, e3, e4);
ProxyUriManager uriManager = createMock(ProxyUriManager.class);
Uri rewrittenUri = Uri.parse("http://bar.com/");
- List<Uri> returned = Lists.newArrayList(rewrittenUri, null, rewrittenUri,
rewrittenUri);
+ List<Uri> returned = Lists.newArrayList(rewrittenUri, rewrittenUri,
rewrittenUri);
ContentRewriterFeature.Config config =
createMock(ContentRewriterFeature.Config.class);
- Integer expires = new Integer(3);
+ Integer expires = 3;
expect(config.getExpires()).andReturn(expires).once();
expect(config);
Capture<List<ProxyUriManager.ProxyUri>> cap = new
Capture<List<ProxyUriManager.ProxyUri>>();
@@ -145,11 +134,10 @@ public class ProxyingVisitorTest extends
assertTrue(rewriter.revisit(gadget, nodes));
verify(config, uriManager);
- assertEquals(4, cap.getValue().size());
+ assertEquals(3, cap.getValue().size());
assertEquals(Uri.parse(scriptSrc), cap.getValue().get(0).getResource());
- assertNull(cap.getValue().get(1));
- assertEquals(Uri.parse(imgSrc), cap.getValue().get(2).getResource());
- assertEquals(Uri.parse(scriptSrc), cap.getValue().get(3).getResource());
+ assertEquals(Uri.parse(imgSrc), cap.getValue().get(1).getResource());
+ assertEquals(Uri.parse(scriptSrc), cap.getValue().get(2).getResource());
assertSame(expires, intCap.getValue());
assertEquals(rewrittenUri.toString(), e1.getAttribute("src"));
assertEquals("^!,,|BLARGH", e2.getAttribute("src"));