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;
}
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;
}
}
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;
}
}
}
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);
}
}
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;
}
Aggregations