use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteRequest method getSourceUrl.
private Template getSourceUrl() {
Template urlTemplate;
// KNOX-439[
// StringBuffer urlString = super.getRequestURL();
StringBuffer urlString = new StringBuffer(128);
urlString.append(getScheme());
urlString.append("://");
urlString.append(getServerName());
urlString.append(":");
urlString.append(getServerPort());
urlString.append(super.getRequestURI());
// ]
String queryString = super.getQueryString();
if (queryString != null) {
urlString.append('?');
urlString.append(queryString);
}
try {
urlTemplate = Parser.parseLiteral(urlString.toString());
} catch (URISyntaxException e) {
LOG.failedToParseValueForUrlRewrite(urlString.toString());
// Shouldn't be possible given that the URL is constructed from parts of an existing URL.
urlTemplate = null;
}
return urlTemplate;
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteResponse method rewriteValue.
private String rewriteValue(String value, String rule) {
try {
Template input = Parser.parseLiteral(value);
Template output = rewriter.rewrite(this, input, UrlRewriter.Direction.OUT, rule);
if (output != null) {
value = output.toString();
}
} catch (URISyntaxException e) {
LOG.failedToParseValueForUrlRewrite(value);
}
return value;
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteProcessorTest method testRewriteViaRuleNameWithAmbiguousRules.
@Test
public void testRewriteViaRuleNameWithAmbiguousRules() throws IOException, URISyntaxException {
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-with-same-rules.xml", "UTF-8"));
processor.initialize(environment, config);
Template inputUrl = Parser.parseLiteral("input-mock-scheme-1://input-mock-host-1:42/test-input-path");
Template outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-2");
assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("output-mock-scheme-2://output-mock-host-2:42/test-input-path"));
outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-1");
assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("output-mock-scheme-1://output-mock-host-1:42/test-input-path"));
processor.destroy();
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteProcessorTest method testSolrRewriteDefaultPort.
@Test
public void testSolrRewriteDefaultPort() throws Exception {
URI inputUri, outputUri;
Matcher<Void> matcher;
Matcher<Void>.Match match;
Template input, pattern, template;
inputUri = new URI("https://hortonworks.sandbox.hdp.24.test/gateway/sandbox/solr/TestCollection/select?q=*.*&wt=json&indent=true");
input = Parser.parseLiteral(inputUri.toString());
pattern = Parser.parseTemplate("*://*:*/**/solr/{collection=**}/{query=**}?{**}");
template = Parser.parseTemplate("http://sandbox.hortonworks.com/solr/{collection=**}/{query=**}?{**}");
matcher = new Matcher<Void>();
matcher.add(pattern, null);
match = matcher.match(input);
outputUri = Expander.expand(template, match.getParams(), null);
final String reWrittenScheme = outputUri.getScheme();
assertEquals("http", reWrittenScheme);
final String reWrittenHost = outputUri.getHost();
assertEquals("sandbox.hortonworks.com", reWrittenHost);
final String reWrittenPath = outputUri.getPath();
assertEquals("/solr/TestCollection/select", reWrittenPath);
// Whole thing is (non-deterministicly ordered around the &s):
// "q=*.*&wt=json&indent=true"
final String reWrittenQuery = outputUri.getQuery();
// Check individual parameters are present, and have the right value.
final Map<String, String> reWrittenParams = mapUrlParameters(reWrittenQuery);
assertTrue(reWrittenParams.containsKey("q"));
assertEquals("*.*", reWrittenParams.get("q"));
assertTrue(reWrittenParams.containsKey("wt"));
assertEquals("json", reWrittenParams.get("wt"));
assertEquals("true", reWrittenParams.get("indent"));
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteProcessorTest method testBasicPathRewrite.
@Test
public void testBasicPathRewrite() throws IOException, URISyntaxException {
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.parseLiteral("test-scheme://test-host:1/test-input-path");
Template outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.IN, null);
assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("test-scheme://test-host:1/test-output-path"));
processor.destroy();
}
Aggregations