Search in sources :

Example 36 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class StreamingUtils method consumeRepeatablePayload.

/**
 * If the {@code event} has a repeatable payload (instance of {@link CursorProvider}), then this method returns a new
 * event which payload has an equivalent, already consumed structure. This functionality makes sense for cases like
 * caching in which the contents of the stream need to survive the completion of the event that generated it.
 * <p>
 * If the payload is a {@link CursorStreamProvider}, then it will be consumed into a {@link ByteArrayCursorStreamProvider}
 * so that the contents are fully in memory while still keeping repeatable byte streaming semantics.
 * <p>
 * If the payload is a {@link CursorIteratorProvider}, then the contents will be consumed into a {@link List}.
 * <p>
 * In any other case, the same input event is returned
 *
 * @param event an event which might have a repeatable payload
 * @return a {@link CoreEvent}
 * @since 4.1
 */
public static CoreEvent consumeRepeatablePayload(CoreEvent event) {
    TypedValue payload = event.getMessage().getPayload();
    final Object originalPayload = payload.getValue();
    if (originalPayload == null) {
        return event;
    }
    DataType originalDataType = payload.getDataType();
    TypedValue replacedPayload = null;
    if (originalPayload instanceof CursorStreamProvider) {
        Object consumedPayload = asCursorProvider(toByteArray((CursorStreamProvider) originalPayload));
        replacedPayload = new TypedValue(consumedPayload, DataType.builder(originalDataType).type(consumedPayload.getClass()).build());
    } else if (originalPayload instanceof CursorIteratorProvider) {
        List consumed = new LinkedList<>();
        ((CursorIteratorProvider) originalPayload).openCursor().forEachRemaining(consumed::add);
        DataType newDataType;
        if (originalDataType instanceof CollectionDataType) {
            final CollectionDataType collectionDataType = (CollectionDataType) originalDataType;
            newDataType = DataType.builder(originalDataType).collectionType(consumed.getClass()).itemType(collectionDataType.getItemDataType().getType()).itemMediaType(collectionDataType.getItemDataType().getMediaType()).build();
        } else {
            newDataType = DataType.builder(originalDataType).type(consumed.getClass()).build();
        }
        replacedPayload = new TypedValue(consumed, newDataType);
    }
    if (replacedPayload != null) {
        event = CoreEvent.builder(event).message(Message.builder(event.getMessage()).payload(replacedPayload).build()).build();
    }
    return event;
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) ByteArrayCursorStreamProvider(org.mule.runtime.core.internal.streaming.bytes.ByteArrayCursorStreamProvider) ListCursorIteratorProvider(org.mule.runtime.core.internal.streaming.object.ListCursorIteratorProvider) CursorIteratorProvider(org.mule.runtime.api.streaming.object.CursorIteratorProvider) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) List(java.util.List) LinkedList(java.util.LinkedList) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 37 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class MuleFunctionsBindingContextProvider method getBindingContext.

@Override
public BindingContext getBindingContext() {
    BindingContext.Builder builder = BindingContext.builder();
    PropertyAccessFunction propertyFunction = new PropertyAccessFunction(configurationProperties);
    builder.addBinding("p", new TypedValue(propertyFunction, fromFunction(propertyFunction)));
    ExpressionFunction lookupFunction = new LookupFunction(componentLocator);
    builder.addBinding("lookup", new TypedValue(lookupFunction, fromFunction(lookupFunction)));
    ExpressionFunction causedByFunction = new CausedByFunction(errorTypeRepository);
    builder.addBinding("causedBy", new TypedValue(causedByFunction, fromFunction(causedByFunction)));
    return builder.build();
}
Also used : ExpressionFunction(org.mule.runtime.api.el.ExpressionFunction) BindingContext(org.mule.runtime.api.el.BindingContext) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 38 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class EventToMessageSequenceSplittingStrategy method split.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public MessageSequence<?> split(CoreEvent event) {
    if (expressionSplitterStrategy.hasDefaultExpression()) {
        Message msg = event.getMessage();
        Object payload = msg.getPayload().getValue();
        if (payload instanceof MessageSequence<?>) {
            return ((MessageSequence<?>) payload);
        }
        if (payload instanceof Iterator<?>) {
            return new IteratorMessageSequence(((Iterator<Object>) payload));
        }
        if (payload instanceof Collection) {
            return new CollectionMessageSequence(copyCollection((Collection) payload));
        } else if (payload instanceof CursorIteratorProvider) {
            return new IteratorMessageSequence(((CursorIteratorProvider) payload).openCursor());
        } else if (payload instanceof Iterable<?>) {
            return new IteratorMessageSequence(((Iterable<Object>) payload).iterator());
        } else if (payload instanceof Object[]) {
            return new ArrayMessageSequence((Object[]) payload);
        } else if (payload instanceof NodeList) {
            return new NodeListMessageSequence((NodeList) payload);
        } else if (payload instanceof Map<?, ?>) {
            List<Object> list = new LinkedList<>();
            Set<Map.Entry<?, ?>> set = ((Map) payload).entrySet();
            for (Map.Entry<?, ?> entry : set) {
                list.add(new ImmutableEntry<>(entry));
            }
            return new CollectionMessageSequence(list);
        }
    }
    try {
        Iterator<TypedValue<?>> valueIterator = expressionSplitterStrategy.split(event);
        if (hasMelExpression(expressionSplitterStrategy.getExpression())) {
            List<Object> iteratorCollection = new ArrayList<>();
            valueIterator.forEachRemaining(iteratorCollection::add);
            return new CollectionMessageSequence<>(iteratorCollection);
        }
        return new IteratorMessageSequence(valueIterator);
    } catch (ExpressionRuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalArgumentException(format("Could not split result of expression %s. The provided value is not instance of %s java " + "type or it's not a collection in any other format", expressionSplitterStrategy.getExpression(), new Class[] { Iterable.class, Iterator.class, MessageSequence.class, Collection.class }), e);
    }
}
Also used : ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) NodeListMessageSequence(org.mule.runtime.core.internal.routing.outbound.NodeListMessageSequence) Set(java.util.Set) Message(org.mule.runtime.api.message.Message) ArrayList(java.util.ArrayList) CursorIteratorProvider(org.mule.runtime.api.streaming.object.CursorIteratorProvider) ImmutableEntry(org.mule.runtime.internal.util.collection.ImmutableEntry) CollectionMessageSequence(org.mule.runtime.core.internal.routing.outbound.CollectionMessageSequence) Iterator(java.util.Iterator) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TypedValue(org.mule.runtime.api.metadata.TypedValue) ImmutableEntry(org.mule.runtime.internal.util.collection.ImmutableEntry) NodeList(org.w3c.dom.NodeList) IteratorMessageSequence(org.mule.runtime.core.internal.routing.outbound.IteratorMessageSequence) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) NodeListMessageSequence(org.mule.runtime.core.internal.routing.outbound.NodeListMessageSequence) IteratorMessageSequence(org.mule.runtime.core.internal.routing.outbound.IteratorMessageSequence) CollectionMessageSequence(org.mule.runtime.core.internal.routing.outbound.CollectionMessageSequence) Collection(java.util.Collection) Map(java.util.Map)

Example 39 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue 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 40 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class SoapOperationExecutor method evaluateHeaders.

private Object evaluateHeaders(InputStream headers) {
    String hs = IOUtils.toString(headers);
    BindingContext context = BindingContext.builder().addBinding("payload", new TypedValue<>(hs, DataType.XML_STRING)).build();
    return expressionExecutor.evaluate("%dw 2.0 \n" + "output application/java \n" + "---\n" + "payload.headers mapObject (value, key) -> {\n" + "    '$key' : write((key): value, \"application/xml\")\n" + "}", context).getValue();
}
Also used : BindingContext(org.mule.runtime.api.el.BindingContext) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Aggregations

TypedValue (org.mule.runtime.api.metadata.TypedValue)97 Test (org.junit.Test)74 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)47 DataType (org.mule.runtime.api.metadata.DataType)17 Message (org.mule.runtime.api.message.Message)16 Description (io.qameta.allure.Description)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 List (java.util.List)11 SmallTest (org.mule.tck.size.SmallTest)10 BindingContext (org.mule.runtime.api.el.BindingContext)9 Map (java.util.Map)8 Optional (java.util.Optional)8 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)5 ArrayList (java.util.ArrayList)4 Matchers.anyString (org.mockito.Matchers.anyString)4 MuleException (org.mule.runtime.api.exception.MuleException)4 Error (org.mule.runtime.api.message.Error)4 ErrorType (org.mule.runtime.api.message.ErrorType)4