Search in sources :

Example 1 with UrlRewriteFilterPathDescriptor

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

the class JsonFilterReader method startBuffering.

protected boolean startBuffering(Level node) {
    boolean buffered = false;
    UrlRewriteFilterGroupDescriptor scope = node.scopeConfig;
    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()) {
                if (selector instanceof UrlRewriteFilterBufferDescriptor) {
                    bufferingLevel = node;
                    bufferingConfig = (UrlRewriteFilterBufferDescriptor) selector;
                    buffered = true;
                }
                break;
            }
        }
    }
    return buffered;
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor) JsonPath(org.apache.knox.gateway.util.JsonPath) UrlRewriteFilterGroupDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 2 with UrlRewriteFilterPathDescriptor

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

the class JsonFilterReader method filterBufferedValues.

private void filterBufferedValues(Level node, List<UrlRewriteFilterPathDescriptor> selectors) {
    for (UrlRewriteFilterPathDescriptor selector : selectors) {
        JsonPath.Expression path = (JsonPath.Expression) selector.compiledPath(JPATH_COMPILER);
        List<JsonPath.Match> matches = path.evaluate(node.node);
        for (JsonPath.Match match : matches) {
            if (match.getNode().isTextual()) {
                if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                    filterBufferedValue(match, (UrlRewriteFilterApplyDescriptor) selector);
                }
            }
        }
    }
}
Also used : JsonPath(org.apache.knox.gateway.util.JsonPath) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 3 with UrlRewriteFilterPathDescriptor

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

the class XmlFilterReader method streamAttribute.

private void streamAttribute(Element element, Attribute attribute) throws XPathExpressionException {
    Attr node;
    QName name = attribute.getName();
    String prefix = name.getPrefix();
    String uri = name.getNamespaceURI();
    if (uri == null || uri.isEmpty()) {
        node = document.createAttribute(name.getLocalPart());
        element.setAttributeNode(node);
    } else {
        node = document.createAttributeNS(uri, name.getLocalPart());
        if (prefix != null && !prefix.isEmpty()) {
            node.setPrefix(prefix);
        }
        element.setAttributeNodeNS(node);
    }
    String value = attribute.getValue();
    Level level = stack.peek();
    if ((level.scopeConfig) == null || (level.scopeConfig.getSelectors().isEmpty())) {
        value = filterAttribute(null, attribute.getName(), value, null);
        node.setValue(value);
    } else {
        UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath(level);
        if (path instanceof UrlRewriteFilterApplyDescriptor) {
            String rule = ((UrlRewriteFilterApplyDescriptor) path).rule();
            value = filterAttribute(null, attribute.getName(), value, rule);
            node.setValue(value);
        }
    }
    if (prefix == null || prefix.isEmpty()) {
        writer.write(" ");
        writer.write(name.getLocalPart());
    } else {
        writer.write(" ");
        writer.write(prefix);
        writer.write(":");
        writer.write(name.getLocalPart());
    }
    writer.write("=\"");
    writer.write(value);
    writer.write("\"");
    element.removeAttributeNode(node);
}
Also used : QName(javax.xml.namespace.QName) Attr(org.w3c.dom.Attr) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 4 with UrlRewriteFilterPathDescriptor

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

the class XmlFilterReader method pickFirstMatchingPath.

protected UrlRewriteFilterPathDescriptor pickFirstMatchingPath(Level level) {
    UrlRewriteFilterPathDescriptor match = null;
    if (level.scopeConfig != null) {
        for (UrlRewriteFilterPathDescriptor selector : level.scopeConfig.getSelectors()) {
            try {
                XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
                Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
                if (node != null) {
                    match = selector;
                    break;
                }
            } catch (XPathExpressionException e) {
                throw new IllegalArgumentException(selector.path(), e);
            }
        }
    }
    return match;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 5 with UrlRewriteFilterPathDescriptor

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

the class XmlFilterReader method processStartElement.

private void processStartElement(StartElement event) throws XPathExpressionException {
    // System.out.println( "SE=" + event );
    // Create a new "empty" element and add it to the document.
    Element element = bufferElement(event);
    Level parent = stack.peek();
    parent.node.appendChild(element);
    // Note: Don't currently support nested buffer or scope descriptors.
    if (currentlyBuffering()) {
        pushLevel(parent, event, element, parent.scopeNode, parent.scopeConfig);
        bufferAttributes(event, element);
    // Else not currently buffering
    } else {
        // See if there is a matching path descriptor in the current scope.
        UrlRewriteFilterPathDescriptor descriptor = pickFirstMatchingPath(parent);
        if (descriptor != null) {
            // If this is a buffer descriptor then switch to buffering and buffer the attributes.
            if (descriptor instanceof UrlRewriteFilterBufferDescriptor) {
                pushLevel(parent, event, element, element, (UrlRewriteFilterBufferDescriptor) descriptor);
                bufferAttributes(event, element);
            // Otherwise if this is a scope descriptor then change the scope and stream the attributes.
            } else if (descriptor instanceof UrlRewriteFilterScopeDescriptor) {
                pushLevel(parent, event, element, element, (UrlRewriteFilterScopeDescriptor) descriptor);
                streamElement(event, element);
            // Else found an unexpected matching path.
            } else {
                // This is likely because there is an <apply> targeted at the text of an element.
                // That "convenience" config will be taken care of in the streamElement() processing.
                pushLevel(parent, event, element, parent.scopeNode, parent.scopeConfig);
                streamElement(event, element);
            }
        // If there is no matching path descriptor then continue streaming.
        } else {
            pushLevel(parent, event, element, parent.scopeNode, parent.scopeConfig);
            streamElement(event, element);
        }
    }
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor) EndElement(javax.xml.stream.events.EndElement) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) UrlRewriteFilterScopeDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterScopeDescriptor) 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