Search in sources :

Example 1 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class EncryptUriProcessor method process.

@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
    if (param != null && !param.isEmpty() && template != null && !template.isEmpty()) {
        Template uri = Parser.parseTemplate(template);
        String resolvedTemplate = Expander.expandToString(uri, context.getParameters(), context.getEvaluator());
        if (resolvedTemplate != null && !resolvedTemplate.isEmpty()) {
            String endcoedUrl = encode(resolvedTemplate);
            EncryptStepContextParams params = new EncryptStepContextParams();
            params.addParam(param, Arrays.asList(endcoedUrl));
            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)

Example 2 with Template

use of org.apache.knox.gateway.util.urltemplate.Template 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 3 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class SecureQueryEncodeProcessor 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 url = context.getCurrentUrl();
    String str = url.toString();
    String path = str;
    String query = null;
    int index = str.indexOf('?');
    if (index >= 0) {
        path = str.substring(0, index);
        if (index < str.length()) {
            query = str.substring(index + 1);
        }
    }
    if (query != null) {
        query = Base64.encodeBase64String(query.getBytes("UTF-8"));
        query = removeTrailingEquals(query);
        url = Parser.parseLiteral(path + "?" + ENCODED_PARAMETER_NAME + "=" + query);
        context.setCurrentUrl(url);
    }
    return UrlRewriteStepStatus.SUCCESS;
}
Also used : Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 4 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class SecureQueryEncryptProcessor 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 url = context.getCurrentUrl();
    String str = url.toString();
    String path = str;
    String query = null;
    int index = str.indexOf('?');
    if (index >= 0) {
        path = str.substring(0, index);
        if (index < str.length()) {
            query = str.substring(index + 1);
        }
    }
    if (query != null) {
        query = encode(query);
        url = Parser.parseLiteral(path + "?" + ENCRYPTED_PARAMETER_NAME + "=" + query);
        context.setCurrentUrl(url);
    }
    return UrlRewriteStepStatus.SUCCESS;
}
Also used : Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 5 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class ServiceMappedHostFunctionProcessor method resolve.

@Override
public List<String> resolve(UrlRewriteContext context, List<String> parameters) throws Exception {
    List<String> results = null;
    if (parameters != null) {
        results = new ArrayList<String>(parameters.size());
        for (String parameter : parameters) {
            String url = lookupServiceUrl(parameter);
            if (url != null) {
                Template template = Parser.parseLiteral(url);
                Host host = template.getHost();
                if (host != null) {
                    String hostStr = host.getFirstValue().getPattern();
                    if (hostmap != null) {
                        switch(context.getDirection()) {
                            case IN:
                                parameter = hostmap.resolveInboundHostName(hostStr);
                                break;
                            case OUT:
                                parameter = hostmap.resolveOutboundHostName(hostStr);
                                break;
                        }
                    } else {
                        parameter = hostStr;
                    }
                }
            }
            results.add(parameter);
        }
    }
    return results;
}
Also used : Host(org.apache.knox.gateway.util.urltemplate.Host) 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