@Test public void mimeTypeWithEncodingInformation() throws Exception { DataType dataType = DataType.builder().type(type).mediaType(format("%s; charset=UTF-8", mimeType)).charset(encoding).build(); assertThat(dataType.getType(), equalTo(type)); assertThat(dataType.getMediaType().getPrimaryType(), is(mimeType.split("/")[0])); assertThat(dataType.getMediaType().getSubType(), is(mimeType.split("/")[1])); assertThat(dataType.getMediaType().getCharset().get(), is(encoding)); }
/** * Uses {@link org.mule.runtime.api.transformation.TransformationService} to get representation of a message for a given * {@link Class} * * @param message message to get payload from * @param clazz type of the payload to be transformed to * @return representation of the message payload of the required class * @throws Exception if there is an unexpected error obtaining the payload representation */ protected <T> T getPayload(Message message, Class<T> clazz) throws Exception { return (T) getPayload(message, DataType.fromType(clazz)); }
private Object doUnWrap(Object value, DataType expected) { return value instanceof TypedValue ? doUnWrap(((TypedValue) value).getValue(), expected) : ClassUtils.isInstance(expected.getType(), value) ? value : transformationService.transform(value, fromObject(value), expected); }
@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()); }
@Override public ValueResolver<Object> parse(String key, MetadataType metadataType, DslElementSyntax elementDsl) { return new StaticValueResolver<>(DataType.builder().mediaType(key).build().getMediaType()); } }
@Test public void ifCharsetItsNotSpecifiedItShouldBeCompatibleWithAny() throws Exception { DataType jsonWithCharset = builder(JSON_PARENT_DATA_TYPE).charset(UTF_8).build(); assertThat(JSON_PARENT_DATA_TYPE, is(not(assignableTo(jsonWithCharset)))); assertThat(jsonWithCharset, is(assignableTo(JSON_PARENT_DATA_TYPE))); }
private Map<String, SoapAttachment> toSoapAttachments(Map<String, TypedValue<?>> attachments) throws MessageTransformerException, TransformerException { Map<String, SoapAttachment> soapAttachmentMap = new HashMap<>(); for (Map.Entry<String, TypedValue<?>> attachment : attachments.entrySet()) { SoapAttachment soapAttachment = new SoapAttachment(toInputStream(attachment.getValue()), attachment.getValue().getDataType().getMediaType()); soapAttachmentMap.put(attachment.getKey(), soapAttachment); } return soapAttachmentMap; }
private Transformer getIncreaseByOneTransformer() throws Exception { AbstractTransformer transformer = new AbstractTransformer() { @Override protected Object doTransform(Object src, Charset encoding) throws TransformerException { return new Integer(((Integer) src).intValue() + 1); } }; DataType integerDataType = DataType.fromType(Integer.class); transformer.registerSourceType(integerDataType); transformer.setReturnDataType(DataType.builder(integerDataType).charset(getDefaultEncoding(muleContext)).build()); return transformer; }
@Test public void buildTypedCollectionFromImplementationClass() { final DataType dataType = DataType.builder().collectionType(SpecificCollection.class).build(); assertThat(dataType, instanceOf(DefaultCollectionDataType.class)); assertThat(dataType.getType(), is(equalTo(SpecificCollection.class))); assertThat(((DefaultCollectionDataType) dataType).getItemDataType(), is(STRING)); }
@Override public void initialise() throws InitialisationException { TypeSafeTransformer typeSafeTransformer = new TypeSafeTransformer(transformationService); resolver = context -> { Object resolvedValue = valueResolverDelegate.resolve(context); return isInstance(expectedType, resolvedValue) ? (T) resolvedValue : typeSafeTransformer.transform(resolvedValue, DataType.fromObject(resolvedValue), DataType.fromType(expectedType)); }; if (!valueResolverDelegate.isDynamic() && !InputStream.class.isAssignableFrom(expectedType)) { resolver = new CachedResolver(resolver); } }
private DataType resolveAttributesDataType(Object value) { if (attributes == null) { return DataType.fromObject(value); } else { return DataType.builder(attributes.getDataType()).fromObject(value).build(); } }
@Test public void authenticationBinding() throws Exception { CoreEvent event = spy(testEvent()); Authentication authentication = new DefaultMuleAuthentication(new DefaultMuleCredentials("username", "password".toCharArray())); when(event.getAuthentication()).thenReturn(of(authentication)); TypedValue result = expressionLanguage.evaluate(AUTHENTICATION, event, BindingContext.builder().build()); assertThat(result.getValue(), is(instanceOf(Authentication.class))); assertThat(result.getValue(), is(authentication)); assertThat(result.getDataType().getType(), is(equalTo(Authentication.class))); }
/** * {@inheritDoc} */ @Override public Supplier<TypedValue<T>> resolve(ExecutionContext executionContext) { return () -> { T value = argumentResolver.resolve(executionContext).get(); return value instanceof TypedValue ? (TypedValue<T>) value : new TypedValue<>(value, DataType.fromObject(value)); }; } }
@Override protected boolean matchesSafely(DataType otherDataType) { return dataType.isCompatibleWith(otherDataType); }
@Test public void acceptsValidMimeType() throws Exception { DataType dataType = DataType.builder().mediaType(JSON).build(); assertThat(dataType.getMediaType(), equalTo(JSON)); }
@Factory public static Matcher<DataType> like(DataType dataType) { return new DataTypeMatcher(dataType.getType(), dataType.getMediaType(), dataType.getMediaType().getCharset().orElse(null)); } }
@Test public void transform() throws Exception { doReturn("MyOutput1").when(mockConverterA).transform(any()); doReturn(DataType.builder().charset(UTF_8).build()).when(mockConverterA).getReturnDataType(); doReturn("MyOutput2").when(mockConverterB).transform(eq("MyOutput1"), eq(UTF_8)); doReturn(DataType.builder().charset(UTF_8).build()).when(mockConverterB).getReturnDataType(); CompositeConverter compositeConverter = new CompositeConverter(mockConverterA, mockConverterB); Object output = compositeConverter.transform("MyInput"); verify(mockConverterA, times(1)).transform("MyInput"); verify(mockConverterB, times(1)).transform("MyOutput1", UTF_8); assertEquals("MyOutput2", output); }
@Override public Supplier<MediaType> resolve(ExecutionContext executionContext) { return () -> ((ExecutionContextAdapter) executionContext).getEvent().getMessage().getPayload().getDataType().getMediaType(); } }