Java Code Examples for org.springframework.http.converter.HttpMessageConverter#canWrite()

The following examples show how to use org.springframework.http.converter.HttpMessageConverter#canWrite() . 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: AbstractMessageConverterMethodProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the media types that can be produced:
 * <ul>
 * <li>The producible media types specified in the request mappings, or
 * <li>Media types of configured converters that can write the specific return value, or
 * <li>{@link MediaType#ALL}
 * </ul>
 * @since 4.2
 */
@SuppressWarnings("unchecked")
protected List<MediaType> getProducibleMediaTypes(HttpServletRequest request, Class<?> valueClass, Type declaredType) {
	Set<MediaType> mediaTypes = (Set<MediaType>) request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
	if (!CollectionUtils.isEmpty(mediaTypes)) {
		return new ArrayList<MediaType>(mediaTypes);
	}
	else if (!this.allSupportedMediaTypes.isEmpty()) {
		List<MediaType> result = new ArrayList<MediaType>();
		for (HttpMessageConverter<?> converter : this.messageConverters) {
			if (converter instanceof GenericHttpMessageConverter && declaredType != null) {
				if (((GenericHttpMessageConverter<?>) converter).canWrite(declaredType, valueClass, null)) {
					result.addAll(converter.getSupportedMediaTypes());
				}
			}
			else if (converter.canWrite(valueClass, null)) {
				result.addAll(converter.getSupportedMediaTypes());
			}
		}
		return result;
	}
	else {
		return Collections.singletonList(MediaType.ALL);
	}
}
 
Example 2
Source File: SpringConverterFactory.java    From spring-cloud-square with Apache License 2.0 6 votes vote down vote up
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
	MediaType requestContentType = MediaType.APPLICATION_JSON; //TODO: determine dynamically?

	if (type instanceof Class) {
		Class<?> requestType = (Class<?>) type;
		for (HttpMessageConverter<?> messageConverter : this.messageConverters
				.getObject().getConverters()) {
			if (messageConverter.canWrite(requestType, requestContentType)) {
				@SuppressWarnings("unchecked")
				SpringRequestConverter converter = new SpringRequestConverter(requestContentType, messageConverter);
				return converter;
			}
		}
	}
	return null;
}
 
Example 3
Source File: FormHttpMessageConverter.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException {
	Object partBody = partEntity.getBody();
	if (partBody == null) {
		throw new IllegalStateException("Empty body for part '" + name + "': " + partEntity);
	}
	Class<?> partType = partBody.getClass();
	HttpHeaders partHeaders = partEntity.getHeaders();
	MediaType partContentType = partHeaders.getContentType();
	for (HttpMessageConverter<?> messageConverter : this.partConverters) {
		if (messageConverter.canWrite(partType, partContentType)) {
			Charset charset = isFilenameCharsetSet() ? StandardCharsets.US_ASCII : this.charset;
			HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os, charset);
			multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody));
			if (!partHeaders.isEmpty()) {
				multipartMessage.getHeaders().putAll(partHeaders);
			}
			((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage);
			return;
		}
	}
	throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter " +
			"found for request type [" + partType.getName() + "]");
}
 
Example 4
Source File: FeignMultipartEncoder.java    From hawkbit-examples with Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void encodeRequest(final Object value, final HttpHeaders requestHeaders, final RequestTemplate template) {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders);
    try {
        final Class<?> requestType = value.getClass();
        final MediaType requestContentType = requestHeaders.getContentType();
        for (final HttpMessageConverter<?> messageConverter : converters) {
            if (messageConverter.canWrite(requestType, requestContentType)) {
                ((HttpMessageConverter<Object>) messageConverter).write(value, requestContentType, dummyRequest);
                break;
            }
        }
    } catch (final IOException ex) {
        throw new EncodeException("Cannot encode request.", ex);
    }
    final HttpHeaders headers = dummyRequest.getHeaders();
    for (final Entry<String, List<String>> entry : headers.entrySet()) {
        template.header(entry.getKey(), entry.getValue());
    }
    /*
     * we should use a template output stream... this will cause issues if
     * files are too big, since the whole request will be in memory.
     */
    template.body(outputStream.toByteArray(), UTF_8);
}
 
Example 5
Source File: AbstractMessageConverterMethodProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the media types that can be produced:
 * <ul>
 * <li>The producible media types specified in the request mappings, or
 * <li>Media types of configured converters that can write the specific return value, or
 * <li>{@link MediaType#ALL}
 * </ul>
 * @since 4.2
 */
@SuppressWarnings("unchecked")
protected List<MediaType> getProducibleMediaTypes(HttpServletRequest request, Class<?> returnValueClass, Type returnValueType) {
	Set<MediaType> mediaTypes = (Set<MediaType>) request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
	if (!CollectionUtils.isEmpty(mediaTypes)) {
		return new ArrayList<MediaType>(mediaTypes);
	}
	else if (!this.allSupportedMediaTypes.isEmpty()) {
		List<MediaType> result = new ArrayList<MediaType>();
		for (HttpMessageConverter<?> converter : this.messageConverters) {
			if (converter instanceof GenericHttpMessageConverter && returnValueType != null) {
				if (((GenericHttpMessageConverter<?>) converter).canWrite(returnValueType, returnValueClass, null)) {
					result.addAll(converter.getSupportedMediaTypes());
				}
			}
			else if (converter.canWrite(returnValueClass, null)) {
				result.addAll(converter.getSupportedMediaTypes());
			}
		}
		return result;
	}
	else {
		return Collections.singletonList(MediaType.ALL);
	}
}
 
Example 6
Source File: HttpMessageConverterResolver.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the media types that can be produced:
 * <ul>
 * <li>The producible media types specified in the request mappings, or
 * <li>Media types of configured converters that can write the specific return value,
 * or
 * <li>{@link MediaType#ALL}.
 * </ul>
 * @param restMethodMetadata {@link RestMethodMetadata} from server side
 * @param returnValueClass the class of return value
 * @return non-null {@link List}
 */
private List<MediaType> getProducibleMediaTypes(RestMethodMetadata restMethodMetadata,
		Class<?> returnValueClass) {
	RequestMetadata serverRequestMetadata = restMethodMetadata.getRequest();
	List<MediaType> mediaTypes = serverRequestMetadata.getProduceMediaTypes();
	if (!CollectionUtils.isEmpty(mediaTypes)) { // Empty
		return mediaTypes;
	}
	else if (!this.allSupportedMediaTypes.isEmpty()) {
		List<MediaType> result = new ArrayList<>();
		for (HttpMessageConverter<?> converter : this.messageConverters) {
			if (converter.canWrite(returnValueClass, null)) {
				result.addAll(converter.getSupportedMediaTypes());
			}
		}
		return result;
	}
	else {
		return Collections.singletonList(MediaType.ALL);
	}
}
 
Example 7
Source File: SpringEncoder.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private FeignOutputMessage checkAndWrite(Object body, MediaType contentType,
		HttpMessageConverter converter, RequestTemplate request) throws IOException {
	if (converter.canWrite(body.getClass(), contentType)) {
		logBeforeWrite(body, contentType, converter);
		FeignOutputMessage outputMessage = new FeignOutputMessage(request);
		converter.write(body, contentType, outputMessage);
		return outputMessage;
	}
	else {
		return null;
	}
}
 
Example 8
Source File: AnnotationMethodHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
private ModelAndView handleResponseBody(Object returnValue, ServletWebRequest webRequest)
		throws ServletException, IOException {

	HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
	List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
	if (acceptedMediaTypes.isEmpty()) {
		acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
	}
	MediaType.sortByQualityValue(acceptedMediaTypes);
	HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
	Class<?> returnValueType = returnValue.getClass();
	if (this.messageConverters != null) {
		for (MediaType acceptedMediaType : acceptedMediaTypes) {
			for (HttpMessageConverter messageConverter : this.messageConverters) {
				if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
					messageConverter.write(returnValue, acceptedMediaType, outputMessage);
					return new ModelAndView();
				}
			}
		}
	}
	if (logger.isWarnEnabled()) {
		logger.warn("Could not find HttpMessageConverter that supports return type [" + returnValueType + "] and " +
				acceptedMediaTypes);
	}
	return null;
}
 
Example 9
Source File: DefaultEntityResponseBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected void writeEntityWithMessageConverters(Object entity, HttpServletRequest request,
		HttpServletResponse response, ServerResponse.Context context)
		throws ServletException, IOException {

	ServletServerHttpResponse serverResponse = new ServletServerHttpResponse(response);
	MediaType contentType = getContentType(response);
	Class<?> entityClass = entity.getClass();

	for (HttpMessageConverter<?> messageConverter : context.messageConverters()) {
		if (messageConverter instanceof GenericHttpMessageConverter<?>) {
			GenericHttpMessageConverter<Object> genericMessageConverter =
					(GenericHttpMessageConverter<Object>) messageConverter;
			if (genericMessageConverter.canWrite(this.entityType, entityClass, contentType)) {
				genericMessageConverter.write(entity, this.entityType, contentType, serverResponse);
				return;
			}
		}
		if (messageConverter.canWrite(entityClass, contentType)) {
			((HttpMessageConverter<Object>)messageConverter).write(entity, contentType, serverResponse);
			return;
		}
	}

	List<MediaType> producibleMediaTypes = producibleMediaTypes(context.messageConverters(), entityClass);
	throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes);
}
 
Example 10
Source File: ResponseBodyEmitterReturnValueHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> void sendInternal(T data, MediaType mediaType) throws IOException {
	for (HttpMessageConverter<?> converter : ResponseBodyEmitterReturnValueHandler.this.messageConverters) {
		if (converter.canWrite(data.getClass(), mediaType)) {
			((HttpMessageConverter<T>) converter).write(data, mediaType, this.outputMessage);
			this.outputMessage.flush();
			if (logger.isDebugEnabled()) {
				logger.debug("Written [" + data + "] using [" + converter + "]");
			}
			return;
		}
	}
	throw new IllegalArgumentException("No suitable converter for " + data.getClass());
}
 
Example 11
Source File: FeignSpringFormEncoder.java    From feign-client-test with MIT License 5 votes vote down vote up
/**
 * Calls the conversion chain actually used by
 * {@link org.springframework.web.client.RestTemplate}, filling the body of the request
 * template.
 *
 * @param value
 * @param requestHeaders
 * @param template
 * @throws EncodeException
 */
private void encodeRequest(Object value, HttpHeaders requestHeaders, RequestTemplate template) throws EncodeException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders);
    try {
        Class<?> requestType = value.getClass();
        MediaType requestContentType = requestHeaders.getContentType();
        for (HttpMessageConverter<?> messageConverter : converters) {
            if (messageConverter.canWrite(requestType, requestContentType)) {
                ((HttpMessageConverter<Object>) messageConverter).write(
                        value, requestContentType, dummyRequest);
                break;
            }
        }
    } catch (IOException ex) {
        throw new EncodeException("Cannot encode request.", ex);
    }
    HttpHeaders headers = dummyRequest.getHeaders();
    if (headers != null) {
        for (Entry<String, List<String>> entry : headers.entrySet()) {
            template.header(entry.getKey(), entry.getValue());
        }
    }
    /*
    we should use a template output stream... this will cause issues if files are too big, 
    since the whole request will be in memory.
     */
    template.body(outputStream.toByteArray(), UTF_8);
}
 
Example 12
Source File: AnnotationMethodHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
private ModelAndView handleResponseBody(Object returnValue, ServletWebRequest webRequest)
		throws ServletException, IOException {

	HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
	List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
	if (acceptedMediaTypes.isEmpty()) {
		acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
	}
	MediaType.sortByQualityValue(acceptedMediaTypes);
	HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
	Class<?> returnValueType = returnValue.getClass();
	if (this.messageConverters != null) {
		for (MediaType acceptedMediaType : acceptedMediaTypes) {
			for (HttpMessageConverter messageConverter : this.messageConverters) {
				if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
					messageConverter.write(returnValue, acceptedMediaType, outputMessage);
					return new ModelAndView();
				}
			}
		}
	}
	if (logger.isWarnEnabled()) {
		logger.warn("Could not find HttpMessageConverter that supports return type [" + returnValueType + "] and " +
				acceptedMediaTypes);
	}
	return null;
}
 
Example 13
Source File: ResponseBodyEmitterReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> void sendInternal(T data, @Nullable MediaType mediaType) throws IOException {
	for (HttpMessageConverter<?> converter : ResponseBodyEmitterReturnValueHandler.this.messageConverters) {
		if (converter.canWrite(data.getClass(), mediaType)) {
			((HttpMessageConverter<T>) converter).write(data, mediaType, this.outputMessage);
			this.outputMessage.flush();
			return;
		}
	}
	throw new IllegalArgumentException("No suitable converter for " + data.getClass());
}
 
Example 14
Source File: ResponseBodyEmitterReturnValueHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> void sendInternal(T data, MediaType mediaType) throws IOException {
	for (HttpMessageConverter<?> converter : ResponseBodyEmitterReturnValueHandler.this.messageConverters) {
		if (converter.canWrite(data.getClass(), mediaType)) {
			((HttpMessageConverter<T>) converter).write(data, mediaType, this.outputMessage);
			this.outputMessage.flush();
			if (logger.isDebugEnabled()) {
				logger.debug("Written [" + data + "] using [" + converter + "]");
			}
			return;
		}
	}
	throw new IllegalArgumentException("No suitable converter for " + data.getClass());
}
 
Example 15
Source File: AbstractMessageConverterMethodProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Returns the media types that can be produced. The resulting media types are:
 * <ul>
 * <li>The producible media types specified in the request mappings, or
 * <li>Media types of configured converters that can write the specific return value, or
 * <li>{@link MediaType#ALL}
 * </ul>
 * @since 4.2
 */
@SuppressWarnings("unchecked")
protected List<MediaType> getProducibleMediaTypes(
		HttpServletRequest request, Class<?> valueClass, @Nullable Type targetType) {

	Set<MediaType> mediaTypes =
			(Set<MediaType>) request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
	if (!CollectionUtils.isEmpty(mediaTypes)) {
		return new ArrayList<>(mediaTypes);
	}
	else if (!this.allSupportedMediaTypes.isEmpty()) {
		List<MediaType> result = new ArrayList<>();
		for (HttpMessageConverter<?> converter : this.messageConverters) {
			if (converter instanceof GenericHttpMessageConverter && targetType != null) {
				if (((GenericHttpMessageConverter<?>) converter).canWrite(targetType, valueClass, null)) {
					result.addAll(converter.getSupportedMediaTypes());
				}
			}
			else if (converter.canWrite(valueClass, null)) {
				result.addAll(converter.getSupportedMediaTypes());
			}
		}
		return result;
	}
	else {
		return Collections.singletonList(MediaType.ALL);
	}
}
 
Example 16
Source File: ResponseBodyEmitterReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> void sendInternal(T data, @Nullable MediaType mediaType) throws IOException {
	for (HttpMessageConverter<?> converter : ResponseBodyEmitterReturnValueHandler.this.messageConverters) {
		if (converter.canWrite(data.getClass(), mediaType)) {
			((HttpMessageConverter<T>) converter).write(data, mediaType, this.outputMessage);
			this.outputMessage.flush();
			return;
		}
	}
	throw new IllegalArgumentException("No suitable converter for " + data.getClass());
}
 
Example 17
Source File: AbstractMessageConverterMethodProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Returns the media types that can be produced. The resulting media types are:
 * <ul>
 * <li>The producible media types specified in the request mappings, or
 * <li>Media types of configured converters that can write the specific return value, or
 * <li>{@link MediaType#ALL}
 * </ul>
 * @since 4.2
 */
@SuppressWarnings("unchecked")
protected List<MediaType> getProducibleMediaTypes(
		HttpServletRequest request, Class<?> valueClass, @Nullable Type targetType) {

	Set<MediaType> mediaTypes =
			(Set<MediaType>) request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
	if (!CollectionUtils.isEmpty(mediaTypes)) {
		return new ArrayList<>(mediaTypes);
	}
	else if (!this.allSupportedMediaTypes.isEmpty()) {
		List<MediaType> result = new ArrayList<>();
		for (HttpMessageConverter<?> converter : this.messageConverters) {
			if (converter instanceof GenericHttpMessageConverter && targetType != null) {
				if (((GenericHttpMessageConverter<?>) converter).canWrite(targetType, valueClass, null)) {
					result.addAll(converter.getSupportedMediaTypes());
				}
			}
			else if (converter.canWrite(valueClass, null)) {
				result.addAll(converter.getSupportedMediaTypes());
			}
		}
		return result;
	}
	else {
		return Collections.singletonList(MediaType.ALL);
	}
}
 
Example 18
Source File: HttpMessageConverterResolver.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve the most match {@link HttpMessageConverter} from {@link RequestMetadata}.
 * @param requestMetadata {@link RequestMetadata}
 * @param restMethodMetadata {@link RestMethodMetadata}
 * @return instance of {@link HttpMessageConverterHolder}
 */
public HttpMessageConverterHolder resolve(RequestMetadata requestMetadata,
		RestMethodMetadata restMethodMetadata) {

	HttpMessageConverterHolder httpMessageConverterHolder = null;

	Class<?> returnValueClass = resolveReturnValueClass(restMethodMetadata);

	/**
	 * @see AbstractMessageConverterMethodProcessor#writeWithMessageConverters(T,
	 * MethodParameter, ServletServerHttpRequest, ServletServerHttpResponse)
	 */
	List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(requestMetadata);
	List<MediaType> producibleMediaTypes = getProducibleMediaTypes(restMethodMetadata,
			returnValueClass);

	Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
	for (MediaType requestedType : requestedMediaTypes) {
		for (MediaType producibleType : producibleMediaTypes) {
			if (requestedType.isCompatibleWith(producibleType)) {
				compatibleMediaTypes
						.add(getMostSpecificMediaType(requestedType, producibleType));
			}
		}
	}

	if (compatibleMediaTypes.isEmpty()) {
		return httpMessageConverterHolder;
	}

	List<MediaType> mediaTypes = new ArrayList<>(compatibleMediaTypes);

	MediaType.sortBySpecificityAndQuality(mediaTypes);

	MediaType selectedMediaType = null;
	for (MediaType mediaType : mediaTypes) {
		if (mediaType.isConcrete()) {
			selectedMediaType = mediaType;
			break;
		}
		else if (mediaType.equals(MediaType.ALL)
				|| mediaType.equals(MEDIA_TYPE_APPLICATION)) {
			selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
			break;
		}
	}

	if (selectedMediaType != null) {
		selectedMediaType = selectedMediaType.removeQualityValue();
		for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
			if (messageConverter.canWrite(returnValueClass, selectedMediaType)) {
				httpMessageConverterHolder = new HttpMessageConverterHolder(
						selectedMediaType, messageConverter);
				break;
			}
		}
	}

	return httpMessageConverterHolder;
}
 
Example 19
Source File: AbstractMessageConverterMethodProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Writes the given return type to the given output message.
 * @param returnValue the value to write to the output message
 * @param returnType the type of the value
 * @param inputMessage the input messages. Used to inspect the {@code Accept} header.
 * @param outputMessage the output message to write to
 * @throws IOException thrown in case of I/O errors
 * @throws HttpMediaTypeNotAcceptableException thrown when the conditions indicated by {@code Accept} header on
 * the request cannot be met by the message converters
 */
@SuppressWarnings("unchecked")
protected <T> void writeWithMessageConverters(T returnValue, MethodParameter returnType,
		ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
		throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {

	Class<?> returnValueClass = getReturnValueType(returnValue, returnType);
	Type returnValueType = getGenericType(returnType);
	HttpServletRequest servletRequest = inputMessage.getServletRequest();
	List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(servletRequest);
	List<MediaType> producibleMediaTypes = getProducibleMediaTypes(servletRequest, returnValueClass, returnValueType);

	if (returnValue != null && producibleMediaTypes.isEmpty()) {
		throw new IllegalArgumentException("No converter found for return value of type: " + returnValueClass);
	}

	Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
	for (MediaType requestedType : requestedMediaTypes) {
		for (MediaType producibleType : producibleMediaTypes) {
			if (requestedType.isCompatibleWith(producibleType)) {
				compatibleMediaTypes.add(getMostSpecificMediaType(requestedType, producibleType));
			}
		}
	}
	if (compatibleMediaTypes.isEmpty()) {
		if (returnValue != null) {
			throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes);
		}
		return;
	}

	List<MediaType> mediaTypes = new ArrayList<MediaType>(compatibleMediaTypes);
	MediaType.sortBySpecificityAndQuality(mediaTypes);

	MediaType selectedMediaType = null;
	for (MediaType mediaType : mediaTypes) {
		if (mediaType.isConcrete()) {
			selectedMediaType = mediaType;
			break;
		}
		else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION)) {
			selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
			break;
		}
	}

	if (selectedMediaType != null) {
		selectedMediaType = selectedMediaType.removeQualityValue();
		for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
			if (messageConverter instanceof GenericHttpMessageConverter) {
				if (((GenericHttpMessageConverter<T>) messageConverter).canWrite(returnValueType,
						returnValueClass, selectedMediaType)) {
					returnValue = (T) getAdvice().beforeBodyWrite(returnValue, returnType, selectedMediaType,
							(Class<? extends HttpMessageConverter<?>>) messageConverter.getClass(),
							inputMessage, outputMessage);
					if (returnValue != null) {
						addContentDispositionHeader(inputMessage, outputMessage);
						((GenericHttpMessageConverter<T>) messageConverter).write(returnValue,
								returnValueType, selectedMediaType, outputMessage);
						if (logger.isDebugEnabled()) {
							logger.debug("Written [" + returnValue + "] as \"" +
									selectedMediaType + "\" using [" + messageConverter + "]");
						}
					}
					return;
				}
			}
			else if (messageConverter.canWrite(returnValueClass, selectedMediaType)) {
				returnValue = (T) getAdvice().beforeBodyWrite(returnValue, returnType, selectedMediaType,
						(Class<? extends HttpMessageConverter<?>>) messageConverter.getClass(),
						inputMessage, outputMessage);
				if (returnValue != null) {
					addContentDispositionHeader(inputMessage, outputMessage);
					((HttpMessageConverter<T>) messageConverter).write(returnValue,
							selectedMediaType, outputMessage);
					if (logger.isDebugEnabled()) {
						logger.debug("Written [" + returnValue + "] as \"" +
								selectedMediaType + "\" using [" + messageConverter + "]");
					}
				}
				return;
			}
		}
	}

	if (returnValue != null) {
		throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
	}
}