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;
}
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));
}
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));
}
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));
}
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();
}
}
Aggregations