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/07/09 00:31:14 UTC

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

Author: johnh
Date: Thu Jul  8 22:31:14 2010
New Revision: 961965

URL: http://svn.apache.org/viewvc?rev=961965&view=rev
Log:
Rewriter for @import statements embedded in <style> tags.

Patch provided by Gagan Singh.


Added:
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsRewriter.java
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitor.java
    shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitorTest.java

Added: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsRewriter.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsRewriter.java?rev=961965&view=auto
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsRewriter.java (added)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsRewriter.java Thu Jul  8 22:31:14 2010
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+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.uri.ProxyUriManager;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Rewriter that replaces urls (@import + background) in
+ * &lt;style&gt; ... &lt;/style&gt; with their proxied versions.
+ */
+public class StyleTagProxyEmbeddedUrlsRewriter extends DomWalker.Rewriter {
+  private final ContentRewriterFeature.Factory featureConfigFactory;
+  private final ProxyUriManager proxyUriManager;
+  private final CssResponseRewriter cssRewriter;
+
+  @Inject
+  public StyleTagProxyEmbeddedUrlsRewriter(ContentRewriterFeature.Factory featureConfigFactory,
+                                           ProxyUriManager proxyUriManager,
+                                           CssResponseRewriter cssRewriter) {
+    this.featureConfigFactory = featureConfigFactory;
+    this.proxyUriManager = proxyUriManager;
+    this.cssRewriter = cssRewriter;
+  }
+
+  @Override
+  protected List<DomWalker.Visitor> makeVisitors(Gadget context, Uri gadgetUri) {
+    ContentRewriterFeature.Config config = featureConfigFactory.get(gadgetUri);
+    return Arrays.<DomWalker.Visitor>asList(
+        new StyleTagProxyEmbeddedUrlsVisitor(config, proxyUriManager,
+                                             cssRewriter));
+  }
+}

Added: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitor.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitor.java?rev=961965&view=auto
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitor.java (added)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitor.java Thu Jul  8 22:31:14 2010
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+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.uri.ProxyUriManager;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import java.util.List;
+
+/**
+ * Visitor that replaces urls (@import + background) in
+ * &lt;style&gt; ... &lt;/style&gt; with their proxied versions.
+ */
+public class StyleTagProxyEmbeddedUrlsVisitor implements DomWalker.Visitor {
+  protected final ContentRewriterFeature.Config config;
+  protected final ProxyUriManager proxyUriManager;
+  protected final CssResponseRewriter cssRewriter;
+
+  @Inject
+  public StyleTagProxyEmbeddedUrlsVisitor(ContentRewriterFeature.Config config,
+                                          ProxyUriManager proxyUriManager,
+                                          CssResponseRewriter cssRewriter) {
+    this.config = config;
+    this.proxyUriManager = proxyUriManager;
+    this.cssRewriter = cssRewriter;
+  }
+
+  public VisitStatus visit(Gadget gadget, Node node) throws RewritingException {
+    // Only process <style> elements.
+    if (node.getNodeType() != Node.ELEMENT_NODE ||
+        !node.getNodeName().equalsIgnoreCase("style")) {
+      return VisitStatus.BYPASS;
+    }
+
+    return VisitStatus.RESERVE_NODE;
+  }
+
+  public boolean revisit(Gadget gadget, List<Node> nodes) throws RewritingException {
+    Uri contentBase = gadget.getSpec().getUrl();
+
+    for (Node node: nodes) {
+      Element elem = (Element) node;
+      cssRewriter.rewrite(
+          elem, contentBase,
+          CssResponseRewriter.uriMaker(proxyUriManager, config), false);
+    }
+    return nodes.size() > 0;
+  }
+}

Added: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitorTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitorTest.java?rev=961965&view=auto
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitorTest.java (added)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/StyleTagProxyEmbeddedUrlsVisitorTest.java Thu Jul  8 22:31:14 2010
@@ -0,0 +1,101 @@
+/*
+ * 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.
+ */
+package org.apache.shindig.gadgets.rewrite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import org.apache.shindig.common.PropertiesModule;
+import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.gadgets.DefaultGuiceModule;
+import org.apache.shindig.gadgets.Gadget;
+import org.apache.shindig.gadgets.oauth.OAuthModule;
+import org.apache.shindig.gadgets.parse.ParseModule;
+import org.apache.shindig.gadgets.parse.caja.CajaHtmlParser;
+import org.apache.shindig.gadgets.parse.caja.CajaHtmlSerializer;
+import org.apache.shindig.gadgets.uri.ProxyUriManager;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests for StyleTagProxyEmbeddedUrlsVisitor.
+ */
+public class StyleTagProxyEmbeddedUrlsVisitorTest extends DomWalkerTestBase {
+  private Injector injector;
+  private CajaHtmlParser htmlParser;
+  CajaHtmlSerializer serializer;
+
+  @Before
+  public void setUp() {
+    super.setUp();
+    injector = Guice.createInjector(
+        new PropertiesModule(), new DefaultGuiceModule(), new OAuthModule());
+    ParseModule.DOMImplementationProvider domImpl =
+        new ParseModule.DOMImplementationProvider();
+    htmlParser = new CajaHtmlParser(domImpl.get());
+    serializer = new CajaHtmlSerializer();
+  }
+
+  @Test
+  public void testImportsAndBackgroundUrlsInStyleTag() throws Exception {
+    String html = "<html><head>"
+                  + "<style>"
+                  + "P {color:blue;}"
+                  + "@import url(/1.css);"
+                  + "P {color:red;}"
+                  + "A {background: url(/2.jpg);}"
+                  + "</style>"
+                  + "</head><body><a href=\"hello\">Hello</a>"
+                  + "</body></html>";
+    String expected =
+        "<html><head>"
+        + "<style>P {color:blue;}"
+        + "@import url('//localhost:8080/gadgets/proxy?container=default&"
+        + "gadget=http%3A%2F%2F1.com%2F&debug=0&nocache=0"
+        + "&url=http%3A%2F%2F1.com%2F1.css');\n"
+        + "P {color:red;}"
+        + "A {background: url('//localhost:8080/gadgets/proxy?container=default"
+        + "&gadget=http%3A%2F%2F1.com%2F&debug=0&nocache=0"
+        + "&url=http%3A%2F%2F1.com%2F2.jpg');}"
+        + "</style></head>"
+        + "<body><a href=\"hello\">Hello</a>\n"
+        + "</body></html>";
+    Document doc = htmlParser.parseDom(html);
+
+    ContentRewriterFeature.Config config = injector.getInstance(
+        ContentRewriterFeature.DefaultConfig.class);
+    EasyMock.replay();
+
+    StyleTagProxyEmbeddedUrlsVisitor visitor = new StyleTagProxyEmbeddedUrlsVisitor(
+        config, injector.getInstance(ProxyUriManager.class),
+        injector.getInstance(CssResponseRewriter.class));
+
+    Gadget gadget = DomWalker.makeGadget(Uri.parse("http://1.com/"));
+    NodeList list = doc.getElementsByTagName("style");
+    visitor.revisit(gadget, ImmutableList.of(list.item(0)));
+    EasyMock.verify();
+
+    assertEquals(expected, serializer.serialize(doc));
+  }
+}