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

The following examples show how to use org.springframework.http.MediaType#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: FormHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
	if (!MultiValueMap.class.isAssignableFrom(clazz)) {
		return false;
	}
	if (mediaType == null) {
		return true;
	}
	for (MediaType supportedMediaType : getSupportedMediaTypes()) {
		// We can't read multipart....
		if (!supportedMediaType.equals(MediaType.MULTIPART_FORM_DATA) && supportedMediaType.includes(mediaType)) {
			return true;
		}
	}
	return false;
}
 
Example 2
Source File: WxApiHttpRequest.java    From FastBootWeixin with Apache License 2.0 6 votes vote down vote up
/**
 * 针对下面这个比较恶心的魔法逻辑的说明:
 * 由于Spring5在对ContentType是MULTIPART_FORM_DATA的头处理时自动添加了charset参数。
 * see org.springframework.http.converter.FormHttpMessageConverter.writeMultipart
 * 如果设置了multipartCharset,则不会添加charset参数。
 * 但是在获取filename时又去判断如果设置了multipartCharset,则调用MimeDelegate.encode(filename, this.multipartCharset.name())
 * 对filename进行encode,但是MimeDelegate调用了javax.mail.internet.MimeUtility这个类,这个类有可能没有被依赖进来,不是强依赖的
 * 故会导致一样的报错。实在没办法切入源码了,故加入下面这个逻辑。
 * 在Spring4中在multipartCharset为空时,也不会自动添加charset,故没有此问题。
 * 而根本原因是微信的服务器兼容性太差了,header中的charset是有http标准规定的,竟然不兼容!
 *
 * @return OutputStream
 * @throws IOException
 */
@Override
public OutputStream getBody() throws IOException {
    HttpHeaders httpHeaders = this.delegate.getHeaders();
    MediaType contentType = httpHeaders.getContentType();
    if (contentType != null && contentType.includes(MediaType.MULTIPART_FORM_DATA)) {
        Map<String, String> parameters = contentType.getParameters();
        if (parameters.containsKey("charset")) {
            Map<String, String> newParameters = new LinkedHashMap<>(contentType.getParameters());
            newParameters.remove("charset");
            MediaType newContentType = new MediaType(MediaType.MULTIPART_FORM_DATA, newParameters);
            httpHeaders.setContentType(newContentType);
        }
    }
    return this.delegate.getBody();
}
 
Example 3
Source File: FormHttpMessageConverter.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
	if (!MultiValueMap.class.isAssignableFrom(clazz)) {
		return false;
	}
	if (mediaType == null) {
		return true;
	}
	for (MediaType supportedMediaType : getSupportedMediaTypes()) {
		// We can't read multipart....
		if (!supportedMediaType.equals(MediaType.MULTIPART_FORM_DATA) && supportedMediaType.includes(mediaType)) {
			return true;
		}
	}
	return false;
}
 
Example 4
Source File: FormHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
	if (!MultiValueMap.class.isAssignableFrom(clazz)) {
		return false;
	}
	if (mediaType == null) {
		return true;
	}
	for (MediaType supportedMediaType : getSupportedMediaTypes()) {
		// We can't read multipart....
		if (!supportedMediaType.equals(MediaType.MULTIPART_FORM_DATA) && supportedMediaType.includes(mediaType)) {
			return true;
		}
	}
	return false;
}
 
Example 5
Source File: JsonContentModifier.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] modifyContent(byte[] originalContent, MediaType contentType) {
    if (originalContent.length > 0 && contentType != null
            && contentType.includes(APPLICATION_JSON)) {
        return modifyContent(originalContent);
    } else {
        return originalContent;
    }
}
 
Example 6
Source File: AbstractHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if any of the {@linkplain #setSupportedMediaTypes(List)
 * supported} media types {@link MediaType#includes(MediaType) include} the
 * given media type.
 * @param mediaType the media type to read, can be {@code null} if not specified.
 * Typically the value of a {@code Content-Type} header.
 * @return {@code true} if the supported media types include the media type,
 * or if the media type is {@code null}
 */
protected boolean canRead(MediaType mediaType) {
	if (mediaType == null) {
		return true;
	}
	for (MediaType supportedMediaType : getSupportedMediaTypes()) {
		if (supportedMediaType.includes(mediaType)) {
			return true;
		}
	}
	return false;
}
 
Example 7
Source File: ProducesRequestCondition.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private int indexOfIncludedMediaType(MediaType mediaType) {
	for (int i = 0; i < getExpressionsToCompare().size(); i++) {
		if (mediaType.includes(getExpressionsToCompare().get(i).getMediaType())) {
			return i;
		}
	}
	return -1;
}
 
Example 8
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private int indexOfIncluded(List<MediaType> infoAccepts, MediaType requestAccept) {
	for (int i = 0; i < infoAccepts.size(); i++) {
		MediaType info1Accept = infoAccepts.get(i);
		if (requestAccept.includes(info1Accept)) {
			return i;
		}
	}
	return -1;
}
 
Example 9
Source File: AbstractHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns {@code true} if any of the {@linkplain #setSupportedMediaTypes(List)
 * supported} media types {@link MediaType#includes(MediaType) include} the
 * given media type.
 * @param mediaType the media type to read, can be {@code null} if not specified.
 * Typically the value of a {@code Content-Type} header.
 * @return {@code true} if the supported media types include the media type,
 * or if the media type is {@code null}
 */
protected boolean canRead(MediaType mediaType) {
	if (mediaType == null) {
		return true;
	}
	for (MediaType supportedMediaType : getSupportedMediaTypes()) {
		if (supportedMediaType.includes(mediaType)) {
			return true;
		}
	}
	return false;
}
 
Example 10
Source File: AdviceTraits.java    From problem-spring-web with MIT License 5 votes vote down vote up
public static Optional<MediaType> getProblemMediaType(final List<MediaType> mediaTypes) {
    for (final MediaType mediaType : mediaTypes) {
        if (mediaType.includes(APPLICATION_JSON) || mediaType.includes(PROBLEM)) {
            return Optional.of(PROBLEM);
        } else if (mediaType.includes(X_PROBLEM)) {
            return Optional.of(X_PROBLEM);
        }
    }

    return Optional.empty();
}
 
Example 11
Source File: AnnotationMethodHandlerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private int indexOfIncluded(List<MediaType> infoAccepts, MediaType requestAccept) {
	for (int i = 0; i < infoAccepts.size(); i++) {
		MediaType info1Accept = infoAccepts.get(i);
		if (requestAccept.includes(info1Accept)) {
			return i;
		}
	}
	return -1;
}
 
Example 12
Source File: MetricsResponse.java    From timely with Apache License 2.0 5 votes vote down vote up
public FullHttpResponse toHttpResponse(String acceptHeader) throws Exception {
    MediaType negotiatedType = MediaType.TEXT_HTML;
    if (null != acceptHeader) {
        List<MediaType> requestedTypes = MediaType.parseMediaTypes(acceptHeader);
        MediaType.sortBySpecificityAndQuality(requestedTypes);
        LOG.trace("Acceptable response types: {}", MediaType.toString(requestedTypes));
        for (MediaType t : requestedTypes) {
            if (t.includes(MediaType.TEXT_HTML)) {
                negotiatedType = MediaType.TEXT_HTML;
                LOG.trace("{} allows HTML", t.toString());
                break;
            }
            if (t.includes(MediaType.APPLICATION_JSON)) {
                negotiatedType = MediaType.APPLICATION_JSON;
                LOG.trace("{} allows JSON", t.toString());
                break;
            }
        }
    }
    byte[] buf = null;
    Object responseType = Constants.HTML_TYPE;
    if (negotiatedType.equals(MediaType.APPLICATION_JSON)) {
        buf = this.generateJson(JsonUtil.getObjectMapper()).getBytes(StandardCharsets.UTF_8);
        responseType = Constants.JSON_TYPE;
    } else {
        buf = this.generateHtml().toString().getBytes(StandardCharsets.UTF_8);
    }
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            Unpooled.copiedBuffer(buf));
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, responseType);
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return response;
}
 
Example 13
Source File: AbstractHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Returns {@code true} if any of the {@linkplain #setSupportedMediaTypes(List)
 * supported} media types {@link MediaType#includes(MediaType) include} the
 * given media type.
 * @param mediaType the media type to read, can be {@code null} if not specified.
 * Typically the value of a {@code Content-Type} header.
 * @return {@code true} if the supported media types include the media type,
 * or if the media type is {@code null}
 */
protected boolean canRead(@Nullable MediaType mediaType) {
	if (mediaType == null) {
		return true;
	}
	for (MediaType supportedMediaType : getSupportedMediaTypes()) {
		if (supportedMediaType.includes(mediaType)) {
			return true;
		}
	}
	return false;
}
 
Example 14
Source File: ProducesRequestCondition.java    From java-technology-stack with MIT License 5 votes vote down vote up
private int indexOfIncludedMediaType(MediaType mediaType) {
	for (int i = 0; i < getExpressionsToCompare().size(); i++) {
		if (mediaType.includes(getExpressionsToCompare().get(i).getMediaType())) {
			return i;
		}
	}
	return -1;
}
 
Example 15
Source File: ProducesRequestCondition.java    From java-technology-stack with MIT License 5 votes vote down vote up
private int indexOfIncludedMediaType(MediaType mediaType) {
	for (int i = 0; i < getExpressionsToCompare().size(); i++) {
		if (mediaType.includes(getExpressionsToCompare().get(i).getMediaType())) {
			return i;
		}
	}
	return -1;
}
 
Example 16
Source File: AbstractHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Returns {@code true} if any of the {@linkplain #setSupportedMediaTypes(List)
 * supported} media types {@link MediaType#includes(MediaType) include} the
 * given media type.
 * @param mediaType the media type to read, can be {@code null} if not specified.
 * Typically the value of a {@code Content-Type} header.
 * @return {@code true} if the supported media types include the media type,
 * or if the media type is {@code null}
 */
protected boolean canRead(@Nullable MediaType mediaType) {
	if (mediaType == null) {
		return true;
	}
	for (MediaType supportedMediaType : getSupportedMediaTypes()) {
		if (supportedMediaType.includes(mediaType)) {
			return true;
		}
	}
	return false;
}
 
Example 17
Source File: ProducesRequestCondition.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private int indexOfIncludedMediaType(MediaType mediaType) {
	for (int i = 0; i < getExpressionsToCompare().size(); i++) {
		if (mediaType.includes(getExpressionsToCompare().get(i).getMediaType())) {
			return i;
		}
	}
	return -1;
}
 
Example 18
Source File: ServletAnnotationMappingUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Check whether the given request matches the specified header conditions.
 * @param headers the header conditions, following
 * {@link org.springframework.web.bind.annotation.RequestMapping#headers() RequestMapping.headers()}
 * @param request the current HTTP request to check
 */
public static boolean checkHeaders(String[] headers, HttpServletRequest request) {
	if (!ObjectUtils.isEmpty(headers)) {
		for (String header : headers) {
			int separator = header.indexOf('=');
			if (separator == -1) {
				if (header.startsWith("!")) {
					if (request.getHeader(header.substring(1)) != null) {
						return false;
					}
				}
				else if (request.getHeader(header) == null) {
					return false;
				}
			}
			else {
				boolean negated = (separator > 0 && header.charAt(separator - 1) == '!');
				String key = !negated ? header.substring(0, separator) : header.substring(0, separator - 1);
				String value = header.substring(separator + 1);
				if (isMediaTypeHeader(key)) {
					List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(key));
					List<MediaType> valueMediaTypes = MediaType.parseMediaTypes(value);
					boolean found = false;
					for (Iterator<MediaType> valIter = valueMediaTypes.iterator(); valIter.hasNext() && !found;) {
						MediaType valueMediaType = valIter.next();
						for (Iterator<MediaType> reqIter = requestMediaTypes.iterator();
								reqIter.hasNext() && !found;) {
							MediaType requestMediaType = reqIter.next();
							if (valueMediaType.includes(requestMediaType)) {
								found = true;
							}
						}

					}
					if (negated) {
						found = !found;
					}
					if (!found) {
						return false;
					}
				}
				else {
					boolean match = value.equals(request.getHeader(key));
					if (negated) {
						match = !match;
					}
					if (!match) {
						return false;
					}
				}
			}
		}
	}
	return true;
}
 
Example 19
Source File: PortletAnnotationMappingUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Check whether the given request matches the specified header conditions.
 * @param headers the header conditions, following {@link RequestMapping#headers()}
 * @param request the current HTTP request to check
 */
public static boolean checkHeaders(String[] headers, PortletRequest request) {
	if (!ObjectUtils.isEmpty(headers)) {
		for (String header : headers) {
			int separator = header.indexOf('=');
			if (separator == -1) {
				if (header.startsWith("!")) {
					if (request.getProperty(header.substring(1)) != null) {
						return false;
					}
				}
				else if (request.getProperty(header) == null) {
					return false;
				}
			}
			else {
				String key = header.substring(0, separator);
				String value = header.substring(separator + 1);
				if (isMediaTypeHeader(key)) {
					List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getProperty(key));
					List<MediaType> valueMediaTypes = MediaType.parseMediaTypes(value);
					boolean found = false;
					for (Iterator<MediaType> valIter = valueMediaTypes.iterator(); valIter.hasNext() && !found;) {
						MediaType valueMediaType = valIter.next();
						for (Iterator<MediaType> reqIter = requestMediaTypes.iterator(); reqIter.hasNext() && !found;) {
							MediaType requestMediaType = reqIter.next();
							if (valueMediaType.includes(requestMediaType)) {
								found = true;
							}
						}

					}
					if (!found) {
						return false;
					}
				}
				else if (!value.equals(request.getProperty(key))) {
					return false;
				}
			}
		}
	}
	return true;
}
 
Example 20
Source File: ServletAnnotationMappingUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Check whether the given request matches the specified header conditions.
 * @param headers the header conditions, following
 * {@link org.springframework.web.bind.annotation.RequestMapping#headers() RequestMapping.headers()}
 * @param request the current HTTP request to check
 */
public static boolean checkHeaders(String[] headers, HttpServletRequest request) {
	if (!ObjectUtils.isEmpty(headers)) {
		for (String header : headers) {
			int separator = header.indexOf('=');
			if (separator == -1) {
				if (header.startsWith("!")) {
					if (request.getHeader(header.substring(1)) != null) {
						return false;
					}
				}
				else if (request.getHeader(header) == null) {
					return false;
				}
			}
			else {
				boolean negated = (separator > 0 && header.charAt(separator - 1) == '!');
				String key = !negated ? header.substring(0, separator) : header.substring(0, separator - 1);
				String value = header.substring(separator + 1);
				if (isMediaTypeHeader(key)) {
					List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(key));
					List<MediaType> valueMediaTypes = MediaType.parseMediaTypes(value);
					boolean found = false;
					for (Iterator<MediaType> valIter = valueMediaTypes.iterator(); valIter.hasNext() && !found;) {
						MediaType valueMediaType = valIter.next();
						for (Iterator<MediaType> reqIter = requestMediaTypes.iterator();
								reqIter.hasNext() && !found;) {
							MediaType requestMediaType = reqIter.next();
							if (valueMediaType.includes(requestMediaType)) {
								found = true;
							}
						}

					}
					if (negated) {
						found = !found;
					}
					if (!found) {
						return false;
					}
				}
				else {
					boolean match = value.equals(request.getHeader(key));
					if (negated) {
						match = !match;
					}
					if (!match) {
						return false;
					}
				}
			}
		}
	}
	return true;
}