knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From su...@apache.org
Subject knox git commit: KNOX-729 added filter apply capability to html content
Date Sat, 15 Oct 2016 11:20:23 GMT
Repository: knox
Updated Branches:
  refs/heads/master 5cb02e2f9 -> febf20c05


KNOX-729 added filter apply capability to html content


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/febf20c0
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/febf20c0
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/febf20c0

Branch: refs/heads/master
Commit: febf20c0570bdcff727e0053b895cc41b00f1a30
Parents: 5cb02e2
Author: Sumit Gupta <sumit@apache.org>
Authored: Sat Oct 15 07:19:42 2016 -0400
Committer: Sumit Gupta <sumit@apache.org>
Committed: Sat Oct 15 07:19:42 2016 -0400

----------------------------------------------------------------------
 .../rewrite/impl/html/HtmlFilterReaderBase.java | 20 +++++++-
 .../api/UrlRewriteServletFilterTest.java        | 51 ++++++++++++++++++++
 .../api/UrlRewriteServletFilterTest/rewrite.xml | 11 +++++
 3 files changed, 81 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/febf20c0/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/html/HtmlFilterReaderBase.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/html/HtmlFilterReaderBase.java
b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/html/HtmlFilterReaderBase.java
index 6a7071f..a6222a8 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/html/HtmlFilterReaderBase.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/html/HtmlFilterReaderBase.java
@@ -24,6 +24,7 @@ import net.htmlparser.jericho.Segment;
 import net.htmlparser.jericho.StartTag;
 import net.htmlparser.jericho.StreamedSource;
 import net.htmlparser.jericho.Tag;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor;
 import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor;
 import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor;
 import org.apache.hadoop.gateway.filter.rewrite.i18n.UrlRewriteMessages;
@@ -43,6 +44,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Stack;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public abstract class HtmlFilterReaderBase extends Reader implements UrlRewriteFilterReader
{
@@ -173,7 +175,8 @@ public abstract class HtmlFilterReaderBase extends Reader implements UrlRewriteF
       String outputValue = inputValue;
       try {
         Level tag = stack.peek();
-        outputValue = filterAttribute( tag.getQName(), tag.getQName( attribute.getName()
), inputValue, null );
+        String name = getRuleName(inputValue);
+        outputValue = filterAttribute( tag.getQName(), tag.getQName( attribute.getName()
), inputValue, name );
         if( outputValue == null ) {
           outputValue = inputValue;
         }
@@ -187,6 +190,21 @@ public abstract class HtmlFilterReaderBase extends Reader implements
UrlRewriteF
     }
   }
 
+  private String getRuleName(String inputValue) {
+    if( config != null && !config.getSelectors().isEmpty() ) {
+      for( UrlRewriteFilterPathDescriptor selector : config.getSelectors() ) {
+        if ( selector instanceof UrlRewriteFilterApplyDescriptor ) {
+          UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor)selector;
+          Matcher matcher = apply.compiledPath( REGEX_COMPILER ).matcher( inputValue );
+            if (matcher.matches()) {
+              return apply.rule();
+            }
+          }
+        }
+      }
+      return null;
+    }
+
   private void processText( Segment segment ) {
     String inputValue = segment.toString();
     String outputValue = inputValue;

http://git-wip-us.apache.org/repos/asf/knox/blob/febf20c0/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java
b/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java
index 8167d55..54fdfa3 100644
--- a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java
+++ b/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java
@@ -675,6 +675,57 @@ public class UrlRewriteServletFilterTest {
   }
 
   @Test
+  public void testResponseHtmlBodyRewriteWithFilterInitParam() throws Exception {
+    Map<String,String> initParams = new HashMap<String,String>();
+    //initParams.put( "url, "" );
+    initParams.put( "response.body", "test-filter-4" );
+    //initParams.put( "response", "" );
+    setUp( initParams );
+
+    String responseHtml = "<!DOCTYPE html>\n" +
+        "<html>\n" +
+        "  <head>\n" +
+        "    <meta charset=\"UTF-8\">\n" +
+        "    <link rel=\"stylesheet\" href=\"pretty.css\">\n" +
+        "    <script src=\"script.js\"></script>\n" +
+        "  </head>\n" +
+        "  <body>\n" +
+        "  </body>\n" +
+        "</html>";
+    String rewrittenResponseHtml = "<!DOCTYPE html>\n" +
+        "<html>\n" +
+        "  <head>\n" +
+        "    <meta charset=\"UTF-8\">\n" +
+        "    <link rel=\"stylesheet\" href=\"http://someotherhost/stylesheets/pretty.css\">\n"
+
+        "    <script src=\"script.js\"></script>\n" +
+        "  </head>\n" +
+        "  <body>\n" +
+        "  </body>\n" +
+        "</html>";
+
+    // Setup the server side request/response interaction.
+    interaction.expect()
+        .method( "GET" )
+        .requestUrl( "http://mock-host:42/test-output-path-1" );
+    interaction.respond()
+        .contentType( "application/html" )
+        .content( responseHtml, Charset.forName( "UTF-8" ) )
+        .status( 200 );
+    interactions.add( interaction );
+    request.setMethod( "GET" );
+    request.setURI( "/test-input-path" );
+    request.setHeader( "Host", "mock-host:42" );
+    request.setHeader( "Content-Type", "application/html" );
+
+    // Execute the request.
+    response = TestUtils.execute( server, request );
+
+    assertThat( response.getStatus(), is( 200 ) );
+    String content = response.getContent();
+    assertThat(content, is(rewrittenResponseHtml));
+  }
+
+  @Test
   public void testResponseXmlBodyRewriteWithFilterInitParam() throws Exception {
     Map<String,String> initParams = new HashMap<String,String>();
     initParams.put( "response.body", "test-filter-2" );

http://git-wip-us.apache.org/repos/asf/knox/blob/febf20c0/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml
b/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml
index ad51cc8..e3e9c63 100644
--- a/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml
+++ b/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml
@@ -49,6 +49,10 @@
         <rewrite template="test-protocol://{$serviceAddr[NAMENODE]}/test-path"/>
     </rule>
 
+    <rule name="test-rule-html-attribute">
+        <rewrite template="http://someotherhost/stylesheets/pretty.css"/>
+    </rule>
+
     <filter name="test-filter-1">
         <content type="application/json">
             <apply path="$.url" rule="test-rule-1"/>
@@ -81,4 +85,11 @@
         </content>
     </filter>
 
+    <filter name="test-filter-4">
+        <content type="*/html">
+            <apply path="pretty.css" rule="test-rule-html-attribute"/>
+        </content>
+    </filter>
+
+
 </rules>
\ No newline at end of file


Mime
View raw message