2

After following this question, I added HttpServletRequest parameter to my method. But I added nearly 20 IF conditions to have a complete case-insensitive URL.

This is a completely painful and ugly way of coding, I know. I request anyone to give an amazing solution to this.

I need to have a case-insensitive request-parameter. The request parameter which is sending is orgID. This parameter would be coming in different ways. E.g. Orgid, oRgid, orGid, orgID ... and so-on

I cannot do this directly as request.getParamter ("orgID"). For this, I am adding many if conditions. :-( as I said, completely ugly coding.

QuickSilver
  • 3,915
  • 2
  • 13
  • 29
Arun
  • 3,440
  • 11
  • 60
  • 108

6 Answers6

2
private String getCaseInsensitiveOrgIdParameter(HttpServletRequest request) {
    for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
        String paramName = entry.getKey();
        if ("orgid".equals(paramName.toLowerCase()) {
            return entry.getValue()[0];
        }
    }
    return null;
}

But I still stand by my position: the other applications sending this parameter are the ones that should be fixed. If the parameter is orgId, they should send orgId. Not orGid or anything else. If they're lazy (or stupid) enough to send orGid instead of orgId, nothing guarantees that they won't send ogrId. What will you do then?

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks a lot JB. Very nice solution. Its working. Now coming to ur question //If they're lazy (or stupid) enough to send orGid instead of orgId, nothing guarantees that they won't send ogrId. What will you do then?// . I will put papers and flee away from my company. Already design is bit weird, I personally feel – Arun Jan 23 '12 at 09:44
  • 1
    As I mentioned above, use "".equalsIgnoreCase() instead of explicitly creating a .toLowerCase() copy. – Ajax Sep 13 '12 at 20:18
2

Though this concept is wrong(it is not good to get the params in different cases) the following code snippet works:

String getCaseInsensitiveParameter(HttpServletRequest request){
Map params = request.getParameterMap();
    Iterator i = params.keySet().iterator();
    while ( i.hasNext() )
      {
        String key = (String) i.next();
        String value = ((String[]) params.get( key ))[ 0 ];
        if ("orgid".equals(key.toLowerCase()) {
            return value;
        }
      }
return null;
}
Arun
  • 2,562
  • 6
  • 25
  • 43
  • 2
    Or, you can use "".equalsIgnoreCase() to let the regex test without incurring the cost of explicitly creating a lowercase copy... – Ajax Sep 13 '12 at 20:17
1

You could have a Spring MVC Handler Interceptor (org.springframework.web.servlet Interface HandlerInterceptor) that take a parameter name and "convert" it to an standardized name.

Something like: if the parameter name in the request ends with "id" not matter which case (except exactly "Id"), than it takes the parameter, copy it and add it under the name "xxxId".

Then you will need only to check for the normalized form fin the request method.

@See: for an quick introduction into Handler interceptor: this blog (attention the very last code block is an idea for an improvement, it does not work)

Ralph
  • 118,862
  • 56
  • 287
  • 383
0

You should use the equalsIgnoreCase method:

public String getRequestParameter(HttpServletRequest request, String parameter){
    for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
        if(entry.getKey().equalsIgnoreCase(parameter)){
            return entry.getValue()[0];
        }
    }
    return null;
}

or if you have the request as an instance variable:

public String getRequestParameter(String parameter){
    for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
        if(entry.getKey().equalsIgnoreCase(parameter)){
            return entry.getValue()[0];
        }
    }
    return null;
}
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
0

My solution: simply create a new params map object, and populate it with the lowercased keys. Then use this params map instead of the orignal object. Then you can ask for the lowercased key. I did it like this (using Apache Wicket's PageParameters class):

Map paramsOriginal = request.getParameterMap();
PageParameters paramsNew = new PageParameters();
Iterator<String> i = paramsOriginal.keySet().iterator();
while(i.hasNext()) {
    String key = (String) i.next();
    String value = ((String[]) paramsOriginal.get(key))[0];
    //lowercase the key
    paramsNew.put(key.toLowerCase(), value);
}

//request the value of a lowercased key
String orgid = paramsNew.getString("orgid");
fegyi001
  • 161
  • 1
  • 4
0

If you have access to Java 8:

request.getParameterMap().stream()
    .collect(Collectors.toMap(e -> e.getKey().toLowerCase(), e.getValue()))
    .get("orgID");
Hazel T
  • 859
  • 1
  • 8
  • 22