Search in sources :

Example 6 with MediaType

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

the class AbstractReturnDelegate method resolveMediaType.

/**
 * If provided, mimeType and encoding configured as operation parameters will take precedence over what comes with the message's
 * {@link DataType}.
 *
 * @param value
 * @param operationContext
 * @return
 */
protected MediaType resolveMediaType(Object value, ExecutionContextAdapter<ComponentModel> operationContext) {
    Charset existingEncoding = getDefaultEncoding(muleContext);
    MediaType mediaType = defaultMediaType;
    if (value instanceof Result) {
        final Optional<MediaType> optionalMediaType = ((Result) value).getMediaType();
        if (optionalMediaType.isPresent()) {
            mediaType = optionalMediaType.get();
            if (mediaType.getCharset().isPresent()) {
                existingEncoding = mediaType.getCharset().get();
            }
        }
    }
    if (mediaType == null) {
        mediaType = ANY;
    }
    if (operationContext.hasParameter(MIME_TYPE_PARAMETER_NAME)) {
        mediaType = MediaType.parse(operationContext.getParameter(MIME_TYPE_PARAMETER_NAME));
    }
    if (operationContext.hasParameter(ENCODING_PARAMETER_NAME)) {
        mediaType = mediaType.withCharset(parseCharset(operationContext.getParameter(ENCODING_PARAMETER_NAME)));
    } else {
        mediaType = mediaType.withCharset(existingEncoding);
    }
    return mediaType;
}
Also used : Charset(java.nio.charset.Charset) MediaTypeUtils.parseCharset(org.mule.runtime.api.metadata.MediaTypeUtils.parseCharset) MediaType(org.mule.runtime.api.metadata.MediaType) Result(org.mule.runtime.extension.api.runtime.operation.Result)

Example 7 with MediaType

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

the class OperationMessageProcessorTestCase method operationReturnsOperationResultOnTarget.

@Test
public void operationReturnsOperationResultOnTarget() throws Exception {
    target = TARGET_VAR;
    messageProcessor = setUpOperationMessageProcessor();
    Object payload = new Object();
    MediaType mediaType = ANY.withCharset(getDefaultEncoding(context));
    Object attributes = mock(Object.class);
    when(operationExecutor.execute(any(ExecutionContext.class))).thenReturn(just(builder().output(payload).mediaType(mediaType).attributes(attributes).build()));
    Message message = (Message) messageProcessor.process(event).getVariables().get(TARGET_VAR).getValue();
    assertThat(message, is(notNullValue()));
    assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
    assertThat(message.getAttributes().getValue(), is(sameInstance(attributes)));
    assertThat(message.getPayload().getDataType().getMediaType(), equalTo(mediaType));
}
Also used : ExecutionContext(org.mule.runtime.extension.api.runtime.operation.ExecutionContext) Message(org.mule.runtime.api.message.Message) MediaType(org.mule.runtime.api.metadata.MediaType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 8 with MediaType

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

the class TargetOutputMessageReturnDelegateTestCase method operationTargetPayloadWithResult.

@Test
public void operationTargetPayloadWithResult() {
    delegate = createDelegate("#[payload]");
    MediaType mediaType = APPLICATION_JSON.withCharset(Charset.defaultCharset());
    CoreEvent result = delegate.asReturnValue(Result.builder().output(payload).mediaType(mediaType).build(), operationContext);
    assertMessage(result.getMessage());
    assertThat(result.getVariables().get(TARGET).getValue(), is(payload));
    assertThat(result.getVariables().get(TARGET).getDataType().getMediaType(), is(mediaType));
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MediaType(org.mule.runtime.api.metadata.MediaType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 9 with MediaType

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

the class ValueReturnDelegateContractTestCase method operationReturnsOperationResultButKeepsAttributes.

@Test
public void operationReturnsOperationResultButKeepsAttributes() throws Exception {
    Object payload = new Object();
    MediaType mediaType = ANY.withCharset(getDefaultEncoding(muleContext));
    CoreEvent result = delegate.asReturnValue(Result.builder().output(payload).mediaType(mediaType).build(), operationContext);
    Message message = getOutputMessage(result);
    assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
    assertThat(message.getAttributes().getValue(), is(nullValue()));
    assertThat(message.getPayload().getDataType().getMediaType(), equalTo(mediaType));
}
Also used : Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MediaType(org.mule.runtime.api.metadata.MediaType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 10 with MediaType

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

the class AbstractReturnDelegate method toMessage.

protected Message toMessage(Object value, ExecutionContextAdapter operationContext) {
    final MediaType mediaType = resolveMediaType(value, operationContext);
    final CoreEvent event = operationContext.getEvent();
    if (value instanceof Result) {
        Result resultValue = (Result) value;
        return isSpecialHandling && returnHandler.handles(resultValue.getOutput()) ? MessageUtils.toMessage((Result) value, mediaType, cursorProviderFactory, event, returnHandler.getDataType()) : MessageUtils.toMessage((Result) value, mediaType, cursorProviderFactory, event);
    } else {
        if (value instanceof Collection && returnsListOfMessages) {
            value = toMessageCollection((Collection<Result>) value, cursorProviderFactory, event);
        } else if (value instanceof Iterator && returnsListOfMessages) {
            value = toMessageIterator((Iterator<Result>) value, cursorProviderFactory, event);
        }
        value = streamingContent(value, cursorProviderFactory, event);
        Message.Builder messageBuilder;
        // org.mule.runtime.api.metadata.DataType.MULE_MESSAGE_COLLECTION doesn't completely makes sense
        if (returnsListOfMessages && value instanceof Collection) {
            messageBuilder = Message.builder().collectionValue((Collection) value, Message.class);
        } else if (isSpecialHandling && returnHandler.handles(value)) {
            messageBuilder = returnHandler.toMessageBuilder(value);
        } else {
            messageBuilder = Message.builder().value(value);
        }
        return messageBuilder.mediaType(mediaType).build();
    }
}
Also used : Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Iterator(java.util.Iterator) MessageUtils.toMessageIterator(org.mule.runtime.core.internal.util.message.MessageUtils.toMessageIterator) MediaType(org.mule.runtime.api.metadata.MediaType) MessageUtils.toMessageCollection(org.mule.runtime.core.internal.util.message.MessageUtils.toMessageCollection) Collection(java.util.Collection) ExtensionMetadataTypeUtils.isJavaCollection(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isJavaCollection) Result(org.mule.runtime.extension.api.runtime.operation.Result)

Aggregations

MediaType (org.mule.runtime.api.metadata.MediaType)13 Test (org.junit.Test)10 SmallTest (org.mule.tck.size.SmallTest)10 Message (org.mule.runtime.api.message.Message)5 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)3 ExecutionContext (org.mule.runtime.extension.api.runtime.operation.ExecutionContext)3 Charset (java.nio.charset.Charset)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Result (org.mule.runtime.extension.api.runtime.operation.Result)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 DataType (org.mule.runtime.api.metadata.DataType)1 MediaTypeUtils.parseCharset (org.mule.runtime.api.metadata.MediaTypeUtils.parseCharset)1 MessageUtils.toMessageCollection (org.mule.runtime.core.internal.util.message.MessageUtils.toMessageCollection)1 MessageUtils.toMessageIterator (org.mule.runtime.core.internal.util.message.MessageUtils.toMessageIterator)1 ExtensionMetadataTypeUtils.isJavaCollection (org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isJavaCollection)1