use of org.mule.runtime.core.api.transformer.MessageTransformerException in project mule by mulesoft.
the class TransformerChain method transformMessage.
@Override
public Object transformMessage(CoreEvent event, Charset outputEncoding) throws MessageTransformerException {
Message result = event.getMessage();
Object temp = event.getMessage();
Transformer lastTransformer = null;
for (Object element : transformers) {
lastTransformer = (Transformer) element;
try {
temp = lastTransformer.transform(temp);
} catch (TransformerException e) {
throw new MessageTransformerException(lastTransformer, e, event.getMessage());
}
if (temp instanceof Message) {
result = (Message) temp;
} else {
result = Message.builder(event.getMessage()).value(temp).build();
event = CoreEvent.builder(event).message(result).build();
}
}
if (lastTransformer != null && Message.class.isAssignableFrom(lastTransformer.getReturnDataType().getType())) {
return result;
} else {
return result.getPayload().getValue();
}
}
use of org.mule.runtime.core.api.transformer.MessageTransformerException 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.MessageTransformerException in project mule by mulesoft.
the class DefaultTransformationService method checkResultDataType.
protected void checkResultDataType(Message message, DataType resultType, Object value) throws MessageTransformerException {
if (value != null && !resultType.getType().isAssignableFrom(value.getClass())) {
TypedValue<Object> actualType = TypedValue.of(value);
Message transformedMessage = Message.builder(message).payload(actualType).build();
throw new MessageTransformerException(transformOnObjectNotOfSpecifiedType(resultType, actualType), null, transformedMessage);
}
}
use of org.mule.runtime.core.api.transformer.MessageTransformerException in project mule by mulesoft.
the class ExpressionTransformer method transformMessage.
@Override
public Object transformMessage(CoreEvent event, Charset outputEncoding) throws MessageTransformerException {
Object[] results = new Object[arguments.size()];
int i = 0;
for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext(); i++) {
ExpressionArgument argument = iterator.next();
try {
results[i] = argument.evaluate(event);
} catch (ExpressionRuntimeException e) {
throw new MessageTransformerException(this, e, event.getMessage());
}
if (!argument.isOptional() && results[i] == null) {
throw new MessageTransformerException(expressionReturnedNull(argument.getExpression()), this, event.getMessage());
}
}
if (isReturnSourceIfNull() && checkIfAllAreNull(results)) {
return event.getMessage();
}
if (results.length == 1) {
return results[0];
} else {
return results;
}
}
use of org.mule.runtime.core.api.transformer.MessageTransformerException in project mule by mulesoft.
the class SoapOperationExecutor method execute.
/**
* {@inheritDoc}
*/
@Override
public Publisher<Object> execute(ExecutionContext<OperationModel> context) {
try {
String serviceId = context.getParameter(SERVICE_PARAM);
ForwardingSoapClient connection = (ForwardingSoapClient) connectionResolver.resolve(context).get();
Map<String, String> customHeaders = connection.getCustomHeaders(serviceId, getOperation(context));
SoapRequest request = getRequest(context, customHeaders);
SoapClient soapClient = connection.getSoapClient(serviceId);
SoapResponse response = connection.getExtensionsClientDispatcher(() -> new ExtensionsClientArgumentResolver(registry, policyManager).resolve(context).get()).map(d -> soapClient.consume(request, d)).orElseGet(() -> soapClient.consume(request));
return justOrEmpty(response.getAsResult(streamingHelperArgumentResolver.resolve(context).get()));
} catch (MessageTransformerException | TransformerException e) {
return error(e);
} catch (Exception e) {
return error(soapExceptionEnricher.enrich(e));
} catch (Throwable t) {
return error(wrapFatal(t));
}
}
Aggregations