Search in sources :

Example 6 with UrlRewriteFilterPathDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor in project knox by apache.

the class UrlRewriteUtil method filterJavaScript.

public static String filterJavaScript(String inputValue, UrlRewriteFilterContentDescriptor config, UrlRewriteFilterReader filterReader, UrlRewriteFilterPathDescriptor.Compiler<Pattern> regexCompiler) {
    StringBuffer tbuff = new StringBuffer();
    StringBuffer sbuff = new StringBuffer();
    sbuff.append(inputValue);
    if (config != null && !config.getSelectors().isEmpty()) {
        for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
            if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                Matcher matcher = apply.compiledPath(regexCompiler).matcher(sbuff);
                int index = 0;
                while (matcher.find()) {
                    int start = matcher.start();
                    int end = matcher.end();
                    if (start != -1 && end != -1) {
                        tbuff.append(sbuff, index, start);
                        String value = matcher.group();
                        value = filterReader.filterValueString(null, value, apply.rule());
                        tbuff.append(value);
                        index = end;
                    }
                }
                tbuff.append(sbuff, index, sbuff.length());
                sbuff.setLength(0);
                sbuff.append(tbuff);
                tbuff.setLength(0);
            }
        }
    }
    return sbuff.toString();
}
Also used : Matcher(java.util.regex.Matcher) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 7 with UrlRewriteFilterPathDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor in project knox by apache.

the class XmlUrlRewriteRulesExporter method createFilterContent.

private Element createFilterContent(Document document, UrlRewriteFilterContentDescriptor parent) throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    Element parentElement = createElement(document, CONTENT, parent);
    for (UrlRewriteFilterPathDescriptor child : parent.getSelectors()) {
        Element childElement = createFilterSelector(document, child);
        parentElement.appendChild(childElement);
    }
    return parentElement;
}
Also used : Element(org.w3c.dom.Element) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 8 with UrlRewriteFilterPathDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor in project knox by apache.

the class XmlUrlRewriteRulesExporter method createFilterSelector.

private Element createFilterSelector(Document document, UrlRewriteFilterPathDescriptor parent) throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    Element parentElement = createElement(document, toTagName(parent), parent);
    if (parent instanceof UrlRewriteFilterGroupDescriptor) {
        for (UrlRewriteFilterPathDescriptor child : ((UrlRewriteFilterGroupDescriptor) parent).getSelectors()) {
            Element childElement = createFilterSelector(document, child);
            parentElement.appendChild(childElement);
        }
    }
    return parentElement;
}
Also used : Element(org.w3c.dom.Element) UrlRewriteFilterGroupDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 9 with UrlRewriteFilterPathDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor in project knox by apache.

the class HtmlFilterReaderBase method getRuleName.

private String getRuleName(String inputValue) {
    if (config != null && !config.getSelectors().isEmpty()) {
        for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
            if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                Matcher matcher = apply.compiledPath(REGEX_COMPILER).matcher(inputValue);
                if (matcher.matches()) {
                    return apply.rule();
                }
            }
        }
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 10 with UrlRewriteFilterPathDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor in project knox by apache.

the class JsonFilterReader method filterStreamValue.

protected String filterStreamValue(Level node) {
    String value;
    if (node.isArray()) {
        value = node.node.get(0).asText();
    } else {
        value = node.node.get(node.field).asText();
    }
    String rule = null;
    UrlRewriteFilterGroupDescriptor scope = node.scopeConfig;
    // TODO: Scan the top level apply rules for the first match.
    if (scope != null) {
        for (UrlRewriteFilterPathDescriptor selector : scope.getSelectors()) {
            JsonPath.Expression path = (JsonPath.Expression) selector.compiledPath(JPATH_COMPILER);
            List<JsonPath.Match> matches = path.evaluate(node.scopeNode);
            if (matches != null && !matches.isEmpty()) {
                JsonPath.Match match = matches.get(0);
                if (match.getNode().isTextual()) {
                    if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                        UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                        rule = apply.rule();
                        break;
                    }
                }
            }
        }
    }
    try {
        value = filterValueString(node.field, value, rule);
        if (node.isArray()) {
            ((ArrayNode) node.node).set(0, new TextNode(value));
        } else {
            ((ObjectNode) node.node).put(node.field, value);
        }
    } catch (Exception e) {
        LOG.failedToFilterValue(value, rule, e);
    }
    return value;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) JsonPath(org.apache.knox.gateway.util.JsonPath) IOException(java.io.IOException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) UrlRewriteFilterGroupDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Aggregations

UrlRewriteFilterPathDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)13 UrlRewriteFilterApplyDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor)8 JsonPath (org.apache.knox.gateway.util.JsonPath)4 Element (org.w3c.dom.Element)4 UrlRewriteFilterGroupDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor)3 Matcher (java.util.regex.Matcher)2 EndElement (javax.xml.stream.events.EndElement)2 StartElement (javax.xml.stream.events.StartElement)2 XPathExpression (javax.xml.xpath.XPathExpression)2 UrlRewriteFilterBufferDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor)2 UrlRewriteFilterDetectDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor)2 Attr (org.w3c.dom.Attr)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 IOException (java.io.IOException)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 QName (javax.xml.namespace.QName)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1