Search in sources :

Example 1 with UrlRewriteStepStatus

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus 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 UrlRewriteStepStatus

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus in project knox by apache.

the class UrlRewriteStepProcessorHolder method processActions.

private UrlRewriteStepStatus processActions(UrlRewriteContext context, UrlRewriteStepProcessorState state) throws Exception {
    UrlRewriteStepStatus flowStatus = UrlRewriteStepStatus.SUCCESS;
    while (state.hasNextAction()) {
        if (flowStatus == UrlRewriteStepStatus.SUCCESS) {
            UrlRewriteStepStatus stepStatus = UrlRewriteStepStatus.SUCCESS;
            UrlRewriteStepProcessorHolder step = state.nextAction(stepStatus);
            stepStatus = step.process(context);
            switch(stepStatus) {
                case FAILURE:
                    flowStatus = UrlRewriteStepStatus.FAILURE;
                    continue;
                case FINISHED:
                    return stepStatus;
            }
        }
    }
    return flowStatus;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)

Example 3 with UrlRewriteStepStatus

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus in project knox by apache.

the class UrlRewriteStepProcessorHolder method processAndFlow.

// All conditions proceeding a set of one or more actions must succeed for the actions to be executed.
private UrlRewriteStepStatus processAndFlow(UrlRewriteContext context) throws Exception {
    UrlRewriteStepProcessorState state = new UrlRewriteStepProcessorState(childProcessors.iterator());
    UrlRewriteStepStatus stepStatus = UrlRewriteStepStatus.SUCCESS;
    UrlRewriteStepProcessorHolder step;
    while (state.hasNext()) {
        while (state.hasNextCondition()) {
            step = state.nextCondition(stepStatus);
            stepStatus = step.process(context);
            if (!(stepStatus == UrlRewriteStepStatus.SUCCESS)) {
                return stepStatus;
            }
        }
        stepStatus = processActions(context, state);
        if (!(stepStatus == UrlRewriteStepStatus.SUCCESS)) {
            return stepStatus;
        }
    }
    return UrlRewriteStepStatus.SUCCESS;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)

Example 4 with UrlRewriteStepStatus

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus in project knox by apache.

the class UrlRewriteStepProcessorHolder method process.

@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
    UrlRewriteStepStatus status = UrlRewriteStepStatus.SUCCESS;
    // If initialization failed then fail processing
    if (processor != null) {
        status = processor.process(context);
        if (UrlRewriteStepStatus.SUCCESS == status && descriptor instanceof UrlRewriteFlowDescriptor && !childProcessors.isEmpty()) {
            UrlRewriteFlowDescriptor flowDescriptor = (UrlRewriteFlowDescriptor) descriptor;
            UrlRewriteStepFlow flow = flowDescriptor.flow();
            if (flow == null) {
                flow = UrlRewriteStepFlow.AND;
            }
            switch(flow) {
                case ALL:
                    return processAllFlow(context);
                case AND:
                    return processAndFlow(context);
                case OR:
                    return processOrFlow(context);
            }
        }
    }
    return status;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus) UrlRewriteFlowDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFlowDescriptor) UrlRewriteStepFlow(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteStepFlow)

Example 5 with UrlRewriteStepStatus

use of org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus in project knox by apache.

the class UrlRewriteStepProcessorHolder method processAllFlow.

private UrlRewriteStepStatus processAllFlow(UrlRewriteContext context) throws Exception {
    UrlRewriteStepProcessorState state = new UrlRewriteStepProcessorState(childProcessors.iterator());
    UrlRewriteStepStatus stepStatus = UrlRewriteStepStatus.SUCCESS;
    UrlRewriteStepProcessorHolder step;
    while (state.hasNext()) {
        while (state.hasNextCondition()) {
            step = state.nextCondition(stepStatus);
            stepStatus = step.process(context);
            if (stepStatus == UrlRewriteStepStatus.FINISHED) {
                return stepStatus;
            }
        }
        stepStatus = processActions(context, state);
        if (stepStatus == UrlRewriteStepStatus.FINISHED) {
            return stepStatus;
        }
    }
    return UrlRewriteStepStatus.SUCCESS;
}
Also used : UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)

Aggregations

UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)10 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)3 Template (org.apache.knox.gateway.util.urltemplate.Template)3 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)2 GatewayServices (org.apache.knox.gateway.services.GatewayServices)2 AliasService (org.apache.knox.gateway.services.security.AliasService)2 CryptoService (org.apache.knox.gateway.services.security.CryptoService)2 DefaultCryptoService (org.apache.knox.gateway.services.security.impl.DefaultCryptoService)2 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)2 Query (org.apache.knox.gateway.util.urltemplate.Query)2 Capture (org.easymock.Capture)2 Test (org.junit.Test)2 EncryptStepContextParams (org.apache.knox.gateway.encrypturi.EncryptStepContextParams)1 DecryptUriDescriptor (org.apache.knox.gateway.encrypturi.api.DecryptUriDescriptor)1 EncryptUriDescriptor (org.apache.knox.gateway.encrypturi.api.EncryptUriDescriptor)1 UrlRewriteFlowDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFlowDescriptor)1 UrlRewriteStepFlow (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteStepFlow)1 ScopedMatcher (org.apache.knox.gateway.filter.rewrite.ext.ScopedMatcher)1 UrlRewriteContextImpl (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl)1 UrlRewriteStepProcessorHolder (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteStepProcessorHolder)1