Re: Filter versus Valve

2009-02-12 Thread Jake Vang
The code for the Filter implementation:

public class SimpleFilter implements Filter {

public void destroy() {

}

public void doFilter(ServletRequest servletRequest, ServletResponse
servletResponse,
FilterChain filterChain) throws IOException, ServletException {
if(servletRequest instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest =
(HttpServletRequest)servletRequest;
FakeHeadersRequest request = new
FakeHeadersRequest(httpServletRequest);
filterChain.doFilter(request, servletResponse);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
return;
}

public void init(FilterConfig filterConfig) throws ServletException {

}

}

The code for the Valve subclass:

public class ModifyHeaderValve extends ValveBase {

public void invoke(Request request, Response response, ValveContext
valveContext)
throws IOException, ServletException {
if(
!(request instanceof HttpRequest) ||
!(response instanceof HttpResponse)
) {
valveContext.invokeNext(request, response);
return;
}

if(
!(request.getRequest() instanceof HttpServletRequest) ||
!(response.getResponse() instanceof HttpServletResponse)
) {
valveContext.invokeNext(request, response);
return;
}

HttpServletRequest httpServletRequest = (HttpServletRequest)request;
FakeHeadersRequest fakeHeadersRequest = new
FakeHeadersRequest(httpServletRequest);
String value = fakeHeadersRequest.getHeader("username");
valveContext.invokeNext(request, response);

return;
}

}

The code for the FakeHeadersRequest:

public class FakeHeadersRequest extends HttpServletRequestWrapper {

public FakeHeadersRequest(HttpServletRequest request) {
super(request);
}

public String getHeader(String name) {
HttpServletRequest request = (HttpServletRequest)getRequest();

if("username".equals(name)) {
Cookie[] cookies = request.getCookies();
for(int i=0; i < cookies.length; i++) {
if("username".equals(cookies[i].getName())) {
String val = cookies[i].getValue();
return val;
}
}
}

return request.getHeader(name);
}

public Enumeration getHeaderNames() {
List list = new ArrayList();

HttpServletRequest request = (HttpServletRequest)getRequest();
Enumeration e = request.getHeaderNames();
while(e.hasMoreElements()) {
String n = (String)e.nextElement();
list.add(n);
}
list.add("username");

Enumeration en = Collections.enumeration(list);
return en;
}

}

On Thu, Feb 12, 2009 at 3:05 AM, Kees de Kooter  wrote:

> Could you post the code of your valve and your filter?
>
> Please also not that a Valve is a tomcat specific thing i.e. not
> portable to other app servers. A Filter is part of the servlet spec
> and portable.
>
>
> On Thu, Feb 12, 2009 at 06:13, Jake Vang  wrote:
> > I've been looking for a way to modify my request header. I found that
> > implementing javax.servlet.Filter is the way to go. However, I noticed
> that
> > once after I got my Filter implementation working, my Valve is no longer
> > reached (I created my own Valve subclassing
> > org.apache.catalina.valves.ValveBase). I've determined this because I've
> > placed breakpoints in the Filter.doFilter(...) method and
> Valve.invoke(...)
> > methods, and only the breakpoint in the Filter class implementation is
> > caught.
> >
> > My context XML for my web application is:
> >
> > 
> >  >docBase="C:\my-valve\dist"
> >reloadable="true">
> >
> > 
> >
> > My web.xml for my web application is:
> >
> > 
> >simpleFilter
> >vang.jake.servlet.filter.SimpleFilter
> >
> >
> >simpleFilter
> >/*
> >
> >
> > Could someone clarify if the use of one (filter) precludes the use of the
> > other (valve)? If so, why? If not, why is my valve never reached?
> >
> > Thanks.
> >
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


Re: Filter versus Valve

2009-02-12 Thread Kees de Kooter
Could you post the code of your valve and your filter?

Please also not that a Valve is a tomcat specific thing i.e. not
portable to other app servers. A Filter is part of the servlet spec
and portable.


On Thu, Feb 12, 2009 at 06:13, Jake Vang  wrote:
> I've been looking for a way to modify my request header. I found that
> implementing javax.servlet.Filter is the way to go. However, I noticed that
> once after I got my Filter implementation working, my Valve is no longer
> reached (I created my own Valve subclassing
> org.apache.catalina.valves.ValveBase). I've determined this because I've
> placed breakpoints in the Filter.doFilter(...) method and Valve.invoke(...)
> methods, and only the breakpoint in the Filter class implementation is
> caught.
>
> My context XML for my web application is:
>
> 
> docBase="C:\my-valve\dist"
>reloadable="true">
>
> 
>
> My web.xml for my web application is:
>
> 
>simpleFilter
>vang.jake.servlet.filter.SimpleFilter
>
>
>simpleFilter
>/*
>
>
> Could someone clarify if the use of one (filter) precludes the use of the
> other (valve)? If so, why? If not, why is my valve never reached?
>
> Thanks.
>

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Filter versus Valve

2009-02-11 Thread Jake Vang
I've been looking for a way to modify my request header. I found that
implementing javax.servlet.Filter is the way to go. However, I noticed that
once after I got my Filter implementation working, my Valve is no longer
reached (I created my own Valve subclassing
org.apache.catalina.valves.ValveBase). I've determined this because I've
placed breakpoints in the Filter.doFilter(...) method and Valve.invoke(...)
methods, and only the breakpoint in the Filter class implementation is
caught.

My context XML for my web application is:






My web.xml for my web application is:

 
simpleFilter
vang.jake.servlet.filter.SimpleFilter


simpleFilter
/*


Could someone clarify if the use of one (filter) precludes the use of the
other (valve)? If so, why? If not, why is my valve never reached?

Thanks.