1

I work with spring-ws 4.3.3 and I would like to know if it's possible to get the real content-size for a soap request that contains a datahandler parameter.

The following code works well if the request size seems to be less than 4096 bytes otherwise if content-size is greater than 4096, requestSize equals -1. However in the javadoc it's written :

Returns the length, in bytes, of the request body and made available by * the input stream, or -1 if the length is not known ir is greater than * Integer.MAX_VALUE

In my example I try to generate an error message if the soap request exceeded 51200000 but if my request is greater than 4096 the error appears.

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>

Thanks

Nicolas
  • 69
  • 3

2 Answers2

0

Requests of that size typically use HTTP chunking and the content length isn't known in advance, i.e. getContentLength() will return -1. To disallow requests exceeding a certain size, you would either configure your application server (if it has an option to limit the request size) or install a servlet filter that triggers an error after a certain number of bytes have been read from the request.

Andreas Veithen
  • 8,868
  • 3
  • 25
  • 28
0

Thanks you Andreas for opening my minds

I found a solution

/**
 * 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;
}
Nicolas
  • 69
  • 3