HttpServletRequest lowecase parameter

After the RequestParam value in spring MVC is case insensitive , I added a parameter HttpServletRequestto my method. But I added almost 20 IF conditions to have a full url containing ins-insenstive.

This is an absolutely painful and ugly way to code, I know. I ask someone to give an amazing solution.

I need to have a case-insensitive query parameter. The request parameter that is sent is orgID. This parameter will act differently. say Orgid, Orgid, Orgid, Orgid....... and so on

I can not do it directly like request.getParamter ("orgID"). For this, I add a lot of conditions. :-( as I said, completely ugly coding.

+2
source share
6 answers

Despite the fact that this concept is incorrect (it does not matter to get the parameters in different cases), the following code fragment 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;
}
+2
source
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 hold on to my position: the other applications sending this parameter are the ones that need to be fixed. If parameter orgId, they must send orgId. Not orgIdor something else. If they are lazy (or stupid) enough to send orgIdinstead orgId, nothing guarantees that they will not send ogrId. What will you do then?

+2
source

MVC Spring (org.springframework.web.servlet Interface HandlerInterceptor), "" .

- : "id", , ( "Id" ), , "xxxId".

, .

@: Handler: ( - , )

+1

You should use the method equalsIgnoreCase:

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 a query 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;
}
0
source

My solution: just create a new params map object and fill it with the bottom keys. Then use this params map instead of the orignal object. Then you can request a lower key. I did it like this (using the Apache Wicket 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");
0
source

If you have access to Java 8:

request.getParameterMap().stream()
    .collect(Collectors.toMap(e -> e.getKey().toLowerCase(), e.getValue()))
    .get("orgID");
0
source

Source: https://habr.com/ru/post/1726307/


All Articles