Search in sources :

Example 81 with DataType

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

the class AbstractMessageTransformer method transform.

@Override
public final Object transform(Object src, Charset enc, CoreEvent event) throws MessageTransformerException {
    DataType sourceType = DataType.fromType(src.getClass());
    if (!isSourceDataTypeSupported(sourceType)) {
        if (isIgnoreBadInput()) {
            logger.debug("Source type is incompatible with this transformer and property 'ignoreBadInput' is set to true, so the transformer chain will continue.");
            return src;
        } else {
            I18nMessage msg = CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint(getName(), src.getClass());
            throw new MessageTransformerException(msg, this, event.getMessage());
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug(format("Applying transformer %s (%s)", getName(), getClass().getName()));
        logger.debug(format("Object before transform: %s", StringMessageUtils.toString(src)));
    }
    Message message;
    if (src instanceof Message) {
        message = (Message) src;
    } else // TODO MULE-9342 Clean up transformer vs message transformer confusion
    if (src instanceof CoreEvent) {
        event = (CoreEvent) src;
        message = event.getMessage();
    } else if (muleContext.getConfiguration().isAutoWrapMessageAwareTransform()) {
        message = of(src);
    } else {
        if (event == null) {
            throw new MessageTransformerException(CoreMessages.noCurrentEventForTransformer(), this, null);
        }
        message = event.getMessage();
    }
    Object result;
    // TODO MULE-9342 Clean up transformer vs message transformer confusion
    if (event == null) {
        MuleClientFlowConstruct flowConstruct = new MuleClientFlowConstruct(muleContext);
        ComponentLocation location = getLocation() != null ? getLocation() : fromSingleComponent("AbstractMessageTransformer");
        event = InternalEvent.builder(create(flowConstruct, location)).message(message).build();
    }
    result = transformMessage(event, enc);
    if (logger.isDebugEnabled()) {
        logger.debug(format("Object after transform: %s", StringMessageUtils.toString(result)));
    }
    result = checkReturnClass(result, event);
    return result;
}
Also used : ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) MuleClientFlowConstruct(org.mule.runtime.core.privileged.client.MuleClientFlowConstruct) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) I18nMessage(org.mule.runtime.api.i18n.I18nMessage) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) I18nMessage(org.mule.runtime.api.i18n.I18nMessage)

Example 82 with DataType

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

the class StreamingUtils method updateTypedValueForStreaming.

/**
 * Updates the value a given {@link TypedValue} instance by replacing it with a {@link CursorProvider}.
 *
 * @param value            the typed value to update
 * @param event            the current event
 * @param streamingManager the streaming manager
 * @return updated {@link TypedValue instance}
 */
public static TypedValue updateTypedValueForStreaming(final TypedValue value, final CoreEvent event, final StreamingManager streamingManager) {
    if (event == null) {
        return value;
    } else {
        Object payload = value.getValue();
        if (payload instanceof CursorProvider) {
            CursorProvider cursorProvider = streamingManager.manage((CursorProvider) payload, event);
            DataType dataType = DataType.builder(value.getDataType()).type(cursorProvider.getClass()).build();
            return new TypedValue<>(cursorProvider, dataType, value.getByteLength());
        }
        return value;
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 83 with DataType

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

the class StreamingUtils method updateTypedValueWithCursorProvider.

/**
 * Updates the {@link Cursor} value a given {@link TypedValue} instance by replacing it with a {@link CursorProvider}.
 *
 * @param value the typed value to update
 * @param event the current event
 * @param streamingManager the streaming manager
 * @return updated {@link TypedValue instance}
 */
public static TypedValue updateTypedValueWithCursorProvider(final TypedValue value, final CoreEvent event, final StreamingManager streamingManager) {
    if (event == null) {
        return value;
    } else {
        Object payload = value.getValue();
        if (payload instanceof CursorStream) {
            CursorProvider provider = ((CursorStream) value.getValue()).getProvider();
            DataType dataType = DataType.builder(value.getDataType()).type(provider.getClass()).build();
            return new TypedValue(provider, dataType, value.getByteLength());
        } else {
            return value;
        }
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) DataType(org.mule.runtime.api.metadata.DataType) CollectionDataType(org.mule.runtime.api.metadata.CollectionDataType) CursorStream(org.mule.runtime.api.streaming.bytes.CursorStream) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 84 with DataType

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

the class GZipUncompressTransformer method doTransform.

@Override
public Object doTransform(Object src, Charset outputEncoding) throws TransformerException {
    try {
        if (src instanceof CursorStreamProvider) {
            return getStrategy().uncompressInputStream(((CursorStreamProvider) src).openCursor());
        }
        if (src instanceof InputStream) {
            return getStrategy().uncompressInputStream((InputStream) src);
        } else {
            byte[] buffer = getStrategy().uncompressByteArray((byte[]) src);
            DataType returnDataType = getReturnDataType();
            // If a return type has been specified, then deserialize the uncompressed byte array.
            if (DataType.STRING.isCompatibleWith(returnDataType)) {
                return new String(buffer, outputEncoding);
            } else if (!DataType.OBJECT.isCompatibleWith(returnDataType) && !DataType.BYTE_ARRAY.isCompatibleWith(returnDataType)) {
                try {
                    return muleContext.getObjectSerializer().getExternalProtocol().deserialize(buffer);
                } catch (SerializationException e) {
                    throw new TransformerException(this, e);
                }
            } else {
                // First try to deserialize the byte array. If it can be deserialized, then it was originally serialized.
                try {
                    return muleContext.getObjectSerializer().getExternalProtocol().deserialize(buffer);
                } catch (SerializationException e) {
                    // If it fails, ignore it. We assume it was not serialized in the first place and return the buffer as it is.
                    return buffer;
                }
            }
        }
    } catch (IOException e) {
        throw new TransformerException(I18nMessageFactory.createStaticMessage("Failed to uncompress message."), this, e);
    }
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) SerializationException(org.mule.runtime.api.serialization.SerializationException) InputStream(java.io.InputStream) DataType(org.mule.runtime.api.metadata.DataType) IOException(java.io.IOException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 85 with DataType

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

the class TransformationGraphLookupStrategy method findTransformationPaths.

private List<List<TransformationEdge>> findTransformationPaths(DataType source, DataType target, Set<DataType> visited) {
    List<List<TransformationEdge>> validTransformationEdges = new LinkedList<>();
    if (visited.contains(source)) {
        return validTransformationEdges;
    } else {
        visited.add(source);
        Set<TransformationEdge> transformationEdges = graph.outgoingEdgesOf(source);
        for (TransformationEdge transformationEdge : transformationEdges) {
            DataType edgeTarget = graph.getEdgeTarget(transformationEdge);
            if (target.isCompatibleWith(edgeTarget)) {
                LinkedList<TransformationEdge> transformationEdges1 = new LinkedList<>();
                transformationEdges1.add(transformationEdge);
                validTransformationEdges.add(transformationEdges1);
            } else {
                List<List<TransformationEdge>> newTransformations = findTransformationPaths(edgeTarget, target, visited);
                for (List<TransformationEdge> transformationEdgeList : newTransformations) {
                    transformationEdgeList.add(0, transformationEdge);
                    validTransformationEdges.add(transformationEdgeList);
                }
            }
        }
        visited.remove(source);
    }
    return validTransformationEdges;
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) List(java.util.List) LinkedList(java.util.LinkedList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

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