Search in sources :

Example 41 with TypedValue

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

the class SoapOperationExecutor method getRequest.

/**
 * Builds a Soap Request with the execution context to be sent using the {@link SoapClient}.
 */
private SoapRequest getRequest(ExecutionContext<OperationModel> context, Map<String, String> fixedHeaders) throws MessageTransformerException, TransformerException {
    SoapRequestBuilder builder = SoapRequest.builder().operation(getOperation(context));
    builder.soapHeaders(fixedHeaders);
    Optional<Object> optionalMessageGroup = getParam(context, MESSAGE_GROUP);
    if (optionalMessageGroup.isPresent()) {
        Map<String, Object> message = (Map<String, Object>) optionalMessageGroup.get();
        InputStream body = (InputStream) message.get(BODY_PARAM);
        if (body != null) {
            builder.content(body);
        }
        InputStream headers = (InputStream) message.get(HEADERS_PARAM);
        if (headers != null) {
            builder.soapHeaders((Map<String, String>) evaluateHeaders(headers));
        }
        Map<String, TypedValue<?>> attachments = (Map<String, TypedValue<?>>) message.get(ATTACHMENTS_PARAM);
        if (attachments != null) {
            toSoapAttachments(attachments).forEach(builder::attachment);
        }
    }
    getParam(context, TRANSPORT_HEADERS_PARAM).ifPresent(th -> builder.transportHeaders((Map<String, String>) th));
    return builder.build();
}
Also used : InputStream(java.io.InputStream) SoapRequestBuilder(org.mule.runtime.soap.api.message.SoapRequestBuilder) HashMap(java.util.HashMap) Map(java.util.Map) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 42 with TypedValue

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

the class ReflectiveExpressionFunctionExecutor method getWrapper.

private Function<Object, Object> getWrapper(Parameter parameter) {
    Type[] generics = parameter.getType().getGenericInterfaces();
    Class<?> type;
    if (generics.length == 0) {
        type = Object.class;
    } else {
        type = model.getAllParameterModels().stream().filter(p -> p.getModelProperty(ImplementingParameterModelProperty.class).map(mp -> mp.getParameter().getName().equals(parameter.getName())).orElse(false)).map(p -> getType(p.getType()).orElse(Object.class)).findFirst().orElseThrow(() -> new IllegalStateException(format("Missing parameter with name [%s]", parameter.getName())));
    }
    DataType expected = DataType.fromType(type);
    return value -> {
        if (value == null) {
            return null;
        }
        if (value instanceof TypedValue) {
            if (((TypedValue) value).getDataType().equals(DataType.TYPED_VALUE)) {
                // DW will wrap anything of type TypedValue in a new TypedValue with DataType.TYPED_VALUE
                value = ((TypedValue) value).getValue();
            }
            TypedValue typedValue = (TypedValue) value;
            // We have to check for transformations of the value because weave won't be able to validate types
            return type.isInstance(typedValue.getValue()) ? value : new TypedValue<>(transformationService.transform(typedValue.getValue(), typedValue.getDataType(), expected), typedValue.getDataType());
        } else {
            return new TypedValue<>(value, fromObject(value));
        }
    };
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) BindingContext(org.mule.runtime.api.el.BindingContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ImplementingParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingParameterModelProperty) DataType.fromObject(org.mule.runtime.api.metadata.DataType.fromObject) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) Function(java.util.function.Function) ReflectionUtils.invokeMethod(org.springframework.util.ReflectionUtils.invokeMethod) ClassUtils(org.mule.runtime.core.api.util.ClassUtils) Inject(javax.inject.Inject) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) Parameter(java.lang.reflect.Parameter) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) Method(java.lang.reflect.Method) TransformationService(org.mule.runtime.api.transformation.TransformationService) ExecutionContext(org.mule.runtime.extension.api.runtime.operation.ExecutionContext) Logger(org.slf4j.Logger) DataType(org.mule.runtime.api.metadata.DataType) Optional.ofNullable(java.util.Optional.ofNullable) LifecycleUtils.startIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.startIfNeeded) String.format(java.lang.String.format) LifecycleUtils.disposeIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.disposeIfNeeded) TypedValue(org.mule.runtime.api.metadata.TypedValue) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) Lifecycle(org.mule.runtime.api.lifecycle.Lifecycle) List(java.util.List) Type(java.lang.reflect.Type) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) DataType(org.mule.runtime.api.metadata.DataType) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) Type(java.lang.reflect.Type) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) DataType(org.mule.runtime.api.metadata.DataType) ImplementingParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingParameterModelProperty) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 43 with TypedValue

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

the class ExpressionTypedValueValueResolver method resolve.

@Override
public TypedValue<T> resolve(ValueResolvingContext context) throws MuleException {
    initEvaluator();
    TypedValue typedValue = evaluator.resolveTypedValue(context.getEvent());
    if (!isInstance(expectedClass, typedValue.getValue())) {
        DataType expectedDataType = DataType.builder().type(expectedClass).mediaType(typedValue.getDataType().getMediaType()).build();
        return new TypedValue<>(typeSafeTransformer.<T>transform(typedValue.getValue(), typedValue.getDataType(), expectedDataType), expectedDataType);
    }
    return typedValue;
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 44 with TypedValue

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

the class ModuleJsonCustomTypeTestCase method testExtractingJsonResponseAndFeedingSimpleType.

@Test
public void testExtractingJsonResponseAndFeedingSimpleType() throws Exception {
    final CoreEvent muleEvent = flowRunner("testExtractingJsonResponseAndFeedingSimpleType").run();
    final Map<String, TypedValue<?>> variables = muleEvent.getVariables();
    assertThat(variables.get("checkingNotAvenue").getValue(), is(false));
    assertThat(variables.get("checkingFromExpression").getValue(), is(true));
    assertThat(variables.get("checkingFromHardcodedType1").getValue(), is(true));
    assertThat(variables.get("checkingFromHardcodedType1WithVariables").getValue(), is(true));
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 45 with TypedValue

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

the class TestEventBuilder method build.

/**
 * Produces an event with the specified configuration.
 *
 * @param flow the recipient for the event to be built.
 * @return an event with the specified configuration.
 */
public CoreEvent build(FlowConstruct flow) {
    final Message.Builder messageBuilder;
    messageBuilder = Message.builder().value(payload).mediaType(mediaType);
    setInboundProperties(messageBuilder, inboundProperties);
    setOutboundProperties(messageBuilder, outboundProperties);
    if (attributes != null) {
        messageBuilder.attributesValue(attributes);
    }
    final Message muleMessage = messageBuilder.build();
    EventContext eventContext;
    if (externalCompletionCallback != null) {
        eventContext = create(flow, TEST_CONNECTOR_LOCATION, sourceCorrelationId, of(externalCompletionCallback));
    } else {
        eventContext = create(flow, TEST_CONNECTOR_LOCATION, sourceCorrelationId);
    }
    CoreEvent.Builder builder = InternalEvent.builder(eventContext).message(spyMessage.apply(muleMessage)).replyToHandler(replyToHandler).itemSequenceInfo(ofNullable(itemSequenceInfo));
    for (Entry<String, TypedValue> variableEntry : variables.entrySet()) {
        builder.addVariable(variableEntry.getKey(), variableEntry.getValue().getValue(), variableEntry.getValue().getDataType());
    }
    CoreEvent event = builder.build();
    for (Entry<String, Attachment> outboundAttachmentEntry : outboundAttachments.entrySet()) {
        event = outboundAttachmentEntry.getValue().addOutboundTo(event, outboundAttachmentEntry.getKey());
    }
    for (Entry<String, Object> sessionPropertyEntry : sessionProperties.entrySet()) {
        ((PrivilegedEvent) event).getSession().setProperty(sessionPropertyEntry.getKey(), sessionPropertyEntry.getValue());
    }
    return spyEvent.apply(event);
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Aggregations

TypedValue (org.mule.runtime.api.metadata.TypedValue)97 Test (org.junit.Test)74 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)47 DataType (org.mule.runtime.api.metadata.DataType)17 Message (org.mule.runtime.api.message.Message)16 Description (io.qameta.allure.Description)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 List (java.util.List)11 SmallTest (org.mule.tck.size.SmallTest)10 BindingContext (org.mule.runtime.api.el.BindingContext)9 Map (java.util.Map)8 Optional (java.util.Optional)8 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)5 ArrayList (java.util.ArrayList)4 Matchers.anyString (org.mockito.Matchers.anyString)4 MuleException (org.mule.runtime.api.exception.MuleException)4 Error (org.mule.runtime.api.message.Error)4 ErrorType (org.mule.runtime.api.message.ErrorType)4