我使用 spring-ws 4.3.3,我想知道是否可以获取包含数据处理程序参数的 soap 请求的真实内容大小。
如果请求大小似乎小于 4096字节,则以下代码效果很好,否则如果内容大小大于 4096,则 requestSize 等于 -1。然而,在javadoc中它写着:
返回请求正文的长度(以字节为单位(,并由 * 输入流,如果长度未知,则为 -1 IR 大于 * 整数.MAX值
在我的示例中,如果 soap 请求超过 51200000,我尝试生成错误消息,但如果我的请求大于 4096,则会出现错误。
TransportContext tc = TransportContextHolder.getTransportContext();
HttpServletConnection connection = (HttpServletConnection) tc.getConnection();
Integer requestSize = connection.getHttpServletRequest().getContentLength();
if (requestSize==-1 || requestSize > 51200000) {
response.setStatus(getStatusResponse(PricingConstants.WS_FILE_SIZE_EXCEEDED_CODE,
PricingConstants.WS_FILE_SIZE_EXCEEDED_MSG));
return response;
XSD
<xs:complexType name="wsAddDocumentRequest">
<xs:sequence>
<xs:element name="callingAspect">
<xs:complexType>
<xs:sequence>
<xs:element name="userId" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Id" type="xs:string" />
<xs:element name="contentPath" type="xs:string" minOccurs="0" />
<xs:element name="file" type="xs:base64Binary" minOccurs="0"
xmime:expectedContentTypes="application/octet-stream" />
<xs:element name="document" type="prc:document" />
<xs:element name="authorizedUsers" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="user" type="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
谢谢
这种大小的请求通常使用 HTTP 分块,并且内容长度事先不知道,即 getContentLength()
将返回 -1。要禁止超过特定大小的请求,您可以配置应用程序服务器(如果它有限制请求大小的选项(或安装 Servlet 过滤器,以便在从请求中读取一定数量的字节后触发错误。
谢谢安德烈亚斯打开我的心扉
我找到了解决方案
/**
* Returns true if content size exceeds the max file size
*
* @param dataHandler content
* @param maxSize the max size allowed
* @return true if contentSize greater than maxSize, false otherwise
* @throws IOException
*/
public static boolean isTooBigContent(DataHandler dataHandler, long maxSize) throws IOException {
long contentSize = 0;
try (InputStream input = dataHandler.getInputStream()) {
int n;
while (IOUtils.EOF != (n = input.read(new byte[4096]))) {
contentSize += n;
if (Long.compare(contentSize, maxSize) > 0)
return true;
}
}
return false;
}