You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jan Ploski <jp...@gmx.de> on 2003/01/22 22:04:19 UTC

Altering the request URI in a filter through HttpServletRequestWrapper

Hello,

I would like to alter the request URI in my ServletFilter to intercept
requests to resources that should only be available to logged in users.

I have found a solution in the archive:

http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg64446.html

which basically suggests to terminate the doFilter method with

request.getRequestDispatcher(/* where to */).forward(request, response);

and don't invoke chain.doFilter as the last step.

It seems to me that it should not be done this way -- what if there
are other legitimate filters that should process the request?
Also, I am not sure about the semantics of calling "forward" from
"doFilter" -- is it officially supported or maybe a "grey area" of
the spec, and it works just by chance?

Anyway, I thought that I could achieve the same by using
a HttpServletRequestWrapper, like below:

    request = new HttpServletRequestWrapper(request) {
        public String getRequestURI() {
            return "/solonline/page/nosession.jsp"; }
        public StringBuffer getRequestURL() {
            return new StringBuffer(
                "http://192.168.0.1:6080/solonline/page/nosession.jsp"); }
        public String getServletPath() {
            return "/page/nosession.jsp"; }
        };

    chain.doFilter(request, response);

Unfortunately, the result is an error page:

---
HTTP Status 400 - Invalid path /page/nosession was requested

type Status report

message Invalid path /page/nosession was requested

description The request sent by the client was syntactically incorrect
(Invalid path /page/nosession was requested).
---

Note the ".jsp" stripped from my URI in this error message (?)

I am using Apache Tomcat/4.1.18-LE-jdk14.

Can someone explain why it does not work as expected? So far I have
not found a single example of HttpServletRequestWrapper's usage.

Best regards -
Jan Ploski


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Altering the request URI in a filter through HttpServletRequestWrapper

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Wed, 22 Jan 2003, Bill Barker wrote:

> Date: Wed, 22 Jan 2003 22:27:54 -0800
> From: Bill Barker <wb...@wilshire.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: Re: Altering the request URI in a filter through
>     HttpServletRequestWrapper
>
> I haven't looked to see why you are getting this particular error message,
> but the basic idea shouldn't work.  Before your Filters are invoked, Tomcat
> has already decided on which Servlet should handle the request at the end of
> the pipeline.  Changing the requestURI and servletPath at this stage
> shouldn't change the mapping.
>

That's correct -- if you want your filter to change which servlet is
actually invoked, using a RequestDispatcher.forward() is the way to go.

Craig


> "Jan Ploski" <jp...@gmx.de> wrote in message
> news:13799530.1043269459692.JavaMail.jpl@remotejava...
> > Hello,
> >
> > I would like to alter the request URI in my ServletFilter to intercept
> > requests to resources that should only be available to logged in users.
> >
> > I have found a solution in the archive:
> >
> > http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg64446.html
> >
> > which basically suggests to terminate the doFilter method with
> >
> > request.getRequestDispatcher(/* where to */).forward(request, response);
> >
> > and don't invoke chain.doFilter as the last step.
> >
> > It seems to me that it should not be done this way -- what if there
> > are other legitimate filters that should process the request?
> > Also, I am not sure about the semantics of calling "forward" from
> > "doFilter" -- is it officially supported or maybe a "grey area" of
> > the spec, and it works just by chance?
> >
> > Anyway, I thought that I could achieve the same by using
> > a HttpServletRequestWrapper, like below:
> >
> >     request = new HttpServletRequestWrapper(request) {
> >         public String getRequestURI() {
> >             return "/solonline/page/nosession.jsp"; }
> >         public StringBuffer getRequestURL() {
> >             return new StringBuffer(
> >                 "http://192.168.0.1:6080/solonline/page/nosession.jsp"); }
> >         public String getServletPath() {
> >             return "/page/nosession.jsp"; }
> >         };
> >
> >     chain.doFilter(request, response);
> >
> > Unfortunately, the result is an error page:
> >
> > ---
> > HTTP Status 400 - Invalid path /page/nosession was requested
> >
> > type Status report
> >
> > message Invalid path /page/nosession was requested
> >
> > description The request sent by the client was syntactically incorrect
> > (Invalid path /page/nosession was requested).
> > ---
> >
> > Note the ".jsp" stripped from my URI in this error message (?)
> >
> > I am using Apache Tomcat/4.1.18-LE-jdk14.
> >
> > Can someone explain why it does not work as expected? So far I have
> > not found a single example of HttpServletRequestWrapper's usage.
> >
> > Best regards -
> > Jan Ploski
>
>
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Altering the request URI in a filter through HttpServletRequestWrapper

Posted by Bill Barker <wb...@wilshire.com>.
I haven't looked to see why you are getting this particular error message,
but the basic idea shouldn't work.  Before your Filters are invoked, Tomcat
has already decided on which Servlet should handle the request at the end of
the pipeline.  Changing the requestURI and servletPath at this stage
shouldn't change the mapping.

"Jan Ploski" <jp...@gmx.de> wrote in message
news:13799530.1043269459692.JavaMail.jpl@remotejava...
> Hello,
>
> I would like to alter the request URI in my ServletFilter to intercept
> requests to resources that should only be available to logged in users.
>
> I have found a solution in the archive:
>
> http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg64446.html
>
> which basically suggests to terminate the doFilter method with
>
> request.getRequestDispatcher(/* where to */).forward(request, response);
>
> and don't invoke chain.doFilter as the last step.
>
> It seems to me that it should not be done this way -- what if there
> are other legitimate filters that should process the request?
> Also, I am not sure about the semantics of calling "forward" from
> "doFilter" -- is it officially supported or maybe a "grey area" of
> the spec, and it works just by chance?
>
> Anyway, I thought that I could achieve the same by using
> a HttpServletRequestWrapper, like below:
>
>     request = new HttpServletRequestWrapper(request) {
>         public String getRequestURI() {
>             return "/solonline/page/nosession.jsp"; }
>         public StringBuffer getRequestURL() {
>             return new StringBuffer(
>                 "http://192.168.0.1:6080/solonline/page/nosession.jsp"); }
>         public String getServletPath() {
>             return "/page/nosession.jsp"; }
>         };
>
>     chain.doFilter(request, response);
>
> Unfortunately, the result is an error page:
>
> ---
> HTTP Status 400 - Invalid path /page/nosession was requested
>
> type Status report
>
> message Invalid path /page/nosession was requested
>
> description The request sent by the client was syntactically incorrect
> (Invalid path /page/nosession was requested).
> ---
>
> Note the ".jsp" stripped from my URI in this error message (?)
>
> I am using Apache Tomcat/4.1.18-LE-jdk14.
>
> Can someone explain why it does not work as expected? So far I have
> not found a single example of HttpServletRequestWrapper's usage.
>
> Best regards -
> Jan Ploski




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>