Hello everyone,

If this is not the right place to post this could you please let me know where. I have searched the forums and Google and cannot find an answer.

I have a Servlet filter that checks to see the content length of the request.

long contentLength = request.getContentLength();

If contentLength is greater than some configurable size, I would like to stop the request and send back a response saying that the request is to large.

This is being used for a file upload Everything is working, except it seems like the ENTIRE request (or file) is sent BEFORE my redirect is happening.

My logs are showing the correct information at the appropriate times, but the entire request is being processed before I can send a request back. Is there a way to immediately cut off the request and return a response? Is there a better way to do this?

Request Size Filter: REQUEST LENGTH=401458785
Request Size Filter: CONTENT TYPE=multipart/form-data; boundary=---------------------------85421569618919
Request Size Filter: REQUEST = /jaxpathwi/imageAction.do
Request Size Filter: REFERER = http://deva2231:8080/jaxpathwi/imageAction.do?dispatch=setup&mouseKey=1&diagnosisKey=1
Request Size Filter: Request is to be filtered and checked for size
Request Size Filter: REQUEST LENGTH [401458785] >= ALLOWABLE CONTENT LENGTH [5000000]
Request Size Filter: NOT Allowing request
Request Size Filter: Redirecting to -> http://deva2231.8080/jaxpathwi/imageAction.do?dispatch=setup&mouseKey=1&diagnosisKey=1

I have posted a modified version of my code to show you.

Thanks!

Matt

<code>
package filters;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

package filters;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RequestSizeFilter implements Filter {
     public static final String REDIRECT = "redirect";
  public static final String LENGTH = "length";
     private FilterConfig filterConfig = null;
  private static boolean bInitialized = false;
  private String strRedirectURL = null;
  private long lContentLength = -1l;
  private boolean bRedirect = false;

  public void init(FilterConfig config)
      throws ServletException {
      this.filterConfig = config;
      ServletContext servletContext = filterConfig.getServletContext();
             String strRedirect = filterConfig.getInitParameter(REDIRECT);
      String strLength = filterConfig.getInitParameter(LENGTH);
             if ((strLength != null) && (strLength.length() > 0)) {
          bInitialized = true;
      }

      // configure the redirect URL
      if ((strRedirect != null) && (strRedirect.length() > 0)) {
          strRedirectURL = new String(strRedirect);

          // configure redirect or forward
          bRedirect = strRedirect.startsWith("HTTP://") ||
                      strRedirect.startsWith("HTTPS://") ||
                      strRedirect.startsWith("http://";) ||
                      strRedirect.startsWith("https://";);
      } else {
          bRedirect = true;
      }
             // configure the content length
      try {
          lContentLength = Long.parseLong(strLength);
      } catch (NumberFormatException nfe) {
          bInitialized = false;
          nfe.printStackTrace();
      }
  }

  public void destroy() {
      this.filterConfig = null;
  }
public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain chain)
      throws IOException, ServletException {
             long lRequestLength = request.getContentLength();
String strRequestURI = ((HttpServletRequest)request).getRequestURI(); String strReferer = ((HttpServletRequest)request).getHeader("Referer");
             log("REQUEST = " + strRequestURI);
      log("REFERER = " + strReferer);
             String strURL = strReferer;
      if ((strRedirectURL != null) && (strRedirectURL.length() > 0)) {
          strURL = strRedirectURL;
      }
             if (lRequestLength >= lContentLength) {
          if(bRedirect) {
HttpServletResponse httpResponse = (HttpServletResponse)response;
              httpResponse.sendRedirect(strURL);
          } else {
              RequestDispatcher dispatcher =
filterConfig.getServletContext().getRequestDispatcher(strURL);
              dispatcher.forward(request, response);
          }
      } else {
          chain.doFilter(request, response);           }
  }
     private void log(String strMessage) {
      if (strMessage != null) {
          StringBuffer sb = new StringBuffer("Request Size Filter: ");
          sb.append(strMessage);
          this.filterConfig.getServletContext().log(sb.toString());
          System.out.println(sb.toString());
          sb = null;
      }
  }
}
</code>





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to