protected MediaType resolveMediaType(Object value) { Charset existingEncoding = defaultEncoding; MediaType mediaType = defaultMediaType; if (value instanceof Result) { final Optional<MediaType> optionalMediaType = ((Result) value).getMediaType(); if (optionalMediaType.isPresent()) { mediaType = optionalMediaType.get(); existingEncoding = mediaType.getCharset().orElse(existingEncoding); } } return mimeTypeInitParam.orElse(mediaType).withCharset(encodingParam.orElse(existingEncoding)); }
/** * 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, Optional<MediaType> contextMimeType, Optional<Charset> contextEncoding) { Charset existingEncoding = defaultEncoding; 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().orElse(existingEncoding); } } } return contextMimeType.orElse(mediaType).withCharset(contextEncoding.orElse(existingEncoding)); }
@Test public void updatesMimeTypeOnTransformation() throws Exception { Message message = Message.builder().value(TEST_PAYLOAD).mediaType(ANY.withCharset(CUSTOM_ENCODING)).build(); Transformer transformer = mock(Transformer.class); when(transformer.isSourceDataTypeSupported(any())).thenReturn(true); DataType outputDataType = DataType.builder().type(Integer.class).mediaType(APPLICATION_XML).build(); when(transformer.getReturnDataType()).thenReturn(outputDataType); when(transformer.transform(any(Message.class))).thenReturn(1); CoreEvent muleEvent = mock(CoreEvent.class); Message result = transformationService.applyTransformers(message, muleEvent, singletonList(transformer)); assertDataType(result, Integer.class, APPLICATION_XML, CUSTOM_ENCODING); }
@Test public void operationReturnsOperationResultWhichKeepsNoValues() throws Exception { 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 = messageProcessor.process(event).getMessage(); assertThat(message, is(notNullValue())); assertThat(message.getPayload().getValue(), is(sameInstance(payload))); assertThat(message.getAttributes().getValue(), is(sameInstance(attributes))); assertThat(message.getPayload().getDataType().getMediaType(), is(mediaType)); }
@Test public void usesCustomEncodingWithNoProperty() throws Exception { Message message = Message.builder().value(TEST_PAYLOAD).mediaType(ANY.withCharset(CUSTOM_ENCODING)).build(); assertThat(message.getPayload().getDataType().getMediaType().getCharset().get(), equalTo(CUSTOM_ENCODING)); assertCustomEncoding(message); }
@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)); }
@Test public void operationReturnsCollectionWithCorrectDataType() throws Exception { Object payload = new ArrayList<>(); setUpOperationReturning(payload, new TypeToken<List<Integer>>() {}.getType()); Message message = messageProcessor.process(event).getMessage(); assertThat(message, is(notNullValue())); assertThat(message.getPayload().getValue(), is(sameInstance(payload))); DataType dataType = message.getPayload().getDataType(); assertThat(dataType, instanceOf(CollectionDataType.class)); assertThat(((CollectionDataType) dataType).getItemDataType(), like(Integer.class, ANY.withCharset(null))); }
@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)); }
@Test public void operationReturnsOperationResultButKeepsAttributes() throws Exception { Object payload = new Object(); MediaType mediaType = ANY.withCharset(getDefaultEncoding(context)); when(operationExecutor.execute(any(ExecutionContext.class))) .thenReturn(just(builder().output(payload).mediaType(mediaType).build())); event = CoreEvent.builder(event).message(Message.builder().value("").attributesValue(mock(Object.class)).build()).build(); Message message = messageProcessor.process(event).getMessage(); assertThat(message, is(notNullValue())); assertThat(message.getPayload().getValue(), is(sameInstance(payload))); assertThat(message.getAttributes().getValue(), is(nullValue())); assertThat(message.getPayload().getDataType().getMediaType(), equalTo(mediaType)); }
@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)); }
@Test public void operationReturnsResultCollectionWithCorrectDataType() throws Exception { Object payload = new ArrayList<>(); setUpOperationReturning(Result.builder().output(payload).build(), new TypeToken<List<Integer>>() {}.getType()); Message message = messageProcessor.process(event).getMessage(); assertThat(message, is(notNullValue())); assertThat(message.getPayload().getValue(), is(sameInstance(payload))); DataType dataType = message.getPayload().getDataType(); assertThat(dataType, instanceOf(CollectionDataType.class)); assertThat(((CollectionDataType) dataType).getItemDataType(), like(Integer.class, ANY.withCharset(null))); }
@Test public void operationReturnsMapWithCorrectDataType() throws Exception { Object payload = new HashMap<>(); setUpOperationReturning(payload, new TypeToken<Map<String, String>>() {}.getType()); Message message = messageProcessor.process(event).getMessage(); assertThat(message, is(notNullValue())); assertThat(message.getPayload().getValue(), is(sameInstance(payload))); DataType dataType = message.getPayload().getDataType(); assertThat(dataType, instanceOf(MapDataType.class)); assertThat(((MapDataType) dataType).getKeyDataType(), like(String.class, ANY.withCharset(null))); assertThat(((MapDataType) dataType).getValueDataType(), like(String.class, ANY)); }
@Test public void operationReturnsResultMapWithCorrectDataType() throws Exception { Object payload = new HashMap<>(); setUpOperationReturning(Result.builder().output(payload).build(), new TypeToken<Map<String, String>>() {}.getType()); Message message = messageProcessor.process(event).getMessage(); assertThat(message, is(notNullValue())); assertThat(message.getPayload().getValue(), is(sameInstance(payload))); DataType dataType = message.getPayload().getDataType(); assertThat(dataType, instanceOf(MapDataType.class)); assertThat(((MapDataType) dataType).getKeyDataType(), like(String.class, ANY.withCharset(null))); assertThat(((MapDataType) dataType).getValueDataType(), like(String.class, ANY)); }
@Override public DataTypeBuilder charset(Charset charset) { validateAlreadyBuilt(); mediaType = mediaType.withCharset(charset); return this; }
/** * Sets the given charset. See {@link MediaType#getCharset()}. * * @param charset the encoding to set. * @return this builder. */ @Override public DataTypeBuilder charset(String charset) { validateAlreadyBuilt(); if (!isEmpty(charset)) { mediaType = mediaType.withCharset(Charset.forName(charset)); } else { mediaType = mediaType.withCharset(null); } return this; }
mediaType = mediaType.withCharset(parseCharset(operationContext.getParameter(ENCODING_PARAMETER_NAME))); } else { mediaType = mediaType.withCharset(existingEncoding);
@Test public void updatesMimeTypeOnTransformation() throws Exception { Message message = Message.builder().value(TEST_PAYLOAD).mediaType(ANY.withCharset(CUSTOM_ENCODING)).build(); Transformer transformer = mock(Transformer.class); when(transformer.isSourceDataTypeSupported(any())).thenReturn(true); DataType outputDataType = DataType.builder().type(Integer.class).mediaType(APPLICATION_XML).build(); when(transformer.getReturnDataType()).thenReturn(outputDataType); when(transformer.transform(anyString())).thenReturn(1); CoreEvent muleEvent = mock(CoreEvent.class); Message result = transformationService.applyTransformers(message, muleEvent, singletonList(transformer)); assertDataType(result, Integer.class, APPLICATION_XML, CUSTOM_ENCODING); }
@Test public void usesCustomEncodingWithNoProperty() throws Exception { Message message = Message.builder().value(TEST_PAYLOAD).mediaType(ANY.withCharset(CUSTOM_ENCODING)).build(); assertThat(message.getPayload().getDataType().getMediaType().getCharset().get(), equalTo(CUSTOM_ENCODING)); assertCustomEncoding(message); }
MediaType mediaType = MediaType.parse(mockedEventPayload.getMediaType()); if (mockedEventPayload.getEncoding() != null) { mediaType = mediaType.withCharset(Charset.forName(mockedEventPayload.getEncoding()));