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

The following examples show how to use org.springframework.util.MimeType#includes() . 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: MailUtils.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
/**
 * Convert message body to text if possible, otherwise throw exception.
 * @param body
 * @return
 */
public static String toPlainText(MailBody body) throws MailBadMessageException {
    MimeType mimeType = body.getMimeType();
    boolean containsMime = false;
    for (MimeType type : mimeTypeSet) {
        containsMime = containsMime || type.includes(mimeType);
    }
    if(!containsMime) {
        throw new MailBadMessageException("Message contains body with unsupported contentType: " + mimeType);
    }
    try(Reader r = body.getReader()) {
        return IOUtils.toString(r);
    } catch (IOException e) {
        throw new MailBadMessageException(e);
    }
}
 
Example 2
Source File: NegotiatingMessageConverterWrapper.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Override
public Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint) {
	MimeType accepted = headers.get(ACCEPT, MimeType.class);
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.copyHeaders(headers);
	accessor.removeHeader(ACCEPT);
	// Fall back to (concrete) 'contentType' header if 'accept' is not present.
	// MimeType.includes() below should then amount to equality.
	if (accepted == null) {
		accepted = headers.get(MessageHeaders.CONTENT_TYPE, MimeType.class);
	}

	if (accepted != null) {
		for (MimeType supportedConcreteType : delegate.getSupportedMimeTypes()) {
			if (accepted.includes(supportedConcreteType)) {
				// Note the use of setHeader() which will set the value even if already present.
				accessor.setHeader(MessageHeaders.CONTENT_TYPE, supportedConcreteType);
				Message<?> result = delegate.toMessage(payload, accessor.toMessageHeaders(), conversionHint);
				if (result != null) {
					return result;
				}
			}
		}
	}
	return null;
}
 
Example 3
Source File: MessageHeaderAccessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected boolean isReadableContentType() {
	MimeType contentType = getContentType();
	for (MimeType mimeType : READABLE_MIME_TYPES) {
		if (mimeType.includes(contentType)) {
			return true;
		}
	}
	return false;
}
 
Example 4
Source File: MessageHeaderAccessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected boolean isReadableContentType() {
	MimeType contentType = getContentType();
	for (MimeType mimeType : READABLE_MIME_TYPES) {
		if (mimeType.includes(contentType)) {
			return true;
		}
	}
	return false;
}
 
Example 5
Source File: MessageHeaderAccessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected boolean isReadableContentType() {
	for (MimeType mimeType : READABLE_MIME_TYPES) {
		if (mimeType.includes(getContentType())) {
			return true;
		}
	}
	return false;
}
 
Example 6
Source File: CompositeMessageConverterFactory.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * Creation method.
 * @param mimeType the target MIME type
 * @return a converter for the target MIME type
 */
public MessageConverter getMessageConverterForType(MimeType mimeType) {
	List<MessageConverter> converters = new ArrayList<>();
	for (MessageConverter converter : this.converters) {
		if (converter instanceof AbstractMessageConverter) {
			for (MimeType type : ((AbstractMessageConverter) converter)
					.getSupportedMimeTypes()) {
				if (type.includes(mimeType)) {
					converters.add(converter);
				}
			}
		}
		else {
			if (this.log.isDebugEnabled()) {
				this.log.debug("Ommitted " + converter + " of type "
						+ converter.getClass().toString() + " for '"
						+ mimeType.toString()
						+ "' as it is not an AbstractMessageConverter");
			}
		}
	}
	if (CollectionUtils.isEmpty(converters)) {
		throw new ConversionException(
				"No message converter is registered for " + mimeType.toString());
	}
	if (converters.size() > 1) {
		return new CompositeMessageConverter(converters);
	}
	else {
		return converters.get(0);
	}
}