Esempio n. 1
0
  private boolean accept(Object obj) {
    if (obj == null) {
      logger.warn("Applying JXPathFilter to null object.");
      return false;
    }
    if (pattern == null) {
      logger.warn("Expression for JXPathFilter is not set.");
      return false;
    }
    if (expectedValue == null) {
      // Handle the special case where the expected value really is null.
      if (pattern.endsWith("= null") || pattern.endsWith("=null")) {
        expectedValue = "null";
        pattern = pattern.substring(0, pattern.lastIndexOf("="));
      } else {
        if (logger.isInfoEnabled()) {
          logger.info("Expected value for JXPathFilter is not set, using 'true' by default");
        }
        expectedValue = Boolean.TRUE.toString();
      }
    }

    Object xpathResult = null;
    boolean accept = false;

    Document dom4jDoc;
    try {
      dom4jDoc = XMLUtils.toDocument(obj, muleContext);
    } catch (Exception e) {
      logger.warn(
          "JxPath filter rejected message because of an error while parsing XML: " + e.getMessage(),
          e);
      return false;
    }

    // Payload is XML
    if (dom4jDoc != null) {
      if (namespaces == null) {
        // no namespace defined, let's perform a direct evaluation
        xpathResult = dom4jDoc.valueOf(pattern);
      } else {
        // create an xpath expression with namespaces and evaluate it
        XPath xpath = DocumentHelper.createXPath(pattern);
        xpath.setNamespaceURIs(namespaces);
        xpathResult = xpath.valueOf(dom4jDoc);
      }
    }
    // Payload is a Java object
    else {
      if (logger.isDebugEnabled()) {
        logger.debug("Passing object of type " + obj.getClass().getName() + " to JXPathContext");
      }
      JXPathContext context = JXPathContext.newContext(obj);
      initialise(context);
      xpathResult = context.getValue(pattern);
    }

    if (logger.isDebugEnabled()) {
      logger.debug(
          "JXPathFilter Expression result = '"
              + xpathResult
              + "' -  Expected value = '"
              + expectedValue
              + "'");
    }
    // Compare the XPath result with the expected result.
    if (xpathResult != null) {
      accept = xpathResult.toString().equals(expectedValue);
    } else {
      // A null result was actually expected.
      if (expectedValue.equals("null")) {
        accept = true;
      }
      // A null result was not expected, something probably went wrong.
      else {
        logger.warn("JXPathFilter expression evaluates to null: " + pattern);
      }
    }

    if (logger.isDebugEnabled()) {
      logger.debug("JXPathFilter accept object  : " + accept);
    }

    return accept;
  }
Esempio n. 2
0
  private boolean accept(Object obj) {
    if (obj == null) {
      logger.warn("Applying JXPathFilter to null object.");
      return false;
    }
    if (pattern == null) {
      logger.warn("Expression for JXPathFilter is not set.");
      return false;
    }
    if (expectedValue == null) {
      // Handle the special case where the expected value really is null.
      if (pattern.endsWith("= null") || pattern.endsWith("=null")) {
        expectedValue = "null";
        pattern = pattern.substring(0, pattern.lastIndexOf("="));
      } else {
        if (logger.isInfoEnabled()) {
          logger.info("Expected value for JXPathFilter is not set, using 'true' by default");
        }
        expectedValue = Boolean.TRUE.toString();
      }
    }

    Object xpathResult = null;
    boolean accept = false;

    // Payload is a DOM Document
    if (obj instanceof Document) {
      if (namespaces == null) {
        // no namespace defined, let's perform a direct evaluation
        xpathResult = ((Document) obj).valueOf(pattern);
      } else {
        // create an xpath expression with namespaces and evaluate it
        XPath xpath = DocumentHelper.createXPath(pattern);
        xpath.setNamespaceURIs(namespaces);
        xpathResult = xpath.valueOf(obj);
      }

    }
    // Payload is a String of XML
    else if (obj instanceof String) {
      try {
        return accept(DocumentHelper.parseText((String) obj));
      } catch (DocumentException e) {
        logger.warn("JXPathFilter unable to parse XML document: " + e.getMessage(), e);
        if (logger.isDebugEnabled())
          logger.debug("XML = " + StringMessageUtils.truncate((String) obj, 200, false));
        return false;
      }
    }
    // Payload is a Java object
    else {
      if (logger.isDebugEnabled()) {
        logger.debug("Passing object of type " + obj.getClass().getName() + " to JXPathContext");
      }
      JXPathContext context = JXPathContext.newContext(obj);
      initialise(context);
      xpathResult = context.getValue(pattern);
    }

    if (logger.isDebugEnabled()) {
      logger.debug(
          "JXPathFilter Expression result = '"
              + xpathResult
              + "' -  Expected value = '"
              + expectedValue
              + "'");
    }
    // Compare the XPath result with the expected result.
    if (xpathResult != null) {
      accept = xpathResult.toString().equals(expectedValue);
    } else {
      // A null result was actually expected.
      if (expectedValue.equals("null")) {
        accept = true;
      }
      // A null result was not expected, something probably went wrong.
      else {
        logger.warn("JXPathFilter expression evaluates to null: " + pattern);
      }
    }

    if (logger.isDebugEnabled()) {
      logger.debug("JXPathFilter accept object  : " + accept);
    }

    return accept;
  }