Hello,

I simplified the code so it does not use RequestGlobals which was giving me
NULL for  this.requestGlobals.getHTTPServletRequest()

AppModule.java
=====================================
...
binder.bind(HttpServletRequestFilter.class,
RequestLoggingFilter.class).withId("RequestLoggingFilter");
...

public static void contributeHttpServletRequestHandler(
        OrderedConfiguration<HttpServletRequestFilter> configuration,
        @InjectService("RequestLoggingFilter") HttpServletRequestFilter
myfilter) {
    configuration.add("myfilter", myfilter, "before:*");
}

RequestLoggingFilter.java
=====================================
public class RequestLoggingFilter implements HttpServletRequestFilter {

    @Override
    public boolean service(HttpServletRequest request, HttpServletResponse
response, HttpServletRequestHandler handler)
            throws IOException {

        MDC.put("remoteIP", request.getRemoteAddr());

        HttpSession se = request.getSession(false);
        if (se != null) {
            String id = se.getId();
            if (id == null) {
                id = "";
            }
            MDC.put("sessionID", id);
        }

        try {
            return handler.service(request, response);
        } finally {
            MDC.remove("remoteIP");
            MDC.remove("sessionID");
        }
    }
}

Thanks for hints.


2009/10/9 Olle Hallin <olle.hal...@hit.se>

> Do like this:
> MDC.put("xxx", ...);
> MDC.put("yyy", ...);
>
> try {
>    return handler.service(request, response);
> } finally {
>  MDC.remove("xxx")
>  MDC.remove("yyy")
> }
>
> or else you will have problems when your request throws an exception....
>
> Olle Hallin
> Senior Java Developer and Architect
> olle.hal...@crisp.se
> www.crisp.se
>
>
>
>
> 2009/10/9 <dirk.latterm...@bgs-ag.de>
>
> > Hi!
> >
> > Borut Bolčina <borut.bolc...@gmail.com> schrieb am 09.10.2009 14:55:32:
> >
> > > this is what I did:
> > >
> > > public class RequestLoggingFilter implements HttpServletRequestFilter {
> > >     public final RequestGlobals requestGlobals;
> > >
> > >     public RequestLoggingFilter(final RequestGlobals requestGlobalss) {
> > >         this.requestGlobals = requestGlobalss;
> > >     }
> > >
> > >     @Override
> > >     public boolean service(HttpServletRequest request,
> > HttpServletResponse
> > > response, HttpServletRequestHandler handler)
> > >             throws IOException {
> > >         MDC.put("remoteIP",
> > > this.requestGlobals.getHTTPServletRequest().getRemoteAddr());
> > >
> > >         String s =
> > > this.requestGlobals.getHTTPServletRequest().getRequestedSessionId();
> > >         if (s == null) {
> > >             s = "";
> > >         }
> > >         MDC.put("sessionID", s);
> > >         return handler.service(request, response);
> > >     }
> > > }
> >
> > >
> > > but I feel I am missing something. Where do I put the code:
> > > MDC.remove("remoteIP");
> > > MDC.remove("sessionID");
> > >
> >
> > In a similar situation, I used something like
> > --------------------
> >     @Override
> >    public boolean service(HttpServletRequest request, HttpServletResponse
> > response, HttpServletRequestHandler handler)
> >            throws IOException {
> >        MDC.put("remoteIP",
> > this.requestGlobals.getHTTPServletRequest().getRemoteAddr());
> >
> >        String s =
> > this.requestGlobals.getHTTPServletRequest().getRequestedSessionId();
> >        if (s == null) {
> >            s = "";
> >        }
> >        MDC.put("sessionID", s);
> >         boolean result = handler.service(request, response);
> >
> >        MDC.remove("remoteIP");
> >        MDC.remove("sessionID");
> >
> >         return result;
> >    }
> > }
> > --------------------
> >
> > but I'm not sure if it does the right thing in all situations.
> >
> > Dirk
> >
> >
> >
> > BGS Beratungsgesellschaft
> > Software Systemplanung AG
> >
> >
> >
> >
> > Niederlassung Köln/Bonn
> > Grantham-Allee 2-8
> > 53757 Sankt Augustin
> > Fon: +49 (0) 2241 / 166-500
> > Fax: +49 (0) 2241 / 166-680
> > www.bgs-ag.de
> > Geschäftssitz Mainz
> > Registergericht
> > Amtsgericht Mainz
> > HRB 62 50
> >
> > Aufsichtsratsvorsitzender
> > Klaus Hellwig
> > Vorstand
> > Hermann Kiefer
> > Nils Manegold
> > Thomas Reitz
> >
> >
>

Reply via email to