Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
35142424
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,发现更多精彩内容 >>
提交
35142424
编写于
1月 21, 2014
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SourceHttpMessageConverter's supports implementation needs to check for StAXSource
Issue: SPR-11341
上级
9cc86a3c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
29 addition
and
13 deletion
+29
-13
spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java
...mework/http/converter/xml/SourceHttpMessageConverter.java
+29
-13
未找到文件。
spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java
浏览文件 @
35142424
...
...
@@ -20,7 +20,8 @@ import java.io.ByteArrayInputStream;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.util.HashSet
;
import
java.util.Set
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
javax.xml.parsers.ParserConfigurationException
;
...
...
@@ -42,6 +43,7 @@ import org.xml.sax.InputSource;
import
org.xml.sax.SAXException
;
import
org.xml.sax.XMLReader
;
import
org.xml.sax.helpers.XMLReaderFactory
;
import
org.springframework.http.HttpInputMessage
;
import
org.springframework.http.HttpOutputMessage
;
import
org.springframework.http.MediaType
;
...
...
@@ -60,10 +62,22 @@ import org.springframework.util.StreamUtils;
*/
public
class
SourceHttpMessageConverter
<
T
extends
Source
>
extends
AbstractHttpMessageConverter
<
T
>
{
private
static
final
Set
<
Class
<?>>
SUPPORTED_CLASSES
=
new
HashSet
<
Class
<?>>(
5
);
static
{
SUPPORTED_CLASSES
.
add
(
DOMSource
.
class
);
SUPPORTED_CLASSES
.
add
(
SAXSource
.
class
);
SUPPORTED_CLASSES
.
add
(
StAXSource
.
class
);
SUPPORTED_CLASSES
.
add
(
StreamSource
.
class
);
SUPPORTED_CLASSES
.
add
(
Source
.
class
);
}
private
final
TransformerFactory
transformerFactory
=
TransformerFactory
.
newInstance
();
private
boolean
processExternalEntities
=
false
;
/**
* Sets the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes}
* to {@code text/xml} and {@code application/xml}, and {@code application/*-xml}.
...
...
@@ -74,18 +88,17 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
/**
* Indicates whether external XML entities are processed when converting
* to a Source.
* Indicates whether external XML entities are processed when converting to a Source.
* <p>Default is {@code false}, meaning that external entities are not resolved.
*/
public
void
setProcessExternalEntities
(
boolean
processExternalEntities
)
{
this
.
processExternalEntities
=
processExternalEntities
;
}
@Override
public
boolean
supports
(
Class
<?>
clazz
)
{
return
DOMSource
.
class
.
equals
(
clazz
)
||
SAXSource
.
class
.
equals
(
clazz
)
||
StreamSource
.
class
.
equals
(
clazz
)
||
Source
.
class
.
equals
(
clazz
);
return
SUPPORTED_CLASSES
.
contains
(
clazz
);
}
@Override
...
...
@@ -108,7 +121,7 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
}
else
{
throw
new
HttpMessageConversionException
(
"Could not read class ["
+
clazz
+
"]. Only DOMSource, SAXSource, and StreamSource are supported."
);
"]. Only DOMSource, SAXSource,
StAXSource,
and StreamSource are supported."
);
}
}
...
...
@@ -116,7 +129,8 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
try
{
DocumentBuilderFactory
documentBuilderFactory
=
DocumentBuilderFactory
.
newInstance
();
documentBuilderFactory
.
setNamespaceAware
(
true
);
documentBuilderFactory
.
setFeature
(
"http://xml.org/sax/features/external-general-entities"
,
processExternalEntities
);
documentBuilderFactory
.
setFeature
(
"http://xml.org/sax/features/external-general-entities"
,
this
.
processExternalEntities
);
DocumentBuilder
documentBuilder
=
documentBuilderFactory
.
newDocumentBuilder
();
Document
document
=
documentBuilder
.
parse
(
body
);
return
new
DOMSource
(
document
);
...
...
@@ -132,7 +146,8 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
private
SAXSource
readSAXSource
(
InputStream
body
)
throws
IOException
{
try
{
XMLReader
reader
=
XMLReaderFactory
.
createXMLReader
();
reader
.
setFeature
(
"http://xml.org/sax/features/external-general-entities"
,
processExternalEntities
);
reader
.
setFeature
(
"http://xml.org/sax/features/external-general-entities"
,
this
.
processExternalEntities
);
byte
[]
bytes
=
StreamUtils
.
copyToByteArray
(
body
);
return
new
SAXSource
(
reader
,
new
InputSource
(
new
ByteArrayInputStream
(
bytes
)));
}
...
...
@@ -144,7 +159,8 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
private
Source
readStAXSource
(
InputStream
body
)
{
try
{
XMLInputFactory
inputFactory
=
XMLInputFactory
.
newFactory
();
inputFactory
.
setProperty
(
"javax.xml.stream.isSupportingExternalEntities"
,
processExternalEntities
);
inputFactory
.
setProperty
(
"javax.xml.stream.isSupportingExternalEntities"
,
this
.
processExternalEntities
);
XMLStreamReader
streamReader
=
inputFactory
.
createXMLStreamReader
(
body
);
return
new
StAXSource
(
streamReader
);
}
...
...
@@ -192,21 +208,21 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
private
static
class
CountingOutputStream
extends
OutputStream
{
private
long
count
=
0
;
long
count
=
0
;
@Override
public
void
write
(
int
b
)
throws
IOException
{
count
++;
this
.
count
++;
}
@Override
public
void
write
(
byte
[]
b
)
throws
IOException
{
count
+=
b
.
length
;
this
.
count
+=
b
.
length
;
}
@Override
public
void
write
(
byte
[]
b
,
int
off
,
int
len
)
throws
IOException
{
count
+=
len
;
this
.
count
+=
len
;
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录