Search in sources :

Example 41 with DataType

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

the class DataTypeFactoryTestCase method createsDataTypeForNonNullObject.

@Test
public void createsDataTypeForNonNullObject() throws Exception {
    DataType dataType = DataType.fromObject("test");
    assertThat(dataType, like(String.class, MediaType.ANY, null));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) DefaultCollectionDataType(org.mule.runtime.core.internal.metadata.DefaultCollectionDataType) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 42 with DataType

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

the class DataTypeMatchingTestCase method ifCharsetItsNotSpecifiedItShouldBeCompatibleWithAny.

@Test
public void ifCharsetItsNotSpecifiedItShouldBeCompatibleWithAny() throws Exception {
    DataType jsonWithCharset = builder(JSON_PARENT_DATA_TYPE).charset(UTF_8).build();
    assertThat(JSON_PARENT_DATA_TYPE, is(not(assignableTo(jsonWithCharset))));
    assertThat(jsonWithCharset, is(assignableTo(JSON_PARENT_DATA_TYPE)));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test)

Example 43 with DataType

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

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

the class AbstractTransformer method transform.

@Override
public Object transform(Object src, Charset enc) throws TransformerException {
    Object payload = src;
    DataType sourceType;
    if (src instanceof Message) {
        Message message = (Message) src;
        if ((!isSourceDataTypeSupported(DataType.MULE_MESSAGE, true) && !(this instanceof AbstractMessageTransformer))) {
            payload = message.getPayload().getValue();
            sourceType = message.getPayload().getDataType();
        } else {
            sourceType = DataType.fromObject(payload);
        }
    } else {
        sourceType = DataType.fromObject(payload);
    }
    if (!isSourceDataTypeSupported(sourceType)) {
        I18nMessage msg = transformOnObjectUnsupportedTypeOfEndpoint(getName(), payload.getClass());
        // / FIXME
        throw new TransformerException(msg, this);
    }
    if (logger.isDebugEnabled()) {
        logger.debug(format("Applying transformer %s (%s)", getName(), getClass().getName()));
        logger.debug(format("Object before transform: %s", StringMessageUtils.toString(payload)));
    }
    Object result = doTransform(payload, enc);
    if (logger.isDebugEnabled()) {
        logger.debug(format("Object after transform: %s", StringMessageUtils.toString(result)));
    }
    checkTransformerReturnClass(this, result);
    return result;
}
Also used : Message(org.mule.runtime.api.message.Message) I18nMessage(org.mule.runtime.api.i18n.I18nMessage) DataType(org.mule.runtime.api.metadata.DataType) I18nMessage(org.mule.runtime.api.i18n.I18nMessage)

Example 45 with DataType

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

the class ExtendedTransformationService method mergeMediaType.

private MediaType mergeMediaType(Message message, DataType transformed) {
    DataType original = message.getPayload().getDataType();
    MediaType mimeType = ANY.matches(transformed.getMediaType()) ? original.getMediaType() : transformed.getMediaType();
    Charset encoding = transformed.getMediaType().getCharset().orElse(message.getPayload().getDataType().getMediaType().getCharset().orElse(getDefaultEncoding(muleContext)));
    return DataType.builder().mediaType(mimeType).charset(encoding).build().getMediaType();
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) MediaType(org.mule.runtime.api.metadata.MediaType) Charset(java.nio.charset.Charset)

Aggregations

DataType (org.mule.runtime.api.metadata.DataType)102 Test (org.junit.Test)67 SmallTest (org.mule.tck.size.SmallTest)29 DefaultCollectionDataType (org.mule.runtime.core.internal.metadata.DefaultCollectionDataType)24 FunctionDataType (org.mule.runtime.api.metadata.FunctionDataType)20 DefaultFunctionDataType (org.mule.runtime.core.internal.metadata.DefaultFunctionDataType)20 DefaultMapDataType (org.mule.runtime.core.internal.metadata.DefaultMapDataType)20 SimpleDataType (org.mule.runtime.core.internal.metadata.SimpleDataType)20 Message (org.mule.runtime.api.message.Message)18 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)18 TypedValue (org.mule.runtime.api.metadata.TypedValue)12 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)12 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)10 CollectionDataType (org.mule.runtime.api.metadata.CollectionDataType)10 Transformer (org.mule.runtime.core.api.transformer.Transformer)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ParserContext (org.mule.mvel2.ParserContext)7 MapDataType (org.mule.runtime.api.metadata.MapDataType)6 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)6