0

I have a Java based server application using maven for dependency management and a tomcat server to host it. I have a bunch of IoT devices sending different kinds of payloads all the time. Please note the payloads vary in sizes from 1 MB to 80 MB. So, I want to track the usage of data by each device.

Instead of writing payload size checks at every single API, can I write a filter or interceptor to examine payloads for all API requests? Also, if the payload is higher than 80 MB I want to reject the payload.

Please let me know if there's anyway to accomplish this.

1 Answers1

0

You can do this with a Filter, something like this:

public class PayLoadCheckFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        if (request.getContentLength() > 80e+07) {
            ((HttpServletResponse) response).setStatus(503);
        }
        else {
            filterChain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }

See here for how to add the filter to the web.xml.

Community
  • 1
  • 1
Essex Boy
  • 7,565
  • 2
  • 21
  • 24