Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework 2.2.1 - Case insensitive routing

I am novice to the Play and currently working Play 2.2.1

I am trying to acheive case insensitive routing for my endpoints which are defined in "routes"

e.g. I have define an route say /accessLicense in routes file, it would look like below

GET /accessLicense controller.MyController.accessLicense()

Now, if I fire /accessLicense it woks great; as expected, but if try to fir /AccessLicense, /AcCeSSLicenSe or any other combination of upper/lower case letter which spell exact same word, it doesn't work.

Thanks in advance for guidance and support!!!

like image 897
Kunal Shewale Avatar asked Feb 25 '14 13:02

Kunal Shewale


1 Answers

Unfortunately, AFAIK, there is no way to magically turn on a switch that will do what you want. Thankfully, there's a workaround, inferior IMHO but its the best that can be done.

GET /[aA][cC][cC][eE][sS][sS].....

EDIT: I did the following, which matches my specific requirement of lower-casing only the first part of URL. So GET /AbCdE/XyZ will become GET /abcde/XyZ and if this has an action in the routes than it will be handled appropriately.

override def onRouteRequest( request: RequestHeader ) = {
   val path = request.path
   val split = path.split( "/" ).toList

   val lowerCasePath = split match{
     case ""::Nil  => ""::Nil
     case ""::x::y => ""::x.toLowerCase::y
   }

   logger.error( lowerCasePath.toString )

   super.onRouteRequest( request.copy( path = lowerCasePath.mkString( "/" ) ) )
}

EDIT See here: https://jazzy.id.au/2013/05/08/advanced_routing_in_play_framework.html

like image 175
0fnt Avatar answered Nov 05 '22 12:11

0fnt