Search in sources :

Example 11 with UrlRewriteFilterPathDescriptor

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

the class JsonFilterReader method filterBufferedNode.

private void filterBufferedNode(Level node) {
    for (UrlRewriteFilterPathDescriptor selector : bufferingConfig.getSelectors()) {
        JsonPath.Expression path = (JsonPath.Expression) selector.compiledPath(JPATH_COMPILER);
        List<JsonPath.Match> matches = path.evaluate(node.node);
        for (JsonPath.Match match : matches) {
            if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                if (match.getNode().isTextual()) {
                    filterBufferedValue(match, (UrlRewriteFilterApplyDescriptor) selector);
                }
            } else if (selector instanceof UrlRewriteFilterDetectDescriptor) {
                UrlRewriteFilterDetectDescriptor detectConfig = (UrlRewriteFilterDetectDescriptor) selector;
                JsonPath.Expression detectPath = (JsonPath.Expression) detectConfig.compiledPath(JPATH_COMPILER);
                List<JsonPath.Match> detectMatches = detectPath.evaluate(node.node);
                for (JsonPath.Match detectMatch : detectMatches) {
                    if (detectMatch.getNode().isTextual()) {
                        String detectValue = detectMatch.getNode().asText();
                        Pattern detectPattern = detectConfig.compiledValue(REGEX_COMPILER);
                        if (detectPattern.matcher(detectValue).matches()) {
                            filterBufferedValues(node, detectConfig.getSelectors());
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) List(java.util.List) 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) UrlRewriteFilterDetectDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor)

Example 12 with UrlRewriteFilterPathDescriptor

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

the class XmlFilterReader method processBufferedElement.

private void processBufferedElement(Level level, UrlRewriteFilterGroupDescriptor config) throws XPathExpressionException {
    for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
        if (selector instanceof UrlRewriteFilterApplyDescriptor) {
            XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
            Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
            if (node != null) {
                UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    String value = element.getTextContent();
                    value = filterText(extractQName(element), value, apply.rule());
                    element.setTextContent(value);
                } else if (node instanceof Text) {
                    Text text = (Text) node;
                    String value = text.getWholeText();
                    value = filterText(extractQName(text.getParentNode()), value, apply.rule());
                    text.replaceWholeText(value);
                } else if (node instanceof Attr) {
                    Attr attr = (Attr) node;
                    String value = attr.getValue();
                    value = filterAttribute(extractQName(attr.getOwnerElement()), extractQName(attr), value, apply.rule());
                    attr.setValue(value);
                } else {
                    throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
                }
            }
        } else if (selector instanceof UrlRewriteFilterDetectDescriptor) {
            XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
            Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
            if (node != null) {
                UrlRewriteFilterDetectDescriptor detect = (UrlRewriteFilterDetectDescriptor) selector;
                String value = null;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    value = element.getTextContent();
                } else if (node instanceof Text) {
                    Text text = (Text) node;
                    value = text.getWholeText();
                } else if (node instanceof Attr) {
                    Attr attr = (Attr) node;
                    value = attr.getValue();
                } else {
                    throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
                }
                if (detect.compiledValue(REGEX_COMPILER).matcher(value).matches()) {
                    processBufferedElement(level, detect);
                }
            }
        } else {
            throw new IllegalArgumentException(RES.unexpectedRewritePathSelector(selector));
        }
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) EndElement(javax.xml.stream.events.EndElement) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) Attr(org.w3c.dom.Attr) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor) UrlRewriteFilterDetectDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor)

Example 13 with UrlRewriteFilterPathDescriptor

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

the class XmlFilterReader method processCharacters.

private void processCharacters(Characters event) throws XPathExpressionException {
    // System.out.println( "T[" + event.isCData() + "," + event.isWhiteSpace() + "," + event.isIgnorableWhiteSpace() + "]=" + event );
    Level level = stack.peek();
    Node node = stack.peek().node;
    if (event.isCData()) {
        node.appendChild(document.createCDATASection(event.getData()));
    } else {
        node.appendChild(document.createTextNode(event.getData()));
    }
    if (!currentlyBuffering()) {
        String value = event.getData();
        if (!event.isWhiteSpace()) {
            if (level.scopeConfig == null || level.scopeConfig.getSelectors().isEmpty()) {
                value = filterText(extractQName(node), value, null);
            } else {
                UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath(level);
                if (path instanceof UrlRewriteFilterApplyDescriptor) {
                    String rule = ((UrlRewriteFilterApplyDescriptor) path).rule();
                    value = filterText(extractQName(node), value, rule);
                }
            }
        }
        if (event.isCData()) {
            writer.write("<![CDATA[");
            writer.write(value);
            writer.write("]]>");
        } else {
            writer.write(StringEscapeUtils.escapeXml(value));
        }
    }
}
Also used : Node(org.w3c.dom.Node) 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