Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
import javax.xml.namespace.*;
import javax.xml.xpath.*;

import java.io.IOException;

import static com.predic8.membrane.core.util.XMLUtil.*;
import static java.lang.Boolean.FALSE;
import static javax.xml.xpath.XPathConstants.*;

public class XPathExchangeExpression extends AbstractExchangeExpression {
Expand All @@ -41,6 +44,18 @@ public XPathExchangeExpression(String xpath) {
@Override
public <T> T evaluate(Exchange exchange, Interceptor.Flow flow, Class<T> type) {
Message msg = exchange.getMessage(flow);

// Guard against empty body and other Content-Types
try {
if (msg.isBodyEmpty() || !msg.isXML()) {
log.debug("Body is empty or Content-Type not XML. Nothing to evaluate for expression: {}", expression);
return resultForNoEvaluation(type);
}
} catch (IOException e) {
log.error("Error checking if body is empty", e);
return resultForNoEvaluation(type);
}

try {
if (Boolean.class.isAssignableFrom(type)) {
return type.cast( evalutateAndCast(msg, BOOLEAN));
Expand Down Expand Up @@ -75,4 +90,14 @@ private Object evalutateAndCast(Message msg, QName xmlType) throws XPathExpressi
// XPath is not thread safe! Therefore every time the factory is called!
return factory.newXPath().evaluate(expression, getInputSource(msg), xmlType);
}

private <T> T resultForNoEvaluation(Class<T> type) {
if (String.class.isAssignableFrom(type)) {
return type.cast("");
}
if (Boolean.class.isAssignableFrom(type)) {
return type.cast(FALSE);
}
return type.cast(new Object());
}
}