Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
91a75ed7
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
91a75ed7
编写于
3月 17, 2017
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish EncoderHttpMessageWriter Content-Type handling
上级
ad91b942
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
24 addition
and
34 deletion
+24
-34
spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java
.../springframework/http/codec/EncoderHttpMessageWriter.java
+24
-28
spring-web/src/test/java/org/springframework/http/codec/ResourceRegionHttpMessageWriterTests.java
...work/http/codec/ResourceRegionHttpMessageWriterTests.java
+0
-6
未找到文件。
spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java
浏览文件 @
91a75ed7
...
...
@@ -49,6 +49,8 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
private
final
List
<
MediaType
>
mediaTypes
;
private
final
MediaType
defaultMediaType
;
/**
* Create an instance wrapping the given {@link Encoder}.
...
...
@@ -57,6 +59,11 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
Assert
.
notNull
(
encoder
,
"Encoder is required"
);
this
.
encoder
=
encoder
;
this
.
mediaTypes
=
MediaType
.
asMediaTypes
(
encoder
.
getEncodableMimeTypes
());
this
.
defaultMediaType
=
initDefaultMediaType
(
this
.
mediaTypes
);
}
private
static
MediaType
initDefaultMediaType
(
List
<
MediaType
>
mediaTypes
)
{
return
mediaTypes
.
stream
().
filter
(
MediaType:
:
isConcrete
).
findFirst
().
orElse
(
null
);
}
...
...
@@ -84,23 +91,13 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
Map
<
String
,
Object
>
hints
)
{
HttpHeaders
headers
=
outputMessage
.
getHeaders
();
if
(
headers
.
getContentType
()
==
null
)
{
MediaType
contentTypeToUse
=
mediaType
;
if
(
mediaType
==
null
||
mediaType
.
isWildcardType
()
||
mediaType
.
isWildcardSubtype
())
{
contentTypeToUse
=
getDefaultContentType
(
elementType
);
}
else
if
(
MediaType
.
APPLICATION_OCTET_STREAM
.
equals
(
mediaType
))
{
MediaType
contentType
=
getDefaultContentType
(
elementType
);
contentTypeToUse
=
(
contentType
!=
null
?
contentType
:
contentTypeToUse
);
}
if
(
contentTypeToUse
!=
null
)
{
if
(
contentTypeToUse
.
getCharset
()
==
null
)
{
MediaType
contentType
=
getDefaultContentType
(
elementType
);
if
(
contentType
!=
null
&&
contentType
.
getCharset
()
!=
null
)
{
contentTypeToUse
=
new
MediaType
(
contentTypeToUse
,
contentType
.
getCharset
());
}
}
headers
.
setContentType
(
contentTypeToUse
);
MediaType
fallback
=
this
.
defaultMediaType
;
MediaType
selected
=
useFallback
(
mediaType
,
fallback
)
?
fallback
:
mediaType
;
if
(
selected
!=
null
)
{
selected
=
addDefaultCharset
(
selected
,
fallback
);
headers
.
setContentType
(
selected
);
}
}
...
...
@@ -110,18 +107,17 @@ public class EncoderHttpMessageWriter<T> implements HttpMessageWriter<T> {
outputMessage
.
writeAndFlushWith
(
body
.
map
(
Flux:
:
just
))
:
outputMessage
.
writeWith
(
body
));
}
/**
* Return the default content type for the given {@code ResolvableType}.
* Used when {@link #write} is called without a concrete content type.
*
* <p>By default returns the first of {@link Encoder#getEncodableMimeTypes()
* encodableMimeTypes} that is concrete({@link MediaType#isConcrete()}), if any.
*
* @param elementType the type of element for encoding
* @return the content type, or {@code null}
*/
protected
MediaType
getDefaultContentType
(
ResolvableType
elementType
)
{
return
this
.
mediaTypes
.
stream
().
filter
(
MediaType:
:
isConcrete
).
findFirst
().
orElse
(
null
);
private
static
boolean
useFallback
(
MediaType
main
,
MediaType
fallback
)
{
return
main
==
null
||
!
main
.
isConcrete
()
||
main
.
equals
(
MediaType
.
APPLICATION_OCTET_STREAM
)
&&
fallback
!=
null
;
}
private
static
MediaType
addDefaultCharset
(
MediaType
main
,
MediaType
defaultType
)
{
if
(
main
.
getCharset
()
==
null
&&
defaultType
!=
null
&&
defaultType
.
getCharset
()
!=
null
)
{
return
new
MediaType
(
main
,
defaultType
.
getCharset
());
}
return
main
;
}
}
spring-web/src/test/java/org/springframework/http/codec/ResourceRegionHttpMessageWriterTests.java
浏览文件 @
91a75ed7
...
...
@@ -66,12 +66,6 @@ public class ResourceRegionHttpMessageWriterTests {
}
@Test
public
void
defaultContentType
()
throws
Exception
{
assertEquals
(
MimeTypeUtils
.
APPLICATION_OCTET_STREAM
,
this
.
writer
.
getDefaultContentType
(
ResolvableType
.
forClass
(
ResourceRegion
.
class
)));
}
@Test
public
void
writableMediaTypes
()
throws
Exception
{
assertThat
(
this
.
writer
.
getWritableMediaTypes
(),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录