Search in sources :

Example 1 with UrlRewriteContextImpl

use of org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl in project knox by apache.

the class UrlRewriteProcessor method rewrite.

@Override
public Template rewrite(Resolver resolver, Template inputUri, Direction direction, String ruleName) {
    Template outputUri = inputUri;
    String serviceRole = null;
    if (resolver != null) {
        List<String> serviceRoles = resolver.resolve("service.role");
        if (serviceRoles != null && !serviceRoles.isEmpty()) {
            serviceRole = serviceRoles.get(0);
        }
    }
    UrlRewriteStepProcessorHolder stepHolder = null;
    String effectiveRuleName = null;
    if (ruleName == null || "*".equals(ruleName)) {
        // Used for logging later.
        ruleName = null;
        Matcher<UrlRewriteRuleProcessorHolder>.Match match = null;
        switch(direction) {
            case IN:
                match = inbound.match(outputUri, serviceRole);
                break;
            case OUT:
                match = outbound.match(outputUri, serviceRole);
                break;
        }
        if (match != null) {
            stepHolder = match.getValue();
            effectiveRuleName = match.getValue().getRuleName();
        }
    } else if (!ruleName.isEmpty()) {
        stepHolder = rules.get(ruleName);
        effectiveRuleName = ruleName;
    }
    if (stepHolder != null) {
        UrlRewriteContext context = new UrlRewriteContextImpl(environment, resolver, functions, direction, inputUri);
        try {
            UrlRewriteStepStatus stepStatus = stepHolder.process(context);
            if (UrlRewriteStepStatus.SUCCESS == stepStatus) {
                outputUri = context.getCurrentUrl();
                if (ruleName == null) {
                    LOG.rewroteUrlViaImplicitRule(inputUri, direction, effectiveRuleName, outputUri);
                } else {
                    LOG.rewroteUrlViaExplicitRule(inputUri, direction, effectiveRuleName, outputUri);
                }
            } else {
                LOG.failedToRewriteUrl(inputUri, direction, effectiveRuleName, stepStatus);
                outputUri = null;
            }
        } catch (Exception e) {
            LOG.failedToRewriteUrlDueToException(inputUri, direction, effectiveRuleName, e);
            outputUri = null;
        }
    } else {
        LOG.noRuleMatchingUrl(inputUri, direction);
    }
    return outputUri;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus) Matcher(org.apache.knox.gateway.util.urltemplate.Matcher) ScopedMatcher(org.apache.knox.gateway.filter.rewrite.ext.ScopedMatcher) UrlRewriteStepProcessorHolder(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteStepProcessorHolder) UrlRewriteContextImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 2 with UrlRewriteContextImpl

use of org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl 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)

Aggregations

UrlRewriteContextImpl (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl)2 Template (org.apache.knox.gateway.util.urltemplate.Template)2 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)1 UrlRewriter (org.apache.knox.gateway.filter.rewrite.api.UrlRewriter)1 ScopedMatcher (org.apache.knox.gateway.filter.rewrite.ext.ScopedMatcher)1 UrlRewriteStepProcessorHolder (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteStepProcessorHolder)1 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)1 UrlRewriteFunctionProcessor (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor)1 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)1 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)1 Test (org.junit.Test)1