use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class InboundUrlFunctionProcessorTest method testServiceResolve.
@Test
public void testServiceResolve() throws Exception {
ServiceLoader loader = ServiceLoader.load(UrlRewriteFunctionProcessor.class);
Iterator iterator = loader.iterator();
assertThat("Service iterator empty.", iterator.hasNext());
InboundUrlFunctionProcessor proc = null;
while (iterator.hasNext()) {
Object object = iterator.next();
if (object instanceof InboundUrlFunctionProcessor) {
proc = (InboundUrlFunctionProcessor) object;
}
}
if (proc == null)
fail("Failed to find " + InboundUrlFunctionProcessor.class.getName() + " via service loader.");
Map<String, UrlRewriteFunctionProcessor> functions = new HashMap<>();
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
UrlRewriter.Direction direction = UrlRewriter.Direction.OUT;
List<String> parameters = Collections.singletonList("host");
Template template = Parser.parseLiteral("https://localhost:8443/gateway/default/datanode/?host=http://foo:50075");
UrlRewriteContextImpl ctx = new UrlRewriteContextImpl(environment, this.getRewriteResponse(), functions, direction, template);
List<String> result = proc.resolve(ctx, parameters);
assertThat(result.get(0), is("http://foo:50075"));
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class InboundUrlFunctionProcessorTest method testQueryParam.
@Test
public void testQueryParam() throws Exception {
GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
EasyMock.expect(environment.resolve("cluster.name")).andReturn(Collections.singletonList("test-cluster-name")).anyTimes();
Resolver resolver = EasyMock.createNiceMock(Resolver.class);
EasyMock.expect(resolver.resolve("query.param.host")).andReturn(Lists.newArrayList("http://foo:50075")).anyTimes();
EasyMock.replay(gatewayServices, environment, resolver);
UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteRuleDescriptor rule = descriptor.addRule("test-location");
rule.pattern("{*}://{*}:{*}/{**}/?{**}");
UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
rewrite.template("{$inboundurl[host]}");
UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
rewriter.initialize(environment, descriptor);
Template input = Parser.parseLiteral("https://localhost:8443/gateway/default/datanode/?host=http://foo:50075");
Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-location");
assertThat(output.toString(), is("http://foo:50075"));
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class SecureQueryDecodeProcessor method process.
@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
// TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
Template currUrl = context.getCurrentUrl();
Builder newUrl = new Builder(currUrl);
Map<String, Query> map = newUrl.getQuery();
Query query = map.remove(ENCODED_PARAMETER_NAME);
if (query != null) {
String value = query.getFirstValue().getPattern();
value = decode(value);
StringTokenizer outerParser = new StringTokenizer(value, "&");
while (outerParser.hasMoreTokens()) {
String pair = outerParser.nextToken();
StringTokenizer innerParser = new StringTokenizer(pair, "=");
if (innerParser.hasMoreTokens()) {
String paramName = innerParser.nextToken();
if (innerParser.hasMoreTokens()) {
String paramValue = innerParser.nextToken();
// Need to take out any existing query param.
// If we don't then someone could override something in the encoded param.
map.remove(paramName);
newUrl.addQuery(paramName, "", paramValue, true);
} else {
newUrl.addQuery(paramName, "", null, true);
}
}
}
context.setCurrentUrl(newUrl.build());
}
return UrlRewriteStepStatus.SUCCESS;
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class SecureQueryDecryptProcessor method process.
@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
// TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
Template currUrl = context.getCurrentUrl();
Builder newUrl = new Builder(currUrl);
Map<String, Query> map = newUrl.getQuery();
Query query = map.remove(ENCRYPTED_PARAMETER_NAME);
UrlRewriteStepStatus status = UrlRewriteStepStatus.FAILURE;
status = getUrlRewriteStepStatus(context, newUrl, map, query, status);
return status;
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class DecryptUriProcessor method process.
@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
if (param != null && !param.isEmpty()) {
Template template = Parser.parseTemplate("{" + param + "}");
String resolvedTemplate = Expander.expandToString(template, context.getParameters(), context.getEvaluator());
String url = decode(resolvedTemplate);
EncryptStepContextParams params = new EncryptStepContextParams();
params.addParam(param, Arrays.asList(url));
context.addParameters(params);
return UrlRewriteStepStatus.SUCCESS;
}
return UrlRewriteStepStatus.FAILURE;
}
Aggregations