Ram Dutt Shukla
Ram Dutt Shukla

Reputation: 1352

How to add a listener to the requestcycle onbegin request?

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 ?

Upvotes: 0

Views: 1719

Answers (1)

Grant Shearer
Grant Shearer

Reputation: 130

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();

Upvotes: 3

Related Questions