Java Code Examples for org.springframework.http.MediaType#isWildcardType()

The following examples show how to use org.springframework.http.MediaType#isWildcardType() . 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: AbstractHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Add default headers to the output message.
 * <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a content
 * type was not provided, calls {@link #getContentLength}, and sets the corresponding headers
 * @since 4.2
 */
protected void addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType) throws IOException{
	if (headers.getContentType() == null) {
		MediaType contentTypeToUse = contentType;
		if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
			contentTypeToUse = getDefaultContentType(t);
		}
		else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
			MediaType mediaType = getDefaultContentType(t);
			contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
		}
		if (contentTypeToUse != null) {
			headers.setContentType(contentTypeToUse);
		}
	}
	if (headers.getContentLength() == -1) {
		Long contentLength = getContentLength(t, headers.getContentType());
		if (contentLength != null) {
			headers.setContentLength(contentLength);
		}
	}
}
 
Example 2
Source File: BufferedImageHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private MediaType getContentType(@Nullable MediaType contentType) {
	if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
		contentType = getDefaultContentType();
	}
	Assert.notNull(contentType, "Could not select Content-Type. " +
			"Please specify one through the 'defaultContentType' property.");
	return contentType;
}
 
Example 3
Source File: BufferedImageHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private MediaType getContentType(@Nullable MediaType contentType) {
	if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
		contentType = getDefaultContentType();
	}
	Assert.notNull(contentType, "Could not select Content-Type. " +
			"Please specify one through the 'defaultContentType' property.");
	return contentType;
}
 
Example 4
Source File: BufferedImageHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private MediaType getContentType(MediaType contentType) {
	if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
		contentType = getDefaultContentType();
	}
	Assert.notNull(contentType, "Could not select Content-Type. " +
			"Please specify one through the 'defaultContentType' property.");
	return contentType;
}
 
Example 5
Source File: BufferedImageHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void writeInternal(BufferedImage image, MediaType contentType, HttpHeaders headers,
		OutputStream body) throws IOException, HttpMessageNotWritableException {

	if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
		contentType = getDefaultContentType();
	}
	Assert.notNull(contentType,
			"Count not determine Content-Type, set one using the 'defaultContentType' property");
	headers.setContentType(contentType);
	ImageOutputStream imageOutputStream = null;
	ImageWriter imageWriter = null;
	try {
		Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(contentType.toString());
		if (imageWriters.hasNext()) {
			imageWriter = imageWriters.next();
			ImageWriteParam iwp = imageWriter.getDefaultWriteParam();
			process(iwp);
			imageOutputStream = createImageOutputStream(body);
			imageWriter.setOutput(imageOutputStream);
			imageWriter.write(null, new IIOImage(image, null, null), iwp);
		}
		else {
			throw new HttpMessageNotWritableException(
					"Could not find javax.imageio.ImageWriter for Content-Type [" + contentType + "]");
		}
	}
	finally {
		if (imageWriter != null) {
			imageWriter.dispose();
		}
		if (imageOutputStream != null) {
			try {
				imageOutputStream.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}
}