Search in sources :

Example 1 with MessageTransformerException

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();
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) AbstractMessageTransformer(org.mule.runtime.core.api.transformer.AbstractMessageTransformer) Message(org.mule.runtime.api.message.Message) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 2 with MessageTransformerException

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);
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) DataType(org.mule.runtime.api.metadata.DataType) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 3 with MessageTransformerException

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);
    }
}
Also used : I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Message(org.mule.runtime.api.message.Message) CoreMessages.noTransformerFoundForMessage(org.mule.runtime.core.api.config.i18n.CoreMessages.noTransformerFoundForMessage) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException)

Example 4 with MessageTransformerException

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;
    }
}
Also used : ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException)

Example 5 with MessageTransformerException

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));
    }
}
Also used : HEADERS_PARAM(org.mule.runtime.module.extension.soap.internal.loader.SoapInvokeOperationDeclarer.HEADERS_PARAM) Exceptions.wrapFatal(org.mule.runtime.core.api.rx.Exceptions.wrapFatal) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) BindingContext(org.mule.runtime.api.el.BindingContext) MuleExpressionLanguage(org.mule.runtime.api.el.MuleExpressionLanguage) ComponentExecutor(org.mule.runtime.extension.api.runtime.operation.ComponentExecutor) ATTACHMENTS_PARAM(org.mule.runtime.module.extension.soap.internal.loader.SoapInvokeOperationDeclarer.ATTACHMENTS_PARAM) HashMap(java.util.HashMap) SoapRequestBuilder(org.mule.runtime.soap.api.message.SoapRequestBuilder) Inject(javax.inject.Inject) ExtensionsClientArgumentResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ExtensionsClientArgumentResolver) ConnectionArgumentResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ConnectionArgumentResolver) MESSAGE_GROUP(org.mule.runtime.module.extension.soap.internal.loader.SoapInvokeOperationDeclarer.MESSAGE_GROUP) SoapClient(org.mule.runtime.soap.api.client.SoapClient) Map(java.util.Map) SoapResponse(org.mule.runtime.soap.api.message.SoapResponse) Registry(org.mule.runtime.api.artifact.Registry) IOUtils(org.mule.runtime.core.api.util.IOUtils) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) Mono.error(reactor.core.publisher.Mono.error) INPUT_STREAM(org.mule.runtime.api.metadata.DataType.INPUT_STREAM) BODY_PARAM(org.mule.runtime.module.extension.soap.internal.loader.SoapInvokeOperationDeclarer.BODY_PARAM) TransformationService(org.mule.runtime.api.transformation.TransformationService) ExecutionContext(org.mule.runtime.extension.api.runtime.operation.ExecutionContext) OPERATION_PARAM(org.mule.runtime.module.extension.soap.internal.loader.SoapInvokeOperationDeclarer.OPERATION_PARAM) DataType(org.mule.runtime.api.metadata.DataType) Publisher(org.reactivestreams.Publisher) SoapRequest(org.mule.runtime.soap.api.message.SoapRequest) Mono.justOrEmpty(reactor.core.publisher.Mono.justOrEmpty) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) SoapAttachment(org.mule.runtime.extension.api.soap.SoapAttachment) SERVICE_PARAM(org.mule.runtime.module.extension.soap.internal.loader.SoapInvokeOperationDeclarer.SERVICE_PARAM) TypedValue(org.mule.runtime.api.metadata.TypedValue) TRANSPORT_HEADERS_PARAM(org.mule.runtime.module.extension.soap.internal.loader.SoapInvokeOperationDeclarer.TRANSPORT_HEADERS_PARAM) StreamingHelperArgumentResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StreamingHelperArgumentResolver) ForwardingSoapClient(org.mule.runtime.module.extension.soap.internal.runtime.connection.ForwardingSoapClient) ExtensionsClient(org.mule.runtime.extension.api.client.ExtensionsClient) Optional(java.util.Optional) SoapExceptionEnricher(org.mule.runtime.soap.api.exception.error.SoapExceptionEnricher) InputStream(java.io.InputStream) SoapClient(org.mule.runtime.soap.api.client.SoapClient) ForwardingSoapClient(org.mule.runtime.module.extension.soap.internal.runtime.connection.ForwardingSoapClient) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) ForwardingSoapClient(org.mule.runtime.module.extension.soap.internal.runtime.connection.ForwardingSoapClient) SoapRequest(org.mule.runtime.soap.api.message.SoapRequest) SoapResponse(org.mule.runtime.soap.api.message.SoapResponse) ExtensionsClientArgumentResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ExtensionsClientArgumentResolver) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Aggregations

MessageTransformerException (org.mule.runtime.core.api.transformer.MessageTransformerException)6 Message (org.mule.runtime.api.message.Message)3 DataType (org.mule.runtime.api.metadata.DataType)3 Transformer (org.mule.runtime.core.api.transformer.Transformer)3 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1 Test (org.junit.Test)1 Registry (org.mule.runtime.api.artifact.Registry)1 BindingContext (org.mule.runtime.api.el.BindingContext)1 MuleExpressionLanguage (org.mule.runtime.api.el.MuleExpressionLanguage)1 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)1 OperationModel (org.mule.runtime.api.meta.model.operation.OperationModel)1 INPUT_STREAM (org.mule.runtime.api.metadata.DataType.INPUT_STREAM)1 TypedValue (org.mule.runtime.api.metadata.TypedValue)1 TransformationService (org.mule.runtime.api.transformation.TransformationService)1