Search in sources :

Example 26 with Template

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"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteFunctionProcessor(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor) Template(org.apache.knox.gateway.util.urltemplate.Template) UrlRewriter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriter) UrlRewriteContextImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl) Test(org.junit.Test)

Example 27 with Template

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"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 28 with Template

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;
}
Also used : StringTokenizer(java.util.StringTokenizer) Query(org.apache.knox.gateway.util.urltemplate.Query) Builder(org.apache.knox.gateway.util.urltemplate.Builder) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 29 with Template

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;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus) Query(org.apache.knox.gateway.util.urltemplate.Query) Builder(org.apache.knox.gateway.util.urltemplate.Builder) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 30 with Template

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;
}
Also used : EncryptStepContextParams(org.apache.knox.gateway.encrypturi.EncryptStepContextParams) Template(org.apache.knox.gateway.util.urltemplate.Template)

Aggregations

Template (org.apache.knox.gateway.util.urltemplate.Template)50 Test (org.junit.Test)23 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)15 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)10 URISyntaxException (java.net.URISyntaxException)9 URL (java.net.URL)8 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)8 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)8 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)8 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)6 GatewayServices (org.apache.knox.gateway.services.GatewayServices)5 Capture (org.easymock.Capture)5 Host (org.apache.knox.gateway.util.urltemplate.Host)4 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)4 Query (org.apache.knox.gateway.util.urltemplate.Query)4 URI (java.net.URI)3 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)3