Added HA support and URL-level dispatch for webhdfs KNOX-483

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

Branch: refs/heads/KNOX-481
Commit: 435fcd50b79af4b38a33df0514cf095dd06c2542
Parents: aba18bf
Author: Sumit Gupta <su...@apache.org>
Authored: Tue Jan 20 11:16:00 2015 -0500
Committer: Sumit Gupta <su...@apache.org>
Committed: Tue Feb 17 22:34:52 2015 -0500

----------------------------------------------------------------------
 .../ServiceDefinitionDeploymentContributor.java | 51 +++++++++++++++-----
 .../gateway/service/definition/UrlBinding.java  | 11 +++++
 2 files changed, 50 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/435fcd50/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ServiceDefinitionDeploymentContributor.java
----------------------------------------------------------------------
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ServiceDefinitionDeploymentContributor.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ServiceDefinitionDeploymentContributor.java
index 79cc4a5..755fc67 100644
--- 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ServiceDefinitionDeploymentContributor.java
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/ServiceDefinitionDeploymentContributor.java
@@ -28,6 +28,7 @@ import 
org.apache.hadoop.gateway.service.definition.CustomDispatch;
 import org.apache.hadoop.gateway.service.definition.RewriteFilter;
 import org.apache.hadoop.gateway.service.definition.ServiceDefinition;
 import org.apache.hadoop.gateway.service.definition.UrlBinding;
+import org.apache.hadoop.gateway.topology.Provider;
 import org.apache.hadoop.gateway.topology.Service;
 
 import java.net.URISyntaxException;
@@ -86,7 +87,7 @@ public class ServiceDefinitionDeploymentContributor extends 
ServiceDeploymentCon
         }
       }
       try {
-        contributeResource(context, service, binding.getPattern(), 
filterParams);
+        contributeResource(context, service, binding, filterParams);
       } catch ( URISyntaxException e ) {
         e.printStackTrace();
       }
@@ -94,11 +95,11 @@ public class ServiceDefinitionDeploymentContributor extends 
ServiceDeploymentCon
 
   }
 
-  private void contributeResource(DeploymentContext context, Service service, 
String pattern, Map<String, String> filterParams) throws URISyntaxException {
+  private void contributeResource(DeploymentContext context, Service service, 
UrlBinding binding, Map<String, String> filterParams) throws URISyntaxException 
{
     List<FilterParamDescriptor> params = new 
ArrayList<FilterParamDescriptor>();
     ResourceDescriptor resource = context.getGatewayDescriptor().addResource();
     resource.role(service.getRole());
-    resource.pattern(pattern);
+    resource.pattern(binding.getPattern());
     addWebAppSecFilters(context, service, resource);
     addAuthenticationFilter(context, service, resource);
     addIdentityAssertionFilter(context, service, resource);
@@ -109,20 +110,46 @@ public class ServiceDefinitionDeploymentContributor 
extends ServiceDeploymentCon
       }
     }
     addRewriteFilter(context, service, resource, params);
-    CustomDispatch customDispatch = serviceDefinition.getDispatch();
-    if (customDispatch != null) {
-      String contributorName = customDispatch.getContributorName();
-      if (contributorName != null) {
-        addDispatchFilter(context, service, resource, DISPATCH_ROLE, 
contributorName);
+    addDispatchFilter(context, service, resource, binding);
+  }
+
+  private void addDispatchFilter(DeploymentContext context, Service service, 
ResourceDescriptor resource, UrlBinding binding) {
+    CustomDispatch customDispatch = binding.getDispatch();
+    if ( customDispatch == null ) {
+      customDispatch = serviceDefinition.getDispatch();
+    }
+    if ( customDispatch != null ) {
+      boolean isHaEnabled = isHaEnabled(context);
+      if ( isHaEnabled && (customDispatch.getHaContributorName() != null) ) {
+        addDispatchFilter(context, service, resource, DISPATCH_ROLE, 
customDispatch.getHaContributorName());
       } else {
-        String className = customDispatch.getClassName();
-        if (className != null) {
-          FilterDescriptor filter = resource.addFilter().name( getName() 
).role( DISPATCH_ROLE ).impl( GatewayDispatchFilter.class );
-          filter.param().name(DISPATCH_IMPL_PARAM).value(className);
+        String contributorName = customDispatch.getContributorName();
+        if ( contributorName != null ) {
+          addDispatchFilter(context, service, resource, DISPATCH_ROLE, 
contributorName);
+        } else {
+          String className = customDispatch.getClassName();
+          if ( className != null ) {
+            FilterDescriptor filter = 
resource.addFilter().name(getName()).role(DISPATCH_ROLE).impl(GatewayDispatchFilter.class);
+            filter.param().name(DISPATCH_IMPL_PARAM).value(className);
+          }
         }
       }
     } else {
       addDispatchFilter(context, service, resource, DISPATCH_ROLE, 
"http-client");
     }
   }
+
+  private boolean isHaEnabled(DeploymentContext context) {
+    Provider provider = getProviderByRole(context, "ha");
+    if ( provider != null && provider.isEnabled() ) {
+      Map<String, String> params = provider.getParams();
+      if ( params != null ) {
+        if ( params.containsKey(getRole()) ) {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/435fcd50/gateway-service-definitions/src/main/java/org/apache/hadoop/gateway/service/definition/UrlBinding.java
----------------------------------------------------------------------
diff --git 
a/gateway-service-definitions/src/main/java/org/apache/hadoop/gateway/service/definition/UrlBinding.java
 
b/gateway-service-definitions/src/main/java/org/apache/hadoop/gateway/service/definition/UrlBinding.java
index 3630ebf..c64658c 100644
--- 
a/gateway-service-definitions/src/main/java/org/apache/hadoop/gateway/service/definition/UrlBinding.java
+++ 
b/gateway-service-definitions/src/main/java/org/apache/hadoop/gateway/service/definition/UrlBinding.java
@@ -29,6 +29,8 @@ public class UrlBinding {
 
   private List<RewriteFilter> rewriteFilters;
 
+  private CustomDispatch dispatch;
+
   @XmlAttribute
   public String getPattern() {
     return pattern;
@@ -46,4 +48,13 @@ public class UrlBinding {
   public void setRewriteFilters(List<RewriteFilter> rewriteFilters) {
     this.rewriteFilters = rewriteFilters;
   }
+
+  @XmlElement(name = "dispatch")
+  public CustomDispatch getDispatch() {
+    return dispatch;
+  }
+
+  public void setDispatch(CustomDispatch dispatch) {
+    this.dispatch = dispatch;
+  }
 }

Reply via email to