Question

We are trying to get the server name, session id browser info... to keep the information in the MDC for logging.

For this we are trying to add a listener in the RequestCycle of the onBeginRequest() of IRequestCycleListener. I have added a class as follows:

public class RequestRListener implements IRequestCycleListener
{
  @Override
  public void onBeginRequest(RequestCycle cycle)
  {
      //TODO 
  } 
}

Now where to add the listener to get the above mentioned informations ?

Was it helpful?

Solution

In your class that extends WebApplication or AuthenticatedWebApplication in the init() function.

You need to get the request cycle listeners and add your new listener to the list.

So, if you want to output the requested URI you'd do something like this:

getRequestCycleListeners().add( 
new AbstractRequestCycleListener()
{       
    public void onBeginRequest(RequestCycle cycle) 
    {                   
        if( cycle.getRequest().getContainerRequest() instanceof HttpServletRequest )
        {
            HttpServletRequest containerRequest = 
                (HttpServletRequest)cycle.getRequest().getContainerRequest();
            System.out.println("URI="+containerRequest.getRequestURI() );
        }
    };
}
);

Getting the browser info can be done almost anywhere, not necessarily just in the request.

String userAgent = WebSession.get().getClientInfo().getUserAgent();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top