@Test public void parseTemplateWithOverriddenDataTypeAsExpression() throws Exception { String customEncoding = "UTF-16"; MediaType customMediaType = create("application", "lrmextension"); parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.setOutputMimeType(customMediaType.toRfcString()); parseTemplateProcessor.setOutputEncoding(customEncoding); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getPrimaryType(), is(equalTo(customMediaType.getPrimaryType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getSubType(), is(equalTo(customMediaType.getSubType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getCharset().get().toString(), is(equalTo(customEncoding))); }
private Optional<MediaType> getContextMimeType(Map<String, Object> params) { return ofNullable((String) params.get(MIME_TYPE_PARAMETER_NAME)).map(mimeType -> MediaType.parse(mimeType)); }
@Override protected boolean matchesSafely(DataType dataType) { boolean sameType = type == null ? dataType.getType() == null : type.equals(dataType.getType()); boolean sameEncoding = dataType.getMediaType().getCharset().equals(Optional.ofNullable(encoding)); boolean sameMimeType = mimeType == null ? dataType.getMediaType() == null : mimeType.matches(dataType.getMediaType()); return sameType && sameEncoding && sameMimeType; }
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)); }
@Test public void validMediaTypeWithCharset() throws TransformerException { final StringToMediaTypeTransformer transformer = new StringToMediaTypeTransformer(); final MediaType transformed = (MediaType) transformer.transform("text/plain;charset=" + US_ASCII.name()); assertThat(transformed.getPrimaryType(), is("text")); assertThat(transformed.getSubType(), is("plain")); assertThat(transformed.getCharset().get(), is(US_ASCII)); }
@Test public void newExecutionContextForNonInterceptedProcessor() throws MuleException { final AtomicReference<PrecalculatedExecutionContextAdapter> context = new AtomicReference<>(); Map<String, String> newContextParameters = ImmutableMap.of(MIME_TYPE_PARAMETER_NAME, MediaType.ANY.toRfcString(), ENCODING_PARAMETER_NAME, Charset.defaultCharset().name()); doReturn(newContextParameters).when(parameters).asMap(); doReturn(parameters).when(resolverSet).resolve(any(ValueResolvingContext.class)); messageProcessor.resolveParameters(CoreEvent.builder(event), (params, ctx) -> { assertThat(ctx, instanceOf(PrecalculatedExecutionContextAdapter.class)); context.set(spy((PrecalculatedExecutionContextAdapter) ctx)); }); messageProcessor.process(quickCopy(event, singletonMap(INTERCEPTION_RESOLVED_CONTEXT, context.get()))); verify(operationExecutor, never()).execute(context.get()); verify(operationExecutor).execute(any(ExecutionContext.class)); messageProcessor.disposeResolvedParameters(context.get()); }
private void assertCustomMimeType(DataType dataType) { assertThat(dataType.getMediaType().getPrimaryType(), is("dead")); assertThat(dataType.getMediaType().getSubType(), is("dead")); }
if (optionalMediaType.isPresent()) { mediaType = optionalMediaType.get(); if (mediaType.getCharset().isPresent()) { existingEncoding = mediaType.getCharset().get(); mediaType = MediaType.parse(operationContext.getParameter(MIME_TYPE_PARAMETER_NAME)); mediaType = mediaType.withCharset(parseCharset(operationContext.getParameter(ENCODING_PARAMETER_NAME))); } else { mediaType = mediaType.withCharset(existingEncoding);
public static TypedValue fromMuleTypedValue(org.mule.runtime.api.metadata.TypedValue typedValue) { MediaType mediaType = typedValue.getDataType().getMediaType(); return new TypedValue(typedValue.getValue(), mediaType.withoutParameters().toRfcString(), mediaType.getCharset().orElse(null)); }
@Override public Event process(Event event) throws MessagingException { MediaType endpointMimeType = endpoint.getMimeType(); if (endpointMimeType != null) { InternalMessage message = event.getMessage(); final DataType dataType = message.getPayload().getDataType(); if (DataType.OBJECT.getMediaType().matches(dataType.getMediaType())) { event = Event.builder(event).message(InternalMessage.builder(event.getMessage()).mediaType(endpointMimeType).build()).build(); } else { if (!dataType.getMediaType().matches(endpointMimeType)) { throw new MessagingException(CoreMessages.unexpectedMIMEType(dataType.getMediaType().toRfcString(), endpointMimeType.toRfcString()), event, this); } } } return event; }
/** * Creates a new {@link MediaType} instance keeping the {@code type} and {@code sub-type} but removing all the * parameters (like the {@code charset}) * * @return new immutable {@link MediaType} instance. */ public MediaType withoutParameters() { return create(this.getPrimaryType(), this.getSubType()); }
@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 strictMimeType() throws Exception { CoreEvent response = runFlow("strictMimeType"); assertThat(response.getMessage().getPayload().getDataType().getMediaType().matches(TEXT), is(true)); }
private boolean mediaTypesMatch(DataType other) { if (this.getMediaType() == null && other.getMediaType() != null) { return false; } if (this.getMediaType() != null && other.getMediaType() == null) { return false; } if (!this.getMediaType().getCharset().isPresent()) { return this.getMediaType().matches(other.getMediaType()); } else { return this.getMediaType().equals(other.getMediaType()); } }
private void doFileMimeTypeTest(String fileExtension, MediaType expectedMimeType) throws IOException { String filename = "test." + fileExtension; String mimeType = mimetypesFileTypeMap.getContentType(filename); assertThat(mimeType, equalTo(expectedMimeType.toString())); } }
.uri(tokenUrl).method(POST.name()) .entity(new ByteArrayHttpEntity(encodeString(tokenRequestFormToSend, encoding).getBytes())) .addHeader(CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED.toRfcString()); response.getHeaderValueIgnoreCase(CONTENT_TYPE) != null ? parse(response.getHeaderValueIgnoreCase(CONTENT_TYPE)) : ANY;
@Override protected void describeMismatchSafely(DataType dataType, Description mismatchDescription) { mismatchDescription .appendText("got a dataType with type = ").appendValue(dataType.getType().getName()) .appendText(", mimeType = ").appendValue(dataType.getMediaType()) .appendText(", encoding = ").appendValue(dataType.getMediaType().getCharset()); }
private String getDescription(MediaType mediaType) { return mediaType.withoutParameters().toRfcString(); }
MediaType mediaType = MediaType.parse(mockedEventPayload.getMediaType()); if (mockedEventPayload.getEncoding() != null) { mediaType = mediaType.withCharset(Charset.forName(mockedEventPayload.getEncoding()));
@Benchmark public Object noCharset() { return MediaType.create("text", "plain" + System.nanoTime()); }