knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lmc...@apache.org
Subject knox git commit: KNOX-1049 - Default Service or App Context for Topologies
Date Wed, 20 Sep 2017 00:01:02 GMT
Repository: knox
Updated Branches:
  refs/heads/KNOX-1049 [created] 3a411555f


KNOX-1049 - Default Service or App Context for Topologies

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

Branch: refs/heads/KNOX-1049
Commit: 3a411555fa9a89965281604c4e56709325abf743
Parents: a5a8825
Author: Larry McCay <lmccay@hortonworks.com>
Authored: Tue Sep 19 20:00:24 2017 -0400
Committer: Larry McCay <lmccay@hortonworks.com>
Committed: Tue Sep 19 20:00:24 2017 -0400

----------------------------------------------------------------------
 .../apache/hadoop/gateway/GatewayFilter.java    | 64 +++++++++++++++++++-
 .../builder/BeanPropertyTopologyBuilder.java    | 11 ++++
 .../xml/KnoxFormatXmlTopologyRules.java         |  2 +
 .../src/main/resources/conf/topology-v1.xsd     |  1 +
 .../service/admin/TopologiesResource.java       | 11 ++++
 .../service/admin/beans/BeanConverter.java      |  2 +
 .../gateway/service/admin/beans/Topology.java   | 11 ++++
 .../hadoop/gateway/topology/Topology.java       |  9 +++
 .../gateway/topology/topology_binding-xml.xml   |  3 +-
 9 files changed, 112 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/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 c92ebfe..2885fe3 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
@@ -31,6 +31,7 @@ import org.apache.hadoop.gateway.config.GatewayConfig;
 import org.apache.hadoop.gateway.filter.AbstractGatewayFilter;
 import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
 import org.apache.hadoop.gateway.i18n.resources.ResourcesFactory;
+import org.apache.hadoop.gateway.topology.Topology;
 import org.apache.hadoop.gateway.util.urltemplate.Matcher;
 import org.apache.hadoop.gateway.util.urltemplate.Parser;
 import org.apache.hadoop.gateway.util.urltemplate.Template;
@@ -43,6 +44,7 @@ import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
 import java.io.IOException;
@@ -122,7 +124,35 @@ public class GatewayFilter implements Filter {
         AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME, contextWithPathAndQuery
);
 
     Matcher<Chain>.Match match = chains.match( pathWithQueryTemplate );
-    
+
+    // if there was no match then look for a default service for the topology
+    if (match == null) {
+      Topology topology = (Topology) servletRequest.getServletContext().getAttribute("org.apache.hadoop.gateway.topology");
+      String defaultServicePath = topology.getDefaultServicePath();
+      if (defaultServicePath != null) {
+        try {
+          String newPathWithQuery = defaultServicePath + "/" + pathWithQueryTemplate;
+          match = chains.match(Parser.parseLiteral(newPathWithQuery));
+          String origUrl = ((HttpServletRequest) servletRequest).getRequestURL().toString();
+          String url = origUrl;
+          if (path.equals("/")) {
+            url += defaultServicePath;
+          }
+          else {
+            int index = origUrl.indexOf(path);
+            url = origUrl.substring(0, index) + "/" + defaultServicePath + path;
+          }
+          String contextPath = defaultServicePath;
+          servletRequest = new ForwardedRequest((HttpServletRequest) servletRequest, 
+              contextPath, 
+              url);
+        } catch (URISyntaxException e) {
+          throw new ServletException( e );
+        }
+//        ((HttpServletRequest) servletRequest).getRequestURL();
+      }
+    }
+
     assignCorrelationRequestId();
     // Populate Audit/correlation parameters
     AuditContext auditContext = auditService.getContext();
@@ -387,4 +417,36 @@ public class GatewayFilter implements Filter {
 
   }
 
+  /**
+   * A request wrapper class that wraps a request and adds the context path if
+   * needed.
+   */
+  static class ForwardedRequest extends HttpServletRequestWrapper {
+
+    private String newURL;
+    private String contextpath;
+
+    public ForwardedRequest(final HttpServletRequest request,
+        final String contextpath, final String newURL) {
+      super(request);
+      this.newURL = newURL;
+      this.contextpath = contextpath;
+    }
+
+    @Override
+    public StringBuffer getRequestURL() {
+      return new StringBuffer(newURL);
+    }
+
+    @Override
+    public String getRequestURI() {
+      return newURL;
+    }
+
+    @Override
+    public String getContextPath() {
+      return super.getContextPath() + "/" + this.contextpath;
+    }
+
+  }
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/builder/BeanPropertyTopologyBuilder.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/builder/BeanPropertyTopologyBuilder.java
b/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/builder/BeanPropertyTopologyBuilder.java
index b5e80d2..b33e52c 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/builder/BeanPropertyTopologyBuilder.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/builder/BeanPropertyTopologyBuilder.java
@@ -27,6 +27,7 @@ import org.apache.hadoop.gateway.topology.Topology;
 public class BeanPropertyTopologyBuilder implements TopologyBuilder {
 
     private String name;
+    private String defaultService;
     private List<Provider> providers;
     private List<Service> services;
     private List<Application> applications;
@@ -46,6 +47,15 @@ public class BeanPropertyTopologyBuilder implements TopologyBuilder {
         return name;
     }
 
+    public BeanPropertyTopologyBuilder defaultService(String defaultService) {
+      this.defaultService = defaultService;
+      return this;
+    }
+
+    public String defaultService() {
+      return defaultService;
+    }
+
     public BeanPropertyTopologyBuilder addProvider(Provider provider) {
         providers.add(provider);
         return this;
@@ -76,6 +86,7 @@ public class BeanPropertyTopologyBuilder implements TopologyBuilder {
     public Topology build() {
         Topology topology = new Topology();
         topology.setName(name);
+        topology.setDefaultServicePath(defaultService);
 
           for (Provider provider : providers) {
             topology.addProvider(provider);

http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/xml/KnoxFormatXmlTopologyRules.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/xml/KnoxFormatXmlTopologyRules.java
b/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/xml/KnoxFormatXmlTopologyRules.java
index e573d63..e221507 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/xml/KnoxFormatXmlTopologyRules.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/topology/xml/KnoxFormatXmlTopologyRules.java
@@ -32,6 +32,7 @@ public class KnoxFormatXmlTopologyRules extends AbstractRulesModule {
   private static final String ROOT_TAG = "topology";
   private static final String NAME_TAG = "name";
   private static final String VERSION_TAG = "version";
+  private static final String DEFAULT_SERVICE_TAG = "default-service";
   private static final String APPLICATION_TAG = "application";
   private static final String SERVICE_TAG = "service";
   private static final String ROLE_TAG = "role";
@@ -48,6 +49,7 @@ public class KnoxFormatXmlTopologyRules extends AbstractRulesModule {
     forPattern( ROOT_TAG ).createObject().ofType( BeanPropertyTopologyBuilder.class );
     forPattern( ROOT_TAG + "/" + NAME_TAG ).callMethod("name").usingElementBodyAsArgument();
     forPattern( ROOT_TAG + "/" + VERSION_TAG ).callMethod("version").usingElementBodyAsArgument();
+    forPattern( ROOT_TAG + "/" + DEFAULT_SERVICE_TAG ).callMethod("defaultService").usingElementBodyAsArgument();
 
     forPattern( ROOT_TAG + "/" + APPLICATION_TAG ).createObject().ofType( Application.class
).then().setNext( "addApplication" );
     forPattern( ROOT_TAG + "/" + APPLICATION_TAG + "/" + ROLE_TAG ).setBeanProperty();

http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/gateway-server/src/main/resources/conf/topology-v1.xsd
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/resources/conf/topology-v1.xsd b/gateway-server/src/main/resources/conf/topology-v1.xsd
index 9e3bed6..ab07caa 100644
--- a/gateway-server/src/main/resources/conf/topology-v1.xsd
+++ b/gateway-server/src/main/resources/conf/topology-v1.xsd
@@ -21,6 +21,7 @@ limitations under the License.
             <h:sequence maxOccurs="1">
 
                 <h:element name="name" minOccurs="0" maxOccurs="1"/>
+                <h:element name="default-service" minOccurs="0" maxOccurs="1"/>
 
                 <h:element name="gateway" minOccurs="0" maxOccurs="1">
                     <h:complexType>

http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/TopologiesResource.java
----------------------------------------------------------------------
diff --git a/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/TopologiesResource.java
b/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/TopologiesResource.java
index 1504eca..4748047 100644
--- a/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/TopologiesResource.java
+++ b/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/TopologiesResource.java
@@ -245,6 +245,8 @@ public class TopologiesResource {
     @XmlElement
     private String timestamp;
     @XmlElement
+    private String defaultServicePath;
+    @XmlElement
     private String uri;
     @XmlElement
     private String href;
@@ -254,6 +256,7 @@ public class TopologiesResource {
     public SimpleTopology(org.apache.hadoop.gateway.topology.Topology t, String uri, String
href) {
       this.name = t.getName();
       this.timestamp = Long.toString(t.getTimestamp());
+      this.defaultServicePath = t.getDefaultServicePath();
       this.uri = uri;
       this.href = href;
     }
@@ -270,6 +273,14 @@ public class TopologiesResource {
       return timestamp;
     }
 
+    public void setDefaultService(String defaultServicePath) {
+      this.defaultServicePath = defaultServicePath;
+    }
+
+    public String getDefaultService() {
+      return defaultServicePath;
+    }
+
     public void setTimestamp(String timestamp) {
       this.timestamp = timestamp;
     }

http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/BeanConverter.java
----------------------------------------------------------------------
diff --git a/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/BeanConverter.java
b/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/BeanConverter.java
index e0c9d06..ac3b19c 100644
--- a/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/BeanConverter.java
+++ b/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/BeanConverter.java
@@ -27,6 +27,7 @@ public class BeanConverter {
     Topology topologyResource = new Topology();
     topologyResource.setName(topology.getName());
     topologyResource.setTimestamp(topology.getTimestamp());
+    topologyResource.setDefaultService(topology.getDefaultServicePath());
     topologyResource.setUri(topology.getUri());
     for ( org.apache.hadoop.gateway.topology.Provider provider : topology.getProviders()
) {
       topologyResource.getProviders().add( getProvider(provider) );
@@ -44,6 +45,7 @@ public class BeanConverter {
     org.apache.hadoop.gateway.topology.Topology deploymentTopology = new org.apache.hadoop.gateway.topology.Topology();
     deploymentTopology.setName(topology.getName());
     deploymentTopology.setTimestamp(topology.getTimestamp());
+    deploymentTopology.setDefaultServicePath(topology.getDefaultService());
     deploymentTopology.setUri(topology.getUri());
     for ( Provider provider : topology.getProviders() ) {
       deploymentTopology.addProvider( getProvider(provider) );

http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/Topology.java
----------------------------------------------------------------------
diff --git a/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/Topology.java
b/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/Topology.java
index 6e85b7a..8bc5fa7 100644
--- a/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/Topology.java
+++ b/gateway-service-admin/src/main/java/org/apache/hadoop/gateway/service/admin/beans/Topology.java
@@ -34,6 +34,9 @@ public class Topology {
   private String name;
 
   @XmlElement
+  private String defaultServicePath;
+
+  @XmlElement
   private long timestamp;
 
   @XmlElement(name="provider")
@@ -69,6 +72,14 @@ public class Topology {
     return timestamp;
   }
 
+  public void setDefaultService( String defaultServicePath ) {
+    this.defaultServicePath = defaultServicePath;
+  }
+
+  public String getDefaultService() {
+    return defaultServicePath;
+  }
+
   public void setTimestamp( long timestamp ) {
     this.timestamp = timestamp;
   }

http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/gateway-spi/src/main/java/org/apache/hadoop/gateway/topology/Topology.java
----------------------------------------------------------------------
diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/topology/Topology.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/topology/Topology.java
index 7f37336..c366421 100644
--- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/topology/Topology.java
+++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/topology/Topology.java
@@ -31,6 +31,7 @@ public class Topology {
 
   private URI uri;
   private String name;
+  private String defaultServicePath = null;
   private long timestamp;
   public List<Provider> providerList = new ArrayList<Provider>();
   private Map<String,Map<String,Provider>> providerMap = new HashMap<>();
@@ -67,6 +68,14 @@ public class Topology {
     this.timestamp = timestamp;
   }
 
+  public String getDefaultServicePath() {
+    return defaultServicePath;
+  }
+
+  public void setDefaultServicePath(String servicePath) {
+    defaultServicePath = servicePath;
+  }
+
   public Collection<Service> getServices() {
     return services;
   }

http://git-wip-us.apache.org/repos/asf/knox/blob/3a411555/gateway-spi/src/main/resources/org/apache/hadoop/gateway/topology/topology_binding-xml.xml
----------------------------------------------------------------------
diff --git a/gateway-spi/src/main/resources/org/apache/hadoop/gateway/topology/topology_binding-xml.xml
b/gateway-spi/src/main/resources/org/apache/hadoop/gateway/topology/topology_binding-xml.xml
index 50d8d58..8c54ed7 100644
--- a/gateway-spi/src/main/resources/org/apache/hadoop/gateway/topology/topology_binding-xml.xml
+++ b/gateway-spi/src/main/resources/org/apache/hadoop/gateway/topology/topology_binding-xml.xml
@@ -24,10 +24,11 @@ or more contributor license agreements.  See the NOTICE file
         element-form-default="QUALIFIED"/>
     <java-types>
         <java-type name="Topology" xml-accessor-type="NONE">
-            <xml-type prop-order="name providers services applications"/>
+            <xml-type prop-order="name defaultServicePath providers services applications"/>
             <xml-root-element/>
             <java-attributes>
                 <xml-element java-attribute="name" name="name"/>
+                <xml-element java-attribute="defaultServicePath" name="default-service"/>
                 <xml-elements java-attribute="providers">
                     <xml-element name="provider"/>
                     <xml-element-wrapper name="gateway"/>


Mime
View raw message