Search in sources :

Example 76 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class AbstractMessageSequenceSplitter method initEventBuilder.

private void initEventBuilder(Object sequenceValue, CoreEvent originalEvent, Builder builder, Map<String, ?> flowVarsFromLastResult) {
    if (sequenceValue instanceof EventBuilderConfigurer) {
        ((EventBuilderConfigurer) sequenceValue).configure(builder);
    } else if (sequenceValue instanceof CoreEvent) {
        final CoreEvent payloadAsEvent = (CoreEvent) sequenceValue;
        builder.message(payloadAsEvent.getMessage());
        for (String flowVarName : payloadAsEvent.getVariables().keySet()) {
            if (!flowVarsFromLastResult.containsKey(flowVarName)) {
                builder.addVariable(flowVarName, payloadAsEvent.getVariables().get(flowVarName).getValue(), payloadAsEvent.getVariables().get(flowVarName).getDataType());
            }
        }
    } else if (sequenceValue instanceof Message) {
        final Message message = (Message) sequenceValue;
        builder.message(message);
    } else if (sequenceValue instanceof TypedValue) {
        builder.message(Message.builder().payload((TypedValue) sequenceValue).build());
    } else if (sequenceValue instanceof Collection) {
        builder.message(Message.builder(originalEvent.getMessage()).value(((Collection) sequenceValue).stream().map(v -> v instanceof TypedValue ? ((TypedValue) v).getValue() : v).collect(toList())).build());
    } else {
        builder.message(Message.builder(originalEvent.getMessage()).value(sequenceValue).build());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collection(java.util.Collection) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 77 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class DefaultSchedulerMessageSource method poll.

/**
 * Triggers the forced execution of the polling message processor ignoring the configured scheduler.
 */
private void poll() {
    boolean execute = false;
    synchronized (this) {
        if (disallowConcurrentExecution && executing) {
            execute = false;
        } else {
            execute = true;
            executing = true;
        }
    }
    if (execute) {
        Message request = of(null);
        pollWith(request);
    } else {
        LOGGER.info("Flow '{}' is already running and 'disallowConcurrentExecution' is set to 'true'. Execution skipped.", flowConstruct.getRootContainerLocation().getGlobalName());
    }
}
Also used : Message(org.mule.runtime.api.message.Message)

Example 78 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class ExtendedTransformationService method applyAllTransformers.

private Message applyAllTransformers(final Message message, final CoreEvent event, final List<? extends Transformer> transformers) throws MuleException {
    Message result = message;
    if (!transformers.isEmpty()) {
        for (int index = 0; index < transformers.size(); index++) {
            Transformer transformer = transformers.get(index);
            Class<?> srcCls = result.getPayload().getDataType().getType();
            DataType originalSourceType = DataType.fromType(srcCls);
            if (transformer.isSourceDataTypeSupported(originalSourceType)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Using " + transformer + " to transform payload.");
                }
                result = transformMessage(result, event, transformer);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Transformer " + transformer + " doesn't support the source payload: " + srcCls);
                }
                if (canSkipTransformer(result, transformers, index)) {
                    continue;
                }
                // Resolves implicit conversion if possible
                Transformer implicitTransformer = muleContext.getDataTypeConverterResolver().resolve(originalSourceType, transformer.getSourceDataTypes());
                if (implicitTransformer != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Performing implicit transformation with: " + transformer);
                    }
                    result = transformMessage(result, event, implicitTransformer);
                    result = transformMessage(result, event, transformer);
                } else {
                    throw new IllegalArgumentException("Cannot apply transformer " + transformer + " on source payload: " + srcCls);
                }
            }
        }
        Transformer lastTransformer = transformers.get(transformers.size() - 1);
        DataType returnDataType = lastTransformer.getReturnDataType();
        checkResultDataType(message, returnDataType, result.getPayload().getValue());
    }
    return result;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MessageTransformer(org.mule.runtime.core.api.transformer.MessageTransformer) Message(org.mule.runtime.api.message.Message) DataType(org.mule.runtime.api.metadata.DataType)

Example 79 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class TransformerChain method transformMessage.

@Override
public Object transformMessage(CoreEvent event, Charset outputEncoding) throws MessageTransformerException {
    Message result = event.getMessage();
    Object temp = event.getMessage();
    Transformer lastTransformer = null;
    for (Object element : transformers) {
        lastTransformer = (Transformer) element;
        try {
            temp = lastTransformer.transform(temp);
        } catch (TransformerException e) {
            throw new MessageTransformerException(lastTransformer, e, event.getMessage());
        }
        if (temp instanceof Message) {
            result = (Message) temp;
        } else {
            result = Message.builder(event.getMessage()).value(temp).build();
            event = CoreEvent.builder(event).message(result).build();
        }
    }
    if (lastTransformer != null && Message.class.isAssignableFrom(lastTransformer.getReturnDataType().getType())) {
        return result;
    } else {
        return result.getPayload().getValue();
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) AbstractMessageTransformer(org.mule.runtime.core.api.transformer.AbstractMessageTransformer) Message(org.mule.runtime.api.message.Message) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 80 with Message

use of org.mule.runtime.api.message.Message in project mule by mulesoft.

the class BytesStreamingExtensionTestCase method rewind.

@Test
@Description("Rewing a stream and consume it twice")
public void rewind() throws Exception {
    CoreEvent result = flowRunner("rewind").withPayload(data).run();
    Message firstRead = (Message) result.getVariables().get("firstRead").getValue();
    Message secondRead = (Message) result.getVariables().get("secondRead").getValue();
    assertThat(firstRead.getPayload().getValue(), equalTo(data));
    assertThat(secondRead.getPayload().getValue(), equalTo(data));
}
Also used : Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Description(io.qameta.allure.Description) Test(org.junit.Test)

Aggregations

Message (org.mule.runtime.api.message.Message)226 Test (org.junit.Test)189 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)104 SmallTest (org.mule.tck.size.SmallTest)68 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)46 Transformer (org.mule.runtime.core.api.transformer.Transformer)35 DataType (org.mule.runtime.api.metadata.DataType)33 ArrayList (java.util.ArrayList)26 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)26 Processor (org.mule.runtime.core.api.processor.Processor)25 List (java.util.List)20 Assert.assertThat (org.junit.Assert.assertThat)19 ExpectedException (org.junit.rules.ExpectedException)19 MuleContextUtils.eventBuilder (org.mule.tck.util.MuleContextUtils.eventBuilder)19 Map (java.util.Map)18 Rule (org.junit.Rule)18 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)18 ExpectedException.none (org.junit.rules.ExpectedException.none)17 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)17 TypedValue (org.mule.runtime.api.metadata.TypedValue)16