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;
}
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);
}
}
}
}
}
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);
}
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;
}
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);
}
}
}
Aggregations