下面列出了org.springframework.http.MediaType#includes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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;
}
@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;
}
/**
* 针对下面这个比较恶心的魔法逻辑的说明:
* 由于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();
}
private int indexOfIncludedMediaType(MediaType mediaType) {
for (int i = 0; i < getExpressionsToCompare().size(); i++) {
if (mediaType.includes(getExpressionsToCompare().get(i).getMediaType())) {
return i;
}
}
return -1;
}
@Override
public byte[] modifyContent(byte[] originalContent, MediaType contentType) {
if (originalContent.length > 0 && contentType != null
&& contentType.includes(APPLICATION_JSON)) {
return modifyContent(originalContent);
} else {
return originalContent;
}
}
/**
* 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;
}
private int indexOfIncludedMediaType(MediaType mediaType) {
for (int i = 0; i < getExpressionsToCompare().size(); i++) {
if (mediaType.includes(getExpressionsToCompare().get(i).getMediaType())) {
return i;
}
}
return -1;
}
private int indexOfIncludedMediaType(MediaType mediaType) {
for (int i = 0; i < getExpressionsToCompare().size(); i++) {
if (mediaType.includes(getExpressionsToCompare().get(i).getMediaType())) {
return i;
}
}
return -1;
}
/**
* 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;
}
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;
}
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;
}
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();
}
/**
* 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;
}
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;
}
private int indexOfIncludedMediaType(MediaType mediaType) {
for (int i = 0; i < getExpressionsToCompare().size(); i++) {
if (mediaType.includes(getExpressionsToCompare().get(i).getMediaType())) {
return i;
}
}
return -1;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}