Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Cancel HttpServletRequest using a Filter

Reply
Thread Tools

Cancel HttpServletRequest using a Filter

 
 
Tim
Guest
Posts: n/a
 
      10-15-2004
I'm trying to stop a file upload from a form post if the file size is
too large, but I need to do it as early as possible in the upload
process.

When exactly does the Filter kick in? Does the file upload have to
complete before the filter?

Here's how things are laid out:
1. JSP form posts multipart request to server
2. Filter kicks in and looks at the content-length header
3. If content-length exceeds limit, redirect to a URL

See my code snippet below:

public void doFilter(ServletRequest req,ServletResponse
res,FilterChain chain) throws IOException,ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

int contentLength = request.getContentLength();
String contentType = request.getContentType();

try {
// if this is a multipart (file upload) request
if (contentLength > 0 && contentType != null
&& contentType.startsWith("multipart/form-data")) {
// compare content-length to max file size
if (!acceptFile(request, contentLength)){
response.sendRedirect(response.encodeRedirectURL(E RROR_URI));
return;
}
}

chain.doFilter(request, response);
return;

} catch (Exception ne) {
throw new ServletException(ne);
}
}
}
 
Reply With Quote
 
 
 
 
Arvind
Guest
Posts: n/a
 
      10-16-2004
(Tim) wrote in message news:<. com>...
> I'm trying to stop a file upload from a form post if the file size is
> too large, but I need to do it as early as possible in the upload
> process.
>
> When exactly does the Filter kick in? Does the file upload have to
> complete before the filter?
>
> Here's how things are laid out:
> 1. JSP form posts multipart request to server
> 2. Filter kicks in and looks at the content-length header
> 3. If content-length exceeds limit, redirect to a URL
>
> See my code snippet below:
>
> public void doFilter(ServletRequest req,ServletResponse
> res,FilterChain chain) throws IOException,ServletException {
> HttpServletRequest request = (HttpServletRequest) req;
> HttpServletResponse response = (HttpServletResponse) res;
>
> int contentLength = request.getContentLength();
> String contentType = request.getContentType();
>
> try {
> // if this is a multipart (file upload) request
> if (contentLength > 0 && contentType != null
> && contentType.startsWith("multipart/form-data")) {
> // compare content-length to max file size
> if (!acceptFile(request, contentLength)){
> response.sendRedirect(response.encodeRedirectURL(E RROR_URI));
> return;
> }
> }
>
> chain.doFilter(request, response);
> return;
>
> } catch (Exception ne) {
> throw new ServletException(ne);
> }
> }
> }


If you did not know, HttpServers/WebServer typically support the
ability to configure the size of upload permissible. (e.g. IBM Http
Server)

--
Arvind
 
Reply With Quote
 
 
 
 
Tim
Guest
Posts: n/a
 
      10-18-2004
Thanks for your reply Arvind.

I do know that. And Struts (which I am using as a framework) can also
configure a maximum upload size.

But I need to determine the maximum size dynamically at runtime.

For example, if the user has logged in and is a registered user, their
upload limit will be 20 MB, otherwise it will be 4 MB. So a static
configuration setting just won't do.

Any other thoughts?


(Arvind) wrote in message news:< om>...
> (Tim) wrote in message news:<. com>...
> > I'm trying to stop a file upload from a form post if the file size is
> > too large, but I need to do it as early as possible in the upload
> > process.
> >
> > When exactly does the Filter kick in? Does the file upload have to
> > complete before the filter?
> >
> > Here's how things are laid out:
> > 1. JSP form posts multipart request to server
> > 2. Filter kicks in and looks at the content-length header
> > 3. If content-length exceeds limit, redirect to a URL
> >
> > See my code snippet below:
> >
> > public void doFilter(ServletRequest req,ServletResponse
> > res,FilterChain chain) throws IOException,ServletException {
> > HttpServletRequest request = (HttpServletRequest) req;
> > HttpServletResponse response = (HttpServletResponse) res;
> >
> > int contentLength = request.getContentLength();
> > String contentType = request.getContentType();
> >
> > try {
> > // if this is a multipart (file upload) request
> > if (contentLength > 0 && contentType != null
> > && contentType.startsWith("multipart/form-data")) {
> > // compare content-length to max file size
> > if (!acceptFile(request, contentLength)){
> > response.sendRedirect(response.encodeRedirectURL(E RROR_URI));
> > return;
> > }
> > }
> >
> > chain.doFilter(request, response);
> > return;
> >
> > } catch (Exception ne) {

> throw new ServletException(ne);
> > }
> > }
> > }

>
> If you did not know, HttpServers/WebServer typically support the
> ability to configure the size of upload permissible. (e.g. IBM Http
> Server)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP CANCEL CANCEL CANCEL Carmen Rosario Computer Support 7 04-06-2005 11:04 PM
Modify HTTPServletRequest Matt Pokress Java 1 11-05-2003 08:28 PM
HttpServletRequest.getRequestURL() for domain name? Larry S Java 0 09-07-2003 04:53 PM
How to get HttpSession object in servlet without HttpServletRequest object Jim Java 3 08-26-2003 02:26 PM
[Q] HttpServletRequest.getCookies() Kaspar Minosiants Java 2 07-09-2003 09:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57