Sunday, May 22, 2016

Making Play route case-insensitive - part 2

The previous post (part 1) explains, how to make the URL route case-insensitive.

If a URL contains some query parameters, additional treatment should be done in order to make the query parameters case insensitive as well.

URL query parameters may be extracted in two ways: implicit and explicit.

Implicit extracting of query parameters

The query parameters are defined in routes configuration and a controller call is done with parameter(s).
Play assigns to the controller parameters values of the corresponding query parameters prior to the controller call.

For example, the routes configuration has the following entry:
 GET /thisIsMyPage   controllers.HomeController.someRoute(myMessage)

The controller methods is defined with a parameter message:
   public Result someRoute(String message) {  

The parameter name message in the controller method signature does not matter.
But the name of the controller parameter myMessage in the routes configuration defines the exact name to be used for the URL query parameter:
http://localhost:9000/thisIsMyPage?myMessage=do it now

If the query parameter will be mentioned with different case, the bad request error will happen:

So, query parameters, defined in routes configuration, cannot be case-insensitive.

Explicit extracting of query parameters

The routes configuration defines appropriate controller call without parameters.
In order to get the values of the query parameter(s) a controller should implement explicit query parameters extraction.
Explicit extracting of query parameters may be done ignoring case.

For example, the routes configuration has the following entry:
# case-insensitive route example
GET /$dummy<(?i)thisIsMyPage> controllers.HomeController.insensitiveRoute(dummy)

The controller code:
public Result insensitiveRoute(String dummy) {  
  // Extract query parameter case-insensitive  
  String message = InputRequestHelper.getInputQueryParameter("mymessage", request(), true);  
  return ok(examplepage.render("This is insensitive route:" + message));  
public static String getInputQueryParameter(String paramName, Request request, boolean ignoreCase) {  
  String paramValue = null;  
  if (ignoreCase) {  
    paramValue = request.getQueryString(paramName);  
  else {  
    Map<String, String[]> queryString = request.queryString();  
    List<String> insenstitiveKeys = queryString.keySet().stream()  
      .filter(k -> {return k.equalsIgnoreCase(paramName);})  
    if (insenstitiveKeys.size() > 0) {  
      String insensitiveParamName = insenstitiveKeys.get(0);  
      paramValue = request.getQueryString(insensitiveParamName);  
  return paramValue;  

The URL:
http://localhost:9000/thisisMyPage?mymessage=insensitive parameter
opens the window:

Both the route and the query parameter are treated case-insensitive.

All the sources on Git.

No comments :

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top