You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by km...@apache.org on 2013/08/20 00:47:14 UTC
git commit: KNOX-77: Update HDFS and HBase to use specific rule for
Location header rewrite.
Updated Branches:
refs/heads/master 7c91f0441 -> 436d656c7
KNOX-77: Update HDFS and HBase to use specific rule for Location header rewrite.
Project: http://git-wip-us.apache.org/repos/asf/incubator-knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-knox/commit/436d656c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-knox/tree/436d656c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-knox/diff/436d656c
Branch: refs/heads/master
Commit: 436d656c76d82c1c1145e420c4ce1c4d0a7f36a0
Parents: 7c91f04
Author: Kevin Minder <ke...@hortonworks.com>
Authored: Mon Aug 19 18:44:21 2013 -0400
Committer: Kevin Minder <ke...@hortonworks.com>
Committed: Mon Aug 19 18:44:21 2013 -0400
----------------------------------------------------------------------
.../filter/rewrite/api/UrlRewriteProcessor.java | 21 ++++++------
.../ext/UrlRewriteMatchDescriptorExt.java | 7 +++-
.../ext/UrlRewriteMatchProcessorExt.java | 23 ++++++++-----
.../filter/rewrite/impl/UrlRewriteResponse.java | 9 +++--
.../rewrite/api/UrlRewriteProcessorTest.java | 22 ++++++++++++
.../api/UrlRewriteProcessorTest/rewrite.xml | 6 ++++
.../hbase/HbaseDeploymentContributor.java | 35 ++++++++++++++------
.../gateway/hdfs/HdfsDeploymentContributor.java | 30 ++++++++++++-----
.../hadoop/gateway/GatewayBasicFuncTest.java | 24 +++++---------
.../gateway/util/urltemplate/Builder.java | 8 +++++
.../gateway/util/urltemplate/Expander.java | 7 ++--
.../gateway/util/urltemplate/ExpanderTest.java | 2 +-
.../gateway/util/urltemplate/MatcherTest.java | 27 +++++++++++++++
.../gateway/util/urltemplate/ParserTest.java | 21 ++++++++++++
.../gateway/util/urltemplate/RewriterTest.java | 17 ++++++++++
15 files changed, 200 insertions(+), 59 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessor.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessor.java
index d9db3c6..b8f95bf 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessor.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessor.java
@@ -85,18 +85,20 @@ public class UrlRewriteProcessor implements UrlRewriter {
try {
UrlRewriteStepProcessorHolder ruleProcessor = new UrlRewriteStepProcessorHolder();
ruleProcessor.initialize( environment, ruleDescriptor );
- Template template = ruleDescriptor.template();
if( !rules.containsKey( ruleDescriptor.name() ) ) {
rules.put( ruleDescriptor.name(), ruleProcessor );
}
- EnumSet<Direction> directions = ruleDescriptor.directions();
- if( directions == null || directions.isEmpty() ) {
- inbound.add( template, ruleProcessor );
- outbound.add( template, ruleProcessor );
- } else if( directions.contains( IN ) ) {
- inbound.add( template, ruleProcessor );
- } else if ( directions.contains( OUT ) ) {
- outbound.add( template, ruleProcessor );
+ Template template = ruleDescriptor.template();
+ if( template != null ) {
+ EnumSet<Direction> directions = ruleDescriptor.directions();
+ if( directions == null || directions.isEmpty() ) {
+ inbound.add( template, ruleProcessor );
+ outbound.add( template, ruleProcessor );
+ } else if( directions.contains( IN ) ) {
+ inbound.add( template, ruleProcessor );
+ } else if ( directions.contains( OUT ) ) {
+ outbound.add( template, ruleProcessor );
+ }
}
} catch( Exception e ) {
LOG.failedToInitializeRewriteRules( e );
@@ -153,7 +155,6 @@ public class UrlRewriteProcessor implements UrlRewriter {
outputUri = null;
}
} catch( Exception e ) {
- e.printStackTrace();
LOG.failedToRewriteUrl( e );
outputUri = null;
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java
index a95fee8..3cb8829 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchDescriptorExt.java
@@ -98,7 +98,12 @@ public class UrlRewriteMatchDescriptorExt
@Override
public UrlRewriteMatchDescriptor template( Template template ) {
this.template = template;
- this.pattern = template.toString();
+ // The template is now optional for rules.
+ if( template == null ) {
+ this.pattern = null;
+ } else {
+ this.pattern = template.toString();
+ }
return this;
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java
index bc10643..04f919a 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/ext/UrlRewriteMatchProcessorExt.java
@@ -22,6 +22,7 @@ import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStepProcessor;
import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
import org.apache.hadoop.gateway.util.urltemplate.Matcher;
+import org.apache.hadoop.gateway.util.urltemplate.Template;
public class UrlRewriteMatchProcessorExt implements UrlRewriteStepProcessor<UrlRewriteMatchDescriptor> {
@@ -35,24 +36,30 @@ public class UrlRewriteMatchProcessorExt implements UrlRewriteStepProcessor<UrlR
@Override
public void initialize( UrlRewriteEnvironment environment, UrlRewriteMatchDescriptor descriptor ) throws Exception {
- //this.descriptor = descriptor;
- this.matcher = new Matcher<Void>( descriptor.template(), null );
+ Template template = descriptor.template();
+ if( template == null ) {
+ this.matcher = null;
+ } else {
+ this.matcher = new Matcher<Void>( descriptor.template(), null );
+ }
}
@Override
public UrlRewriteStepStatus process( UrlRewriteContext context ) throws Exception {
- UrlRewriteStepStatus status = UrlRewriteStepStatus.FAILURE;
- Matcher.Match match = matcher.match( context.getCurrentUrl() );
- if( match != null ) {
- context.addParameters( match.getParams() );
- status = UrlRewriteStepStatus.SUCCESS;
+ UrlRewriteStepStatus status = UrlRewriteStepStatus.SUCCESS;
+ if( matcher != null ) {
+ status = UrlRewriteStepStatus.FAILURE;
+ Matcher.Match match = matcher.match( context.getCurrentUrl() );
+ if( match != null ) {
+ context.addParameters( match.getParams() );
+ status = UrlRewriteStepStatus.SUCCESS;
+ }
}
return status;
}
@Override
public void destroy() {
- //descriptor = null;
matcher = null;
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java
index de7075c..2c14b9f 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java
@@ -108,7 +108,9 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements Params
try {
Template input = Parser.parse( value );
Template output = rewriter.rewrite( this, input, UrlRewriter.Direction.OUT, rule );
- value = output.toString();
+ if( output != null ) {
+ value = output.toString();
+ }
} catch( URISyntaxException e ) {
LOG.failedToParseValueForUrlRewrite( value );
}
@@ -127,8 +129,9 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements Params
// Ignore the Content-Length from the dispatch respond since the respond body may be rewritten.
@Override
public void addHeader( String name, String value ) {
- if( !ignoreHeader( name) ) {
- value = rewriteValue( value, pickFirstRuleWithEqualsIgnoreCasePathMatch( headersFilterConfig, name ) );
+ if( !ignoreHeader( name ) ) {
+ String rule = pickFirstRuleWithEqualsIgnoreCasePathMatch( headersFilterConfig, name );
+ value = rewriteValue( value, rule );
super.addHeader( name, value );
}
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java b/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
index 3ebe163..3fdb896 100644
--- a/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
+++ b/gateway-provider-rewrite/src/test/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest.java
@@ -151,4 +151,26 @@ public class UrlRewriteProcessorTest {
processor.destroy();
}
+ @Test
+ public void testRewriteViaRuleWithWildcardTemplateAndOptionalQuery() throws Exception {
+ UrlRewriteEnvironment environment = EasyMock.createNiceMock( UrlRewriteEnvironment.class );
+ HttpServletRequest request = EasyMock.createNiceMock( HttpServletRequest.class );
+ HttpServletResponse response = EasyMock.createNiceMock( HttpServletResponse.class );
+ EasyMock.replay( environment, request, response );
+
+ UrlRewriteProcessor processor = new UrlRewriteProcessor();
+ UrlRewriteRulesDescriptor config = UrlRewriteRulesDescriptorFactory.load(
+ "xml", getTestResourceReader( "rewrite.xml", "UTF-8" ) );
+ processor.initialize( environment, config );
+
+ Template inputUrl = Parser.parse( "test-scheme-input://test-host-input:42/test-path-input-one/test-path-input-two" );
+ Template outputUrl = processor.rewrite( null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-2" );
+
+ assertThat(
+ "Expect rewrite to contain the correct path.",
+ outputUrl.toString(), is( "test-scheme-output://test-host-output:777/test-path-output/test-path-input-one/test-path-input-two" ) );
+
+ processor.destroy();
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite.xml
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite.xml b/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite.xml
index 5e0d5b5..998f54c 100644
--- a/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite.xml
+++ b/gateway-provider-rewrite/src/test/resources/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteProcessorTest/rewrite.xml
@@ -18,4 +18,10 @@
<rule name="test-rule-1" url="{scheme=*}://{host=*}:{port=*}/{path=**}">
<rewrite param="{scheme}://{host}:{port}/test-output-path"/>
</rule>
+
+ <rule dir="OUT" name="test-rule-2">
+ <match pattern="*://*:*/{path=**}?{**}"/>
+ <rewrite template="test-scheme-output://test-host-output:777/test-path-output/{path}?{**}"/>
+ </rule>
+
</rules>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-service-hbase/src/main/java/org/apache/hadoop/gateway/hbase/HbaseDeploymentContributor.java
----------------------------------------------------------------------
diff --git a/gateway-service-hbase/src/main/java/org/apache/hadoop/gateway/hbase/HbaseDeploymentContributor.java b/gateway-service-hbase/src/main/java/org/apache/hadoop/gateway/hbase/HbaseDeploymentContributor.java
index f134973..c6a9567 100644
--- a/gateway-service-hbase/src/main/java/org/apache/hadoop/gateway/hbase/HbaseDeploymentContributor.java
+++ b/gateway-service-hbase/src/main/java/org/apache/hadoop/gateway/hbase/HbaseDeploymentContributor.java
@@ -19,7 +19,10 @@ package org.apache.hadoop.gateway.hbase;
import org.apache.hadoop.gateway.deploy.DeploymentContext;
import org.apache.hadoop.gateway.deploy.ServiceDeploymentContributorBase;
+import org.apache.hadoop.gateway.descriptor.FilterParamDescriptor;
import org.apache.hadoop.gateway.descriptor.ResourceDescriptor;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor;
import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor;
import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor;
import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt;
@@ -27,10 +30,13 @@ import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteMatchDescriptor;
import org.apache.hadoop.gateway.topology.Service;
import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
public class HbaseDeploymentContributor extends ServiceDeploymentContributorBase {
private static final String EXTERNAL_PATH = "/hbase/api";
+ private static final String CLUSTER_URL_FUNCTION = "{gateway.url}";
@Override
public String getRole() {
@@ -54,25 +60,28 @@ public class HbaseDeploymentContributor extends ServiceDeploymentContributorBase
UrlRewriteActionRewriteDescriptorExt rewrite;
UrlRewriteMatchDescriptor match;
- rule = rules.addRule( getRole() + "/" + getName() + "/root/inbound" )
+ rule = rules.addRule( getQualifiedName() + "/root/inbound" )
.directions( "inbound" )
.pattern( "*://*:*/**" + EXTERNAL_PATH + "/?{**}" );
rewrite = rule.addStep( "rewrite" );
rewrite.template( service.getUrl().toExternalForm() + "/?{**}" );
- rule = rules.addRule( getRole() + "/" + getName() + "/root/inbound" )
+ rule = rules.addRule( getQualifiedName() + "/root/inbound" )
.directions( "inbound" )
.pattern( "*://*:*/**" + EXTERNAL_PATH + "/{**}?{**}" );
rewrite = rule.addStep( "rewrite" );
rewrite.template( service.getUrl().toExternalForm() + "/{**}?{**}" );
-// rule = rules.addRule( getRole() + "/" + getName() + "/hbase/outbound" )
-// .directions( "outbound" )
-// .pattern( "*://*:*/**?**" );
-// match = rule.addStep( "match" );
-// match.pattern( "*://{host}:{port}/{path=**}?{**}" );
-// rewrite = rule.addStep( "rewrite" );
-// rewrite.template( service.getUrl().toExternalForm() + "/{path=**}" );
+ rule = rules.addRule( getQualifiedName() + "/hbase/outbound" )
+ .directions( "outbound" );
+ match = rule.addStep( "match" );
+ match.pattern( "*://*:*/{path=**}?{**}" );
+ rewrite = rule.addStep( "rewrite" );
+ rewrite.template( CLUSTER_URL_FUNCTION + EXTERNAL_PATH + "/{path}?{**}" );
+
+ UrlRewriteFilterDescriptor filter = rules.addFilter( getQualifiedName() + "/hbase/outbound" );
+ UrlRewriteFilterContentDescriptor content = filter.addContent( "application/x-http-headers" );
+ content.addApply( "Location", getQualifiedName() + "/hbase/outbound" );
}
private void contributeResources( DeploymentContext context, Service service ) throws URISyntaxException {
@@ -104,7 +113,9 @@ public class HbaseDeploymentContributor extends ServiceDeploymentContributorBase
private void addRewriteFilter(
DeploymentContext context, Service service, ResourceDescriptor resource ) throws URISyntaxException {
- context.contributeFilter( service, resource, "rewrite", null, null );
+ List<FilterParamDescriptor> params = new ArrayList<FilterParamDescriptor>();
+ params.add( resource.createFilterParam().name( "response.headers" ).value( getQualifiedName() + "/hbase/outbound" ) );
+ context.contributeFilter( service, resource, "rewrite", null, params );
}
private void addIdentityAssertionFilter(DeploymentContext context, Service service, ResourceDescriptor resource) {
@@ -116,4 +127,8 @@ public class HbaseDeploymentContributor extends ServiceDeploymentContributorBase
context.contributeFilter( service, resource, "dispatch", null, null );
}
+ private String getQualifiedName() {
+ return getRole() + "/" + getName();
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-service-hdfs/src/main/java/org/apache/hadoop/gateway/hdfs/HdfsDeploymentContributor.java
----------------------------------------------------------------------
diff --git a/gateway-service-hdfs/src/main/java/org/apache/hadoop/gateway/hdfs/HdfsDeploymentContributor.java b/gateway-service-hdfs/src/main/java/org/apache/hadoop/gateway/hdfs/HdfsDeploymentContributor.java
index 49a26b2..50e1280 100644
--- a/gateway-service-hdfs/src/main/java/org/apache/hadoop/gateway/hdfs/HdfsDeploymentContributor.java
+++ b/gateway-service-hdfs/src/main/java/org/apache/hadoop/gateway/hdfs/HdfsDeploymentContributor.java
@@ -19,15 +19,19 @@ package org.apache.hadoop.gateway.hdfs;
import org.apache.hadoop.gateway.deploy.DeploymentContext;
import org.apache.hadoop.gateway.deploy.ServiceDeploymentContributorBase;
+import org.apache.hadoop.gateway.descriptor.FilterParamDescriptor;
import org.apache.hadoop.gateway.descriptor.ResourceDescriptor;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor;
import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor;
import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor;
import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt;
import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteMatchDescriptor;
-import org.apache.hadoop.gateway.topology.Provider;
import org.apache.hadoop.gateway.topology.Service;
import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
public class HdfsDeploymentContributor extends ServiceDeploymentContributorBase {
@@ -59,19 +63,19 @@ public class HdfsDeploymentContributor extends ServiceDeploymentContributorBase
UrlRewriteActionRewriteDescriptorExt rewrite;
UrlRewriteMatchDescriptor match;
- rule = rules.addRule( getRole() + "/" + getName() + "/namenode/root/inbound" )
+ rule = rules.addRule( getQualifiedName() + "/namenode/root/inbound" )
.directions( "inbound" )
.pattern( "*://*:*/**" + NAMENODE_EXTERNAL_PATH + "/?{**}" );
rewrite = rule.addStep( "rewrite" );
rewrite.template( service.getUrl().toExternalForm() + "/?{**}" );
- rule = rules.addRule( getRole() + "/" + getName() + "/namenode/file/inbound" )
+ rule = rules.addRule( getQualifiedName() + "/namenode/file/inbound" )
.directions( "inbound" )
.pattern( "*://*:*/**" + NAMENODE_EXTERNAL_PATH + "/{path=**}?{**}" );
rewrite = rule.addStep( "rewrite" );
rewrite.template( service.getUrl().toExternalForm() + "/{path=**}?{**}" );
- rule = rules.addRule( getRole() + "/" + getName() + "/datanode/inbound" )
+ rule = rules.addRule( getQualifiedName() + "/datanode/inbound" )
.directions( "inbound" )
.pattern( "*://*:*/**" + DATANODE_EXTERNAL_PATH + "/{path=**}?**" );
//TODO: If the input type is wrong it throws a NPE.
@@ -81,14 +85,18 @@ public class HdfsDeploymentContributor extends ServiceDeploymentContributorBase
rewrite = rule.addStep( "rewrite" );
rewrite.template( "http://{host}:{port}/{path=**}?{**}" );
- rule = rules.addRule( getRole() + "/" + getName() + "/datanode/outbound" )
- .directions( "outbound" )
- .pattern( "*://*:*/**?**" );
+ rule = rules.addRule( getQualifiedName() + "/datanode/outbound" )
+ .directions( "outbound" );
+// .pattern( "*://*:*/**?**" );
match = rule.addStep( "match" );
match.pattern( "*://{host}:{port}/{path=**}?{**}" );
rewrite = rule.addStep( "rewrite" );
rewrite.template( CLUSTER_URL_FUNCTION + DATANODE_EXTERNAL_PATH + "/{path=**}?{host}&{port}&{**}" );
rule.addStep( "encode-query" );
+
+ UrlRewriteFilterDescriptor filter = rules.addFilter( getQualifiedName() + "/outbound" );
+ UrlRewriteFilterContentDescriptor content = filter.addContent( "application/x-http-headers" );
+ content.addApply( "Location", getQualifiedName() + "/datanode/outbound" );
}
public void contributeNameNodeResource( DeploymentContext context, Service service ) throws URISyntaxException {
@@ -139,8 +147,14 @@ public class HdfsDeploymentContributor extends ServiceDeploymentContributorBase
private void addRewriteFilter(
DeploymentContext context, Service service, ResourceDescriptor resource ) throws URISyntaxException {
- context.contributeFilter( service, resource, "rewrite", null, null );
+ List<FilterParamDescriptor> params = new ArrayList<FilterParamDescriptor>();
+ params.add( resource.createFilterParam().name( "response.headers" ).value( getQualifiedName() + "/outbound" ) );
+ context.contributeFilter( service, resource, "rewrite", null, params );
+
+ }
+ private String getQualifiedName() {
+ return getRole() + "/" + getName();
}
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayBasicFuncTest.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayBasicFuncTest.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayBasicFuncTest.java
index 852d2ae..66c9555 100644
--- a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayBasicFuncTest.java
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayBasicFuncTest.java
@@ -1299,17 +1299,15 @@ public class GatewayBasicFuncTest {
.respond()
.status( HttpStatus.SC_CREATED )
.content( driver.getResourceBytes( resourceName + ".xml" ) )
- .contentType( ContentType.XML.toString() );
- //TODO: Add "Location" header check when issue with incorrect outbound rewrites will be resolved
- //.header( "Location", driver.getRealUrl( "HBASE" ) + path )
+ .contentType( ContentType.XML.toString() )
+ .header( "Location", driver.getRealUrl( "HBASE" ) + path );
given()
.auth().preemptive().basic( username, password )
.expect()
.statusCode( HttpStatus.SC_CREATED )
.contentType( ContentType.XML )
- //TODO: Add "Location" header check when issue with incorrect outbound rewrites will be resolved
- //.header( "Location", startsWith( driver.getUrl( "HBASE" ) + path ) )
+ .header( "Location", startsWith( driver.getUrl( "HBASE" ) + path ) )
.when().put( driver.getUrl( "HBASE" ) + path );
driver.assertComplete();
@@ -1320,17 +1318,15 @@ public class GatewayBasicFuncTest {
.respond()
.status( HttpStatus.SC_CREATED )
.content( driver.getResourceBytes( resourceName + ".json" ) )
- .contentType( ContentType.JSON.toString() );
- //TODO: Add "Location" header check when issue with incorrect outbound rewrites will be resolved
- //.header( "Location", driver.getRealUrl( "HBASE" ) + path )
+ .contentType( ContentType.JSON.toString() )
+ .header( "Location", driver.getRealUrl( "HBASE" ) + path );
given()
.auth().preemptive().basic( username, password )
.expect()
.statusCode( HttpStatus.SC_CREATED )
.contentType( ContentType.JSON )
- //TODO: Add "Location" header check when issue with incorrect outbound rewrites will be resolved
- //.header( "Location", startsWith( driver.getUrl( "HBASE" ) + path ) )
+ .header( "Location", startsWith( driver.getUrl( "HBASE" ) + path ) )
.when().put( driver.getUrl( "HBASE" ) + path );
driver.assertComplete();
@@ -1341,17 +1337,15 @@ public class GatewayBasicFuncTest {
.respond()
.status( HttpStatus.SC_CREATED )
.content( driver.getResourceBytes( resourceName + ".protobuf" ) )
- .contentType( "application/x-protobuf" );
- //TODO: Add "Location" header check when issue with incorrect outbound rewrites will be resolved
- //.header( "Location", driver.getRealUrl( "HBASE" ) + path );
+ .contentType( "application/x-protobuf" )
+ .header( "Location", driver.getRealUrl( "HBASE" ) + path );
given()
.auth().preemptive().basic( username, password )
.expect()
.statusCode( HttpStatus.SC_CREATED )
.contentType( "application/x-protobuf" )
- //TODO: Add "Location" header check when issue with incorrect outbound rewrites will be resolved
- //.header( "Location", startsWith( driver.getUrl( "HBASE" ) + path ) )
+ .header( "Location", startsWith( driver.getUrl( "HBASE" ) + path ) )
.when().put( driver.getUrl( "HBASE" ) + path );
driver.assertComplete();
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Builder.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Builder.java b/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Builder.java
index cac763d..3e9cf24 100644
--- a/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Builder.java
+++ b/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Builder.java
@@ -224,6 +224,10 @@ public class Builder {
public void setHost( String paramName, String valuePattern ) {
setHasAuthority( true );
+ // Make sure that ** is converted to * since ** doesn't make any sense in this context.
+ if( Segment.GLOB_PATTERN.equals( valuePattern ) ) {
+ valuePattern = Segment.STAR_PATTERN;
+ }
host = new Host( paramName, valuePattern );
}
@@ -233,6 +237,10 @@ public class Builder {
public void setPort( String paramName, String valuePattern ) {
setHasAuthority( true );
+ // Make sure that ** is converted to * since ** doesn't make any sense in this context.
+ if( Segment.GLOB_PATTERN.equals( valuePattern ) ) {
+ valuePattern = Segment.STAR_PATTERN;
+ }
port = new Port( paramName, valuePattern );
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Expander.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Expander.java b/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Expander.java
index 9f9ba6b..56e57e1 100644
--- a/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Expander.java
+++ b/gateway-util-urltemplate/src/main/java/org/apache/hadoop/gateway/util/urltemplate/Expander.java
@@ -146,9 +146,10 @@ public class Expander {
AtomicInteger index = new AtomicInteger( 0 );
expandExplicitQuery( template, names, params, builder, index );
expandExtraQuery( template, names, params, builder, index );
- if( template.hasQuery() && index.get() == 0 ) {
- builder.append( '?' );
- }
+ //Kevin: I took this out because it causes '?' to be added to expanded templates when there are not query params.
+// if( template.hasQuery() && index.get() == 0 ) {
+// builder.append( '?' );
+// }
}
private static void expandExplicitQuery( Template template, Set<String> names, Params params, StringBuilder builder, AtomicInteger index ) {
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ExpanderTest.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ExpanderTest.java b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ExpanderTest.java
index 68ba5e7..f70dfe1 100644
--- a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ExpanderTest.java
+++ b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ExpanderTest.java
@@ -135,7 +135,7 @@ public class ExpanderTest {
template = Parser.parse( "?" );
params = new MockParams();
expanded = Expander.expand( template, params );
- assertThat( expanded.toString(), equalTo( "?" ) ) ;
+ assertThat( expanded.toString(), equalTo( "" ) ) ;
template = Parser.parse( "?query-name={queryParam-name}" );
params = new MockParams();
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
index ffcb48a..7817271 100644
--- a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
+++ b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/MatcherTest.java
@@ -613,6 +613,33 @@ public class MatcherTest {
assertThat( params.resolve( "mid" ).size(), equalTo( 2 ) );
assertThat( params.resolve( "mid" ), hasItem( "A" ) );
assertThat( params.resolve( "mid" ), hasItem( "B" ) );
+
+ template = Parser.parse( "*://*:*/{path=**}?{**}" );
+ input = Parser.parse( "http://host:port/pathA/pathB" );
+ matcher = new Matcher<Void>( template, null );
+ match = matcher.match( input );
+ params = match.getParams();
+ assertThat( params.resolve( "path" ), hasItem( "pathA" ) );
+ assertThat( params.resolve( "path" ), hasItem( "pathB" ) );
+ assertThat( params.resolve( "path" ).size(), is( 2 ) );
+
+ template = Parser.parse( "*://*:*/{path=**}?{**}" );
+ input = Parser.parse( "http://host:port/pathA/pathB" );
+ matcher = new Matcher<Void>( template, null );
+ match = matcher.match( input );
+ params = match.getParams();
+ assertThat( params.resolve( "path" ), hasItem( "pathA" ) );
+ assertThat( params.resolve( "path" ), hasItem( "pathB" ) );
+ assertThat( params.resolve( "path" ).size(), is( 2 ) );
+
+ template = Parser.parse( "*://*:*/{path=**}?{**}" );
+ input = Parser.parse( "http://host:port/pathA/pathB" );
+ matcher = new Matcher<Void>( template, null );
+ match = matcher.match( input );
+ params = match.getParams();
+ assertThat( params.resolve( "path" ), hasItem( "pathA" ) );
+ assertThat( params.resolve( "path" ), hasItem( "pathB" ) );
+ assertThat( params.resolve( "path" ).size(), is( 2 ) );
}
@Test
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ParserTest.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ParserTest.java b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ParserTest.java
index 2c67822..82fb7d0 100644
--- a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ParserTest.java
+++ b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/ParserTest.java
@@ -997,4 +997,25 @@ public class ParserTest {
assertThat( template.toString(), is( "{test-scheme=*}:///{test-path=**}" ) );
}
+ @Test
+ public void testAuthorityWildcards() throws Exception {
+ Template template;
+
+ template = Parser.parse( "*://*:*/" );
+ assertThat( template.getHost().getFirstValue().getPattern(), is( "*" ) );
+ assertThat( template.getPort().getFirstValue().getPattern(), is( "*" ) );
+
+ template = Parser.parse( "*://**/" );
+ assertThat( template.getHost().getFirstValue().getPattern(), is( "*" ) );
+ assertThat( template.getPort(), nullValue() );
+
+ template = Parser.parse( "*://*/" );
+ assertThat( template.getHost().getFirstValue().getPattern(), is( "*" ) );
+ assertThat( template.getPort(), nullValue() );
+
+ template = Parser.parse( "*://**:**/" );
+ assertThat( template.getHost().getFirstValue().getPattern(), is( "*" ) );
+ assertThat( template.getPort().getFirstValue().getPattern(), is( "*" ) );
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/436d656c/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/RewriterTest.java
----------------------------------------------------------------------
diff --git a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/RewriterTest.java b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/RewriterTest.java
index c8f59a6..87abea9 100644
--- a/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/RewriterTest.java
+++ b/gateway-util-urltemplate/src/test/java/org/apache/hadoop/gateway/util/urltemplate/RewriterTest.java
@@ -35,6 +35,7 @@ import java.util.List;
import java.util.Set;
import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@Category( { UnitTests.class, FastTests.class } )
@@ -288,6 +289,22 @@ public class RewriterTest {
actualOutput = Rewriter.rewrite( actualInput, sourcePattern, targetPattern, new TestResolver( config, request ) );
assertThat( actualOutput, equalTo( expectOutput ) );
}
+
+ @Test
+ public void testRewriteExcludesQueryDelimWhenInputHasNoQueryParams() throws Exception {
+ Template inputTemplate, outputTemplate;
+ URI actualInput, actualOutput, expectOutput;
+
+ inputTemplate = Parser.parse( "{scheme}://{host}:*/{path=**}?{**}" );
+ outputTemplate = Parser.parse( "{scheme}://{host}:777/test-output/{path=**}?{**}" );
+
+ actualInput = new URI( "http://host:42/pathA/pathB" );
+ expectOutput = new URI( "http://host:777/test-output/pathA/pathB" );
+
+ actualOutput = Rewriter.rewrite( actualInput, inputTemplate, outputTemplate, null );
+
+ assertThat( actualOutput, is( expectOutput ) );
+ }
private class TestResolver implements Params {