In Play 2.4.3 customized RequestHeader is replaced with original version once it gets to Controller

38 views
Skip to first unread message

Dmitriy Tserekhman

unread,
Oct 7, 2015, 12:42:03 AM10/7/15
to play-framework
Hi,

I'm using Play 2.4.3, and I overrode onRouteRequest(request: RequestHeader) method from the GlobalSettings to enrich the request with my Headers like so:

override def onRouteRequest(requestHeader: RequestHeader): Option[Handler] = {
super.onRouteRequest(requestHeader.copy(headers = Headers("ABC_HEADER"->"for testing")))
}

However, when the call gets to the resolved route, the requestHeader object that is received by the implementing method on the Controller no longer has my "ABC_HEADER". I turned on debugger and confirmed that the overridden method above does get called, and I also confirmed that's when the super.onRouteRequest is called on the parent GlobalSettings class, the requestHeader passed into the onRouteRequest method there does have my "ABC_HEADER". 

However, something happens when the requestHeader gets to the resolved route method. By that time, it is replaced by a different requestHeader object which contains the state of the original requestHeader, and it no longer has my "ABC_HEADER". (I also tried using Filters, but ran into the same problem)

Any suggestions of what's going on here and how to go about it?

Thanks.

Hob Spillane

unread,
Oct 7, 2015, 11:05:30 AM10/7/15
to play-framework
I'm not sure if it's impacting you or not, but GlobalSettings is deprecated as of 2.4.0.  You might try moving to the prescribed approach in 2.4 and see if it helps:

Dmitriy Tserekhman

unread,
Oct 7, 2015, 4:28:39 PM10/7/15
to play-framework
Thank you Hob! 

I experimented with a new style of overriding these request methods as you suggested under https://www.playframework.com/documentation/2.4.x/GlobalSettings, and it still initially did not work for me. I was getting the same behavior as I initially described.

However, here's what I've noticed and tried different:

I originally was overriding GlobalSetting's onRouteRequest method, where the corresponding method according to the new Play's infrastructure is routeRequest(..) on the DefaultHttpRequestHandler. By overriding that method, it still did not work for me.

Then, I tried overriding handlerForRequest on  DefaultHttpRequestHandler, and that did it! The new request with the additional headers was successfully floating down to the routeRequest method and then to my route's implementing method, still keeping my new headers intact.

I looked and saw there was a corresponding onRequestReceived method on the original GlobalSettings class I was overriding. So, I went ahead, and overrode the onRequestReceived method there in my old implementation, and that also worked for me! 

So, it was just about overriding the right method for the task I was trying to achieve. Thanks for your help, it eventually got my on the right path!
Reply all
Reply to author
Forward
0 new messages