@Test
  public void testDispatchNotification() throws Exception {
    TestEndpointMessageNotificationListener listener =
        new TestEndpointMessageNotificationListener(2);
    muleContext.registerListener(listener);

    OutboundEndpoint endpoint =
        createOutboundEndpoint(null, null, null, null, MessageExchangePattern.ONE_WAY, null);
    MuleEvent outboundEvent = createTestOutboundEvent();

    endpoint.process(outboundEvent);

    assertEventDispatched();
    assertTrue(listener.latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals(2, listener.messageNotificationList.size());
    assertEquals(
        EndpointMessageNotification.MESSAGE_DISPATCH_BEGIN,
        listener.messageNotificationList.get(0).getAction());
    assertEquals(
        EndpointMessageNotification.MESSAGE_DISPATCH_END,
        listener.messageNotificationList.get(1).getAction());
    assertTrue(listener.messageNotificationList.get(0).getSource() instanceof MuleMessage);
    assertTrue(listener.messageNotificationList.get(1).getSource() instanceof MuleMessage);
    assertEquals(
        outboundEvent.getMessage().getPayload(),
        listener.messageNotificationList.get(0).getSource().getPayload());
    assertEquals(
        outboundEvent.getMessage().getPayload(),
        listener.messageNotificationList.get(1).getSource().getPayload());
  }
  @Test(expected = UnsupportedOperationException.class)
  public void testConnectorNotAvailableOnDynamicEndpoint() throws Exception {
    OutboundEndpoint endpoint =
        createOutboundEndpoint(
            null, null, null, null, MessageExchangePattern.REQUEST_RESPONSE, null);

    endpoint.getConnector();
  }
  @Test
  public void testDefaultFlowOneWay() throws Exception {
    OutboundEndpoint endpoint =
        createOutboundEndpoint(null, null, null, null, MessageExchangePattern.ONE_WAY, null);
    assertTrue(endpoint instanceof DynamicOutboundEndpoint);

    testOutboundEvent = createTestOutboundEvent();

    MuleEvent result = endpoint.process(testOutboundEvent);

    assertEventDispatched();
    assertSame(VoidMuleEvent.getInstance(), result);
    assertMessageSentEqual(MyMessageDispatcherFactory.dispatcher.sensedDispatchEvent);
  }
Exemplo n.º 4
0
 public JmsMessageDispatcher(OutboundEndpoint endpoint) {
   super(endpoint);
   this.connector = (JmsConnector) endpoint.getConnector();
   disableTemporaryDestinations =
       connector.isDisableTemporaryReplyToDestinations()
           || ("true"
               .equals(endpoint.getProperty(JmsConstants.DISABLE_TEMP_DESTINATIONS_PROPERTY)));
   returnOriginalMessageAsReply =
       connector.isReturnOriginalMessageAsReply()
           || ("true".equals(endpoint.getProperty(JmsConstants.RETURN_ORIGINAL_MESSAGE_PROPERTY)));
   if (returnOriginalMessageAsReply && !disableTemporaryDestinations) {
     logger.warn(
         "The returnOriginalMessageAsReply property will be ignored because disableTemporaryReplyToDestinations=false.  You need to disable temporary ReplyTo destinations in order for this propery to take effect.");
   }
 }
  @Test
  public void testDefaultFlowRequestResponse() throws Exception {
    OutboundEndpoint endpoint =
        createOutboundEndpoint(
            null, null, null, null, MessageExchangePattern.REQUEST_RESPONSE, null);
    testOutboundEvent = createTestOutboundEvent();

    MuleEvent result = endpoint.process(testOutboundEvent);

    assertEventSent();

    assertSame(responseMessage, result.getMessage());

    assertEqualMessages(responseMessage, result.getMessage());
  }
Exemplo n.º 6
0
  public void dispatchEvent(MuleEvent event) throws MuleException {
    if (stopping.get() || stopped.get()) {
      throw new ServiceException(CoreMessages.componentIsStopped(name), event.getMessage(), this);
    }

    // Dispatching event to an inbound endpoint
    // in the DefaultMuleSession#dispatchEvent
    ImmutableEndpoint endpoint = event.getEndpoint();

    if (endpoint instanceof OutboundEndpoint) {
      try {
        ((OutboundEndpoint) endpoint).dispatch(event);
      } catch (Exception e) {
        throw new DispatchException(event.getMessage(), event.getEndpoint(), e);
      }

      return;
    }

    if (logger.isDebugEnabled()) {
      logger.debug(
          "Service: "
              + name
              + " has received asynchronous event on: "
              + event.getEndpoint().getEndpointURI());
    }

    // Dispatching event to the service
    if (stats.isEnabled()) {
      stats.incReceivedEventASync();
    }
    doDispatch(event);
  }
  @Test
  public void testTimeoutSetOnEvent() throws Exception {
    int testTimeout = 999;

    OutboundEndpoint endpoint =
        createOutboundEndpoint(
            null, null, null, null, MessageExchangePattern.REQUEST_RESPONSE, null);

    testOutboundEvent = createTestOutboundEvent();
    testOutboundEvent
        .getMessage()
        .setOutboundProperty(MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, testTimeout);

    MuleEvent response = endpoint.process(testOutboundEvent);

    assertEquals(testTimeout, response.getTimeout());
  }
  @Test
  public void testSecurityFilterAccept() throws Exception {
    OutboundEndpoint endpoint =
        createOutboundEndpoint(
            null,
            new TestSecurityFilter(true),
            null,
            null,
            MessageExchangePattern.REQUEST_RESPONSE,
            null);
    testOutboundEvent = createTestOutboundEvent();

    MuleEvent result = endpoint.process(testOutboundEvent);

    assertEventSent();
    assertMessageSentEqual(MyMessageDispatcherFactory.dispatcher.sensedSendEvent);

    assertSame(responseMessage, result.getMessage());

    assertEqualMessages(responseMessage, result.getMessage());
  }
Exemplo n.º 9
0
  /** @inheritDocs */
  protected MuleMessage getMessagePart(MuleMessage message, OutboundEndpoint endpoint) {
    List payloads = (List) payloadContext.get();

    for (Iterator i = payloads.iterator(); i.hasNext(); ) {
      Object payload = i.next();
      MuleMessage result = new DefaultMuleMessage(payload, (Map) propertiesContext.get());
      // If there is no filter assume that the endpoint can accept the
      // message. Endpoints will be processed in order to only the last
      // (if any) of the the endpoints may not have a filter
      if (endpoint.getFilter() == null || endpoint.getFilter().accept(result)) {
        if (logger.isDebugEnabled()) {
          logger.debug(
              "Endpoint filter matched. Routing message over: "
                  + endpoint.getEndpointURI().toString());
        }
        i.remove();
        return result;
      }
    }

    return null;
  }
  @Test
  public void testTransformers() throws Exception {
    OutboundEndpoint endpoint =
        createOutboundEndpoint(
            null,
            null,
            new OutboundAppendTransformer(),
            new ResponseAppendTransformer(),
            MessageExchangePattern.REQUEST_RESPONSE,
            null);

    testOutboundEvent = createTestOutboundEvent();

    MuleEvent result = endpoint.process(testOutboundEvent);

    assertNotNull(result);
    assertEquals(
        TEST_MESSAGE + OutboundAppendTransformer.APPEND_STRING,
        MyMessageDispatcherFactory.dispatcher.sensedSendEvent.getMessageAsString());
    assertEquals(
        RESPONSE_MESSAGE + ResponseAppendTransformer.APPEND_STRING, result.getMessageAsString());
  }
  @Test
  public void cachesResolvedStaticEndpoints() throws Exception {
    OutboundEndpoint prototypeEndpoint = mock(OutboundEndpoint.class);
    when(prototypeEndpoint.getMuleContext()).thenReturn(muleContext);

    EndpointBuilder staticEndpointBuilder = mock(EndpointBuilder.class);
    when(staticEndpointBuilder.buildOutboundEndpoint()).thenReturn(prototypeEndpoint);

    EndpointBuilder endpointBuilder = mock(EndpointBuilder.class);
    when(endpointBuilder.buildOutboundEndpoint()).thenReturn(prototypeEndpoint);
    when(endpointBuilder.clone()).thenReturn(staticEndpointBuilder);

    DynamicOutboundEndpoint dynamicOutboundEndpoint =
        new DynamicOutboundEndpoint(
            endpointBuilder,
            new DynamicURIBuilder(new URIBuilder("test://localhost:#[header:port]", muleContext)));

    testOutboundEvent = createTestOutboundEvent();
    dynamicOutboundEndpoint.process(testOutboundEvent);
    dynamicOutboundEndpoint.process(testOutboundEvent);

    verify(endpointBuilder, times(1)).buildOutboundEndpoint();
  }
  @Test
  public void testSecurityFilterNotAccept() throws Exception {
    TestSecurityNotificationListener securityNotificationListener =
        new TestSecurityNotificationListener();
    muleContext.registerListener(securityNotificationListener);

    OutboundEndpoint endpoint =
        createOutboundEndpoint(
            null,
            new TestSecurityFilter(false),
            null,
            null,
            MessageExchangePattern.REQUEST_RESPONSE,
            null);
    testOutboundEvent = createTestOutboundEvent();

    try {
      endpoint.process(testOutboundEvent);
      fail("Exception expected");
    } catch (TestSecurityFilter.StaticMessageUnauthorisedException e) {
      testOutboundEvent
          .getFlowConstruct()
          .getExceptionListener()
          .handleException(e, testOutboundEvent);
    }

    assertNull(MyMessageDispatcherFactory.dispatcher);

    assertTrue(securityNotificationListener.latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals(
        SecurityNotification.SECURITY_AUTHENTICATION_FAILED,
        securityNotificationListener.securityNotification.getAction());
    assertEquals(
        securityNotificationListener.securityNotification.getResourceIdentifier(),
        TestSecurityFilter.StaticMessageUnauthorisedException.class.getName());
  }
Exemplo n.º 13
0
 public JmsMessageDispatcher(OutboundEndpoint endpoint) {
   super(endpoint);
   this.connector = (JmsConnector) endpoint.getConnector();
 }
  @Test
  public void testOutboundMessageRouter() throws Exception {
    DefaultOutboundRouterCollection messageRouter =
        createObject(DefaultOutboundRouterCollection.class);
    messageRouter.setCatchAllStrategy(new LoggingCatchAllStrategy());
    assertNotNull(messageRouter.getCatchAllStrategy());

    OutboundEndpoint endpoint1 =
        getTestOutboundEndpoint("Test1Provider", "test://Test1Provider?exchangePattern=one-way");
    assertNotNull(endpoint1);
    OutboundEndpoint mockendpoint1 = RouterTestUtils.createMockEndpoint(endpoint1);

    OutboundEndpoint endpoint2 = getTestOutboundEndpoint("Test2Provider");
    assertNotNull(endpoint2);
    OutboundEndpoint mockendpoint2 = RouterTestUtils.createMockEndpoint(endpoint2);

    FilteringOutboundRouter router1 = new FilteringOutboundRouter();
    PayloadTypeFilter filter = new PayloadTypeFilter(String.class);
    router1.setFilter(filter);
    List<MessageProcessor> endpoints = new ArrayList<MessageProcessor>();
    endpoints.add(mockendpoint1);
    router1.setRoutes(endpoints);

    FilteringOutboundRouter router2 = new FilteringOutboundRouter();
    PayloadTypeFilter filter2 = new PayloadTypeFilter();
    filter2.setExpectedType(Exception.class);
    router2.setFilter(filter2);
    endpoints = new ArrayList<MessageProcessor>();
    endpoints.add(mockendpoint2);
    router2.setRoutes(endpoints);

    messageRouter.addRoute(router1);
    assertEquals(1, messageRouter.getRoutes().size());
    messageRouter.removeRoute(router1);
    assertEquals(0, messageRouter.getRoutes().size());

    List<MatchableMessageProcessor> list = new ArrayList<MatchableMessageProcessor>();
    list.add(router1);
    list.add(router2);
    messageRouter.setMessageProcessors(list);

    MuleSession session = mock(MuleSession.class);
    MuleEvent event = getTestEvent("test event", session);

    when(mockendpoint1.process(any(MuleEvent.class))).thenAnswer(new MuleEventCheckAnswer());
    messageRouter.process(event);

    event = getTestEvent(new IllegalArgumentException(), session);

    when(mockendpoint2.process(any(MuleEvent.class))).thenAnswer(new MuleEventCheckAnswer());
    messageRouter.process(event);

    FilteringOutboundRouter router3 = new FilteringOutboundRouter();
    router3.setFilter(new PayloadTypeFilter(Object.class));
    endpoints = new ArrayList<MessageProcessor>();
    endpoints.add(mockendpoint2);
    router3.setRoutes(endpoints);
    messageRouter.addRoute(router3);

    // now the message should be routed twice to different targets
    event = getTestEvent("testing multiple routing", session);

    messageRouter.setMatchAll(true);
    messageRouter.process(event);
  }