knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmin...@apache.org
Subject git commit: KNOX-401: Add service role request attribute
Date Mon, 07 Jul 2014 22:03:41 GMT
Repository: knox
Updated Branches:
  refs/heads/master c062ee1d4 -> 66bc41a8e


KNOX-401: Add service role request attribute


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

Branch: refs/heads/master
Commit: 66bc41a8ef8b53e9aaafc2cef09f165677d5121e
Parents: c062ee1
Author: Kevin Minder <kevin.minder@hortonworks.com>
Authored: Mon Jul 7 18:03:24 2014 -0400
Committer: Kevin Minder <kevin.minder@hortonworks.com>
Committed: Mon Jul 7 18:03:24 2014 -0400

----------------------------------------------------------------------
 .../apache/hadoop/gateway/GatewayFilter.java    |  8 +++-
 .../hadoop/gateway/GatewayFilterTest.java       | 43 ++++++++++++++++++++
 .../gateway/filter/AbstractGatewayFilter.java   |  1 +
 3 files changed, 50 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/66bc41a8/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayFilter.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayFilter.java b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayFilter.java
index 35d35fa..da903a8 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayFilter.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayFilter.java
@@ -124,6 +124,7 @@ public class GatewayFilter implements Filter {
     
     if( match != null ) {
       Chain chain = match.getValue();
+      servletRequest.setAttribute( AbstractGatewayFilter.TARGET_SERVICE_ROLE, chain.getResourceRole()
);
       try {
         chain.doFilter( servletRequest, servletResponse );
       } catch( IOException e ) {
@@ -209,7 +210,9 @@ public class GatewayFilter implements Filter {
 
     public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse
) throws IOException, ServletException {
       if( chain != null && !chain.isEmpty() ) {
-        chain.get( 0 ).doFilter( servletRequest, servletResponse, subChain() );
+        final Filter filter = chain.get( 0 );
+        final FilterChain chain = subChain();
+        filter.doFilter( servletRequest, servletResponse, chain );
       }
     }
 
@@ -308,7 +311,8 @@ public class GatewayFilter implements Filter {
 
     @Override
     public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain ) throws IOException, ServletException {
-      getInstance().doFilter( servletRequest, servletResponse, filterChain );
+      final Filter filter = getInstance();
+      filter.doFilter( servletRequest, servletResponse, filterChain );
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/knox/blob/66bc41a8/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayFilterTest.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayFilterTest.java
b/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayFilterTest.java
index 0ce0886..973fd99 100644
--- a/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayFilterTest.java
+++ b/gateway-server/src/test/java/org/apache/hadoop/gateway/GatewayFilterTest.java
@@ -17,6 +17,7 @@
  */
 package org.apache.hadoop.gateway;
 
+import org.apache.hadoop.gateway.filter.AbstractGatewayFilter;
 import org.apache.hadoop.test.category.FastTests;
 import org.apache.hadoop.test.category.UnitTests;
 import org.easymock.EasyMock;
@@ -29,6 +30,9 @@ import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.net.URISyntaxException;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
 /**
  *
  */
@@ -84,4 +88,43 @@ public class GatewayFilterTest {
 
   }
 
+  public static class TestRoleFilter extends AbstractGatewayFilter {
+
+    public Object role;
+
+    @Override
+    protected void doFilter( HttpServletRequest request, HttpServletResponse response, FilterChain
chain ) throws IOException, ServletException {
+      this.role = request.getAttribute( AbstractGatewayFilter.TARGET_SERVICE_ROLE );
+    }
+
+  }
+
+  @Test
+  public void testTargetServiceRoleRequestAttribute() throws Exception {
+
+    FilterConfig config = EasyMock.createNiceMock( FilterConfig.class );
+    EasyMock.replay( config );
+
+    HttpServletRequest request = EasyMock.createNiceMock( HttpServletRequest.class );
+    EasyMock.expect( request.getPathInfo() ).andReturn( "test-path/test-resource" ).anyTimes();
+    request.setAttribute( AbstractGatewayFilter.TARGET_SERVICE_ROLE, "test-role" );
+    EasyMock.expectLastCall().anyTimes();
+    EasyMock.expect( request.getAttribute( AbstractGatewayFilter.TARGET_SERVICE_ROLE ) ).andReturn(
"test-role" ).anyTimes();
+    EasyMock.replay( request );
+
+    HttpServletResponse response = EasyMock.createNiceMock( HttpServletResponse.class );
+    EasyMock.replay( response );
+
+    TestRoleFilter filter = new TestRoleFilter();
+
+    GatewayFilter gateway = new GatewayFilter();
+    gateway.addFilter( "test-path/**", "test-filter", filter, null, "test-role" );
+    gateway.init( config );
+    gateway.doFilter( request, response );
+    gateway.destroy();
+
+    assertThat( (String)filter.role, is( "test-role" ) );
+
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/66bc41a8/gateway-spi/src/main/java/org/apache/hadoop/gateway/filter/AbstractGatewayFilter.java
----------------------------------------------------------------------
diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/filter/AbstractGatewayFilter.java
b/gateway-spi/src/main/java/org/apache/hadoop/gateway/filter/AbstractGatewayFilter.java
index 6ce985d..db9e475 100644
--- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/filter/AbstractGatewayFilter.java
+++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/filter/AbstractGatewayFilter.java
@@ -39,6 +39,7 @@ public abstract class AbstractGatewayFilter implements Filter {
   public static final String SOURCE_REQUEST_URL_ATTRIBUTE_NAME = "sourceRequestUrl";
   public static final String TARGET_REQUEST_URL_ATTRIBUTE_NAME = "targetRequestUrl";
   public static final String SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME = "sourceRequestContextUrl";
+  public static final String TARGET_SERVICE_ROLE = "targetServiceRole";
 //  public static final String RESPONSE_STREAMER_ATTRIBUTE_NAME = "responseStreamer";
   private static final GatewaySpiMessages LOG = MessagesFactory.get( GatewaySpiMessages.class
);
 


Mime
View raw message