Ejemplo n.º 1
0
  public Object onCall(UMOEventContext context) throws Exception {
    String contents = context.getTransformedMessageAsString();
    String msg =
        StringMessageUtils.getBoilerPlate(
            "Message Received in component: "
                + context.getComponentDescriptor().getName()
                + ". Content is: "
                + StringMessageUtils.truncate(contents, 100, true),
            '*',
            80);

    logger.info(msg);

    if (eventCallback != null) {
      eventCallback.eventReceived(context, this);
    }

    Object replyMessage;
    if (returnMessage != null) {
      replyMessage = returnMessage;
    } else {
      replyMessage =
          contents
              + " Received"
              + (appendComponentName ? " " + context.getComponentDescriptor().getName() : "");
    }

    MuleManager.getInstance()
        .fireNotification(
            new FunctionalTestNotification(
                context, replyMessage, FunctionalTestNotification.EVENT_RECEIVED));

    if (throwException) {
      throw new MuleException(Message.createStaticMessage("Functional Test Component Exception"));
    }

    return replyMessage;
  }
Ejemplo 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;
  }