Support for ServletRequest version 2

Issue #114 resolved
ooxaam created an issue

I'm using ServletRequest version 2.5 and I'm unable to run Swagger validation filter. As "SwaggerValidationFilter" is using function

servletRequest.getContentLengthLong()

which is available in Servlet version 3.0 and above.

So can you please guide me any work around or over haul this issue so that Swagger works fine with Servlet version 2.0.

Thanks.

Comments (6)

  1. Sven Döring

    The SwaggerValidationFilter simply wraps incoming requests into the ResettableRequestServletWrapper. This filter does nothing more.

    You can try copying that filter and use getContentLength() instead of getContentLengthLong() on your version of the filter. On the Spring configuration simply register your filter.

    Let me hear from you if it works.

    Edit: But be aware. If you send requests having more than ~2.1GB of content the Spring MVC adapter will throw an exception.

  2. Sven Döring

    Could you please try this filter?

    import java.io.IOException;
    
    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.web.cors.CorsUtils;
    import org.springframework.web.filter.OncePerRequestFilter;
    
    public class SwaggerValidationCustomFilter extends OncePerRequestFilter {
    
        @Override
        protected void doFilterInternal(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final FilterChain filterChain)
                throws ServletException, IOException {
            final HttpServletRequest requestToUse = wrapValidatableServletRequest(servletRequest);
            filterChain.doFilter(requestToUse, servletResponse);
        }
    
        private HttpServletRequest wrapValidatableServletRequest(final HttpServletRequest servletRequest) {
            // wrap only validatable requests
            final long contentLengthLong = getContentLength(servletRequest);
            final boolean doValidationStep = contentLengthLong <= Integer.MAX_VALUE &&
                    !CorsUtils.isPreFlightRequest(servletRequest);
            return doValidationStep ? new ResettableRequestServletWrapper(servletRequest) : servletRequest;
        }
    
        private long getContentLength(final HttpServletRequest servletRequest) {
            final String contentLength = servletRequest.getHeader("content-length");
            if (StringUtils.isNotBlank(contentLength)) {
                try {
                    return Long.parseLong(contentLength);
                } catch (final NumberFormatException e) {
                    // either no content-length was set or the content-length exceeded Long.MAX_VALUE
                }
            }
            return -1L;
        }
    }
    

    If this works, I'll add it to the source code. This should work for ServletRequest v2 and v3.

  3. ooxaam reporter

    lols :D I have been using this file for around more than 6 months and its working perfectly fine 😃 👍

    I have used the recommended approach and it worked for me .

    Thanks

  4. Log in to comment