use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class AbstractMockTransformerBuilder method build.
public Transformer build() {
Transformer transformer;
if (name == null || name.isEmpty()) {
transformer = mock(getClassToMock());
} else {
transformer = mock(getClassToMock(), name);
doReturn(name).when(transformer).getName();
}
if (resultDataType != null) {
doReturn(resultDataType).when(transformer).getReturnDataType();
}
if (sourceDataTypes != null) {
doReturn(Arrays.asList(sourceDataTypes)).when(transformer).getSourceDataTypes();
when(transformer.isSourceDataTypeSupported((DataType) argThat(new SupportsSourceDataType()))).thenReturn(true);
}
try {
doReturn(value).when(transformer).transform(Mockito.any(Object.class));
} catch (TransformerException e) {
// Not going to happen during mock setup
}
return transformer;
}
use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class DefaultTransformationService method getPayload.
/**
* Attempts to obtain the payload of this message with the desired Class type. This will try and resolve a transformer that can
* do this transformation. If a transformer cannot be found an exception is thrown. Any transformers added to the registry will
* be checked for compatibility.
*
* @param resultType the desired return type
* @param encoding the encoding to use if required
* @return The converted payload of this message. Note that this method will not alter the payload of this message <b>unless</b>
* the payload is an {@link InputStream} in which case the stream will be read and the payload will become the fully
* read stream.
* @throws MessageTransformerException if a transformer cannot be found or there is an error during transformation of the payload.
*/
@SuppressWarnings("unchecked")
protected <T> T getPayload(Message message, DataType resultType, Charset encoding) throws MessageTransformerException {
// Handle null by ignoring the request
if (resultType == null) {
throw new IllegalArgumentException(objectIsNull("resultType").getMessage());
}
DataType dataType = DataType.builder(resultType).type(message.getPayload().getDataType().getType()).build();
// If no conversion is necessary, just return the payload as-is
if (resultType.isCompatibleWith(dataType)) {
return (T) message.getPayload().getValue();
}
// The transformer to execute on this message
Transformer transformer = null;
try {
transformer = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(dataType, resultType);
if (transformer == null) {
throw new MessageTransformerException(noTransformerFoundForMessage(dataType, resultType), null, message);
}
// Pass in the message itself
Object result = transformer.transform(message, encoding);
// Unless we disallow Object.class as a valid return type we need to do this extra check
checkResultDataType(message, resultType, result);
return (T) result;
} catch (MessageTransformerException e) {
throw e;
} catch (TransformerException e) {
throw new MessageTransformerException(transformer, e, message);
}
}
use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class MuleRegistryHelper method resolveTransformer.
protected Transformer resolveTransformer(DataType source, DataType result) throws TransformerException {
Lock readLock = transformerResolversLock.readLock();
readLock.lock();
try {
for (TransformerResolver resolver : transformerResolvers) {
try {
Transformer trans = resolver.resolve(source, result);
if (trans != null) {
return trans;
}
} catch (ResolverException e) {
throw new TransformerException(CoreMessages.noTransformerFoundForMessage(source, result), e);
}
}
} finally {
readLock.unlock();
}
return null;
}
use of org.mule.runtime.core.api.transformer.TransformerException in project mule by mulesoft.
the class MuleRegistryHelper method lookupTransformer.
/**
* {@inheritDoc}
*/
@Override
public Transformer lookupTransformer(DataType source, DataType result) throws TransformerException {
// To maintain the previous behaviour, we don't want to consider the result mimeType when resolving a transformer
// and only find transformers with a targetType the same as or a super class of the expected one.
// The same could be done for the source but since if the source expected by the transformer is more generic that
// the provided, it will be found.
result = builder(result).mediaType(ANY).charset((Charset) null).build();
final String dataTypePairHash = getDataTypeSourceResultPairHash(source, result);
Transformer cachedTransformer = (Transformer) exactTransformerCache.get(dataTypePairHash);
if (cachedTransformer != null) {
return cachedTransformer;
}
Transformer trans = resolveTransformer(source, result);
if (trans != null) {
Transformer concurrentlyAddedTransformer = (Transformer) exactTransformerCache.putIfAbsent(dataTypePairHash, trans);
if (concurrentlyAddedTransformer != null) {
return concurrentlyAddedTransformer;
} else {
return trans;
}
} else {
throw new TransformerException(CoreMessages.noTransformerFoundForMessage(source, result));
}
}
use of org.mule.runtime.core.api.transformer.TransformerException 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);
}
}
Aggregations