Java Code Examples for org.springframework.util.MimeType#valueOf()

The following examples show how to use org.springframework.util.MimeType#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultContentTypeResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Nullable
public MimeType resolve(@Nullable MessageHeaders headers) {
	if (headers == null || headers.get(MessageHeaders.CONTENT_TYPE) == null) {
		return this.defaultMimeType;
	}
	Object value = headers.get(MessageHeaders.CONTENT_TYPE);
	if (value == null) {
		return null;
	}
	else if (value instanceof MimeType) {
		return (MimeType) value;
	}
	else if (value instanceof String) {
		return MimeType.valueOf((String) value);
	}
	else {
		throw new IllegalArgumentException(
				"Unknown type for contentType header value: " + value.getClass());
	}
}
 
Example 2
Source File: BaseFilter.java    From convergent-ui with Apache License 2.0 6 votes vote down vote up
protected MimeType getMimeType(RequestContext context) {
    List<Pair<String, String>> headers = context.getZuulResponseHeaders();
    String contentType = null;
    for (Pair<String, String> pair : headers) {
        if ("content-type".equalsIgnoreCase(pair.first())) {
            contentType = pair.second();
            break;
        }
    }

    if (contentType != null) {
        MimeType type = MimeType.valueOf(contentType);
        return type;
    }
    return null;
}
 
Example 3
Source File: ResourceRegionEncoderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void canEncode() {
	ResolvableType resourceRegion = ResolvableType.forClass(ResourceRegion.class);
	MimeType allMimeType = MimeType.valueOf("*/*");

	assertFalse(this.encoder.canEncode(ResolvableType.forClass(Resource.class),
			MimeTypeUtils.APPLICATION_OCTET_STREAM));
	assertFalse(this.encoder.canEncode(ResolvableType.forClass(Resource.class), allMimeType));
	assertTrue(this.encoder.canEncode(resourceRegion, MimeTypeUtils.APPLICATION_OCTET_STREAM));
	assertTrue(this.encoder.canEncode(resourceRegion, allMimeType));

	// SPR-15464
	assertFalse(this.encoder.canEncode(ResolvableType.NONE, null));
}
 
Example 4
Source File: NegotiatingMessageConverterWrapperTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationWithCompatibleConcreteAcceptHeader() {
	MimeType acceptableType = MimeType.valueOf("text/csv");

	Message<?> result = NegotiatingMessageConverterWrapper.wrap(new NaiveCsvTupleMessageConverter())
		.toMessage(somePayload, new MessageHeaders(newHashMap(ACCEPT, acceptableType)));

	assertMessageContent(result, "text/csv", expectedSerializedPayload);
}
 
Example 5
Source File: NegotiatingMessageConverterWrapperTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationWithFallbackContentTypeHeader() {
	MimeType fallbackContentType = MimeType.valueOf("text/csv");

	Message<?> result = NegotiatingMessageConverterWrapper.wrap(new NaiveCsvTupleMessageConverter())
		.toMessage(somePayload, new MessageHeaders(newHashMap(CONTENT_TYPE, fallbackContentType)));

	assertMessageContent(result, "text/csv", expectedSerializedPayload);
}
 
Example 6
Source File: NegotiatingMessageConverterWrapperTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoSerializationWithNullPayload() {
	Object payload = MAGIC_NULL;
	MimeType acceptableType = MimeType.valueOf("text/csv");

	Message<?> result = NegotiatingMessageConverterWrapper.wrap(new NaiveCsvTupleMessageConverter())
		.toMessage(payload, new MessageHeaders(newHashMap(ACCEPT, acceptableType)));

	assertThat(result).overridingErrorMessage("Serialization should not happen").isNull();
}
 
Example 7
Source File: NegotiatingMessageConverterWrapperTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoSerializationWithIncompatibleFallbackContentTypeHeader() {
	MimeType fallbackContentType = MimeType.valueOf("application/*");

	Message<?> result = NegotiatingMessageConverterWrapper.wrap(new NaiveCsvTupleMessageConverter())
		.toMessage(somePayload, new MessageHeaders(newHashMap(CONTENT_TYPE, fallbackContentType)));

	assertThat(result).overridingErrorMessage("Serialization should not happen").isNull();
}
 
Example 8
Source File: BookingApp.java    From Mastering-Microservices-with-Java-9-Second-Edition with MIT License 5 votes vote down vote up
@Bean
public MessageConverter bookingOrderMessageConverter() throws IOException {
    LOG.info("avro message converter bean initialized.");
    AvroSchemaMessageConverter avroSchemaMessageConverter = new AvroSchemaMessageConverter(MimeType.valueOf("application/bookingOrder.v1+avro"));
    avroSchemaMessageConverter.setSchemaLocation(new ClassPathResource("avro/bookingOrder.avsc"));
    return avroSchemaMessageConverter;
}
 
Example 9
Source File: NegotiatingMessageConverterWrapperTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoSerializationWithIncompatibleAcceptHeader() {
	MimeType acceptableType = MimeType.valueOf("application/*");

	Message<?> result = NegotiatingMessageConverterWrapper.wrap(new NaiveCsvTupleMessageConverter())
		.toMessage(somePayload, new MessageHeaders(newHashMap(ACCEPT, acceptableType)));

	assertThat(result).overridingErrorMessage("Serialization should not happen").isNull();
}
 
Example 10
Source File: JavaClassMimeTypeUtils.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * Convert payload to {@link MimeType} based on the content type on the message.
 * @param payload the payload to convert
 * @param originalContentType content type on the message
 * @return converted {@link MimeType}
 */
public static MimeType mimeTypeFromObject(Object payload,
		String originalContentType) {
	Assert.notNull(payload, "payload object cannot be null.");
	if (payload instanceof byte[]) {
		return MimeTypeUtils.APPLICATION_OCTET_STREAM;
	}
	if (payload instanceof String) {
		return MimeTypeUtils.APPLICATION_JSON_VALUE.equals(originalContentType)
				? MimeTypeUtils.APPLICATION_JSON : MimeTypeUtils.TEXT_PLAIN;
	}
	String className = payload.getClass().getName();
	MimeType mimeType = mimeTypesCache.get(className);
	if (mimeType == null) {
		String modifiedClassName = className;
		if (payload.getClass().isArray()) {
			// Need to remove trailing ';' for an object array, e.g.
			// "[Ljava.lang.String;" or multi-dimensional
			// "[[[Ljava.lang.String;"
			if (modifiedClassName.endsWith(";")) {
				modifiedClassName = modifiedClassName.substring(0,
						modifiedClassName.length() - 1);
			}
			// Wrap in quotes to handle the illegal '[' character
			modifiedClassName = "\"" + modifiedClassName + "\"";
		}
		mimeType = MimeType
				.valueOf("application/x-java-object;type=" + modifiedClassName);
		mimeTypesCache.put(className, mimeType);
	}
	return mimeType;
}
 
Example 11
Source File: ResourceRegionEncoderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void canEncode() {
	ResolvableType resourceRegion = ResolvableType.forClass(ResourceRegion.class);
	MimeType allMimeType = MimeType.valueOf("*/*");

	assertFalse(this.encoder.canEncode(ResolvableType.forClass(Resource.class),
			MimeTypeUtils.APPLICATION_OCTET_STREAM));
	assertFalse(this.encoder.canEncode(ResolvableType.forClass(Resource.class), allMimeType));
	assertTrue(this.encoder.canEncode(resourceRegion, MimeTypeUtils.APPLICATION_OCTET_STREAM));
	assertTrue(this.encoder.canEncode(resourceRegion, allMimeType));

	// SPR-15464
	assertFalse(this.encoder.canEncode(ResolvableType.NONE, null));
}
 
Example 12
Source File: MessageHeaderAccessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
public MimeType getContentType() {
	Object value = getHeader(MessageHeaders.CONTENT_TYPE);
	if (value == null) {
		return null;
	}
	return (value instanceof MimeType ? (MimeType) value : MimeType.valueOf(value.toString()));
}
 
Example 13
Source File: NegotiatingMessageConverterWrapperTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationWithCompatibleWildcardSubtypeAcceptHeader() {
	MimeType acceptableType = MimeType.valueOf("text/*");

	Message<?> result = NegotiatingMessageConverterWrapper.wrap(new NaiveCsvTupleMessageConverter())
		.toMessage(somePayload, new MessageHeaders(newHashMap(ACCEPT, acceptableType)));

	assertMessageContent(result, "text/csv", expectedSerializedPayload);
}
 
Example 14
Source File: BillingApp.java    From Mastering-Microservices-with-Java-Third-Edition with MIT License 5 votes vote down vote up
@Bean
@StreamMessageConverter
public MessageConverter bookingOrderMessageConverter() throws IOException {
  MessageConverter avroSchemaMessageConverter = new AvroSchemaMessageConverter(
      MimeType.valueOf("application/*+avro"));
  ((AvroSchemaMessageConverter) avroSchemaMessageConverter)
      .setSchemaLocation(new ClassPathResource("avro/bookingOrder.avsc"));
  return avroSchemaMessageConverter;
}
 
Example 15
Source File: AppConfig.java    From Mastering-Microservices-with-Java-Third-Edition with MIT License 5 votes vote down vote up
@Bean
@StreamMessageConverter
public MessageConverter bookingOrderMessageConverter() throws IOException {
  LOG.info("avro message converter bean initialized.");
  MessageConverter avroSchemaMessageConverter = new AvroSchemaMessageConverter(
      MimeType.valueOf("application/*+avro"));
  ((AvroSchemaMessageConverter) avroSchemaMessageConverter)
      .setSchemaLocation(new ClassPathResource("avro/bookingOrder.avsc"));
  return avroSchemaMessageConverter;
}
 
Example 16
Source File: ContentTypeTckTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Bean
public AlwaysStringKryoMessageConverter kryoOverrideMessageConverter() {
	return new AlwaysStringKryoMessageConverter(
			MimeType.valueOf("application/x-java-object"));
}
 
Example 17
Source File: ContentTypeTckTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Bean
public FooBarMessageConverter fooBarMessageConverter() {
	return new FooBarMessageConverter(MimeType.valueOf("foo/bar"));
}
 
Example 18
Source File: RequestContents.java    From gocd with Apache License 2.0 4 votes vote down vote up
default MimeType contentType() {
    return MimeType.valueOf(request().contentType());
}
 
Example 19
Source File: AvroCodecAutoConfiguration.java    From schema-evolution-samples with Apache License 2.0 4 votes vote down vote up
public AvroToPojoMessageConverter(AvroCodec codec) {
	super(MimeType.valueOf("avro/binary"));
	this.codec = codec;
}
 
Example 20
Source File: MessageConverterUtils.java    From spring-cloud-stream with Apache License 2.0 2 votes vote down vote up
/**
 * Build the conventional {@link MimeType} for a java object.
 * @param clazz the java type
 * @return the MIME type
 */
public static MimeType javaObjectMimeType(Class<?> clazz) {
	return MimeType.valueOf("application/x-java-object;type=" + clazz.getName());
}