Evaluate if a list of Strings contains a number in java

0

I'm working with a list that contains urls that should be excluded in a filter.

List<String> excludeFromFilter = Arrays.asList(ApiResources.Account_LOGIN,
        ApiResources.Account.URI_GET_MERCHANT,
        ApiResources.Account.URI_GET_ACCOUNT);

Next, for the functionality, I evaluate if excludeFromFilter contains the url that I passed through the request:

final HttpServletResponse response = (HttpServletResponse) servletResponse;
final String path = request.getPathInfo();

if (!excludeFromFilter.contains(path)) {
   ...
}

The problem is that sometimes I have to verify that a url that contains a parameter is excluded, for example: /api/accounts/24 what would correspond to the constant ApiResources.Account.URI_GET_ACCOUNT that has the value /api/accounts/{accountId} . I can not replace 24 with {accountId} for validation because later I may need to exclude a url that contains another parameter, for example /api/accounts/{merchantId} .

How can I use the contains () function to validate if the url has any number in that parameter. Currently this is the list:

[/ user / login, / api / merchants / {merchantId}, / api / accounts / {accountId}]

and for this last value it should be true the function contains () with values such as: "/api/accounts/24" , "/api/accounts/1" , "/api/accounts/130" .

    
asked by AndreFontaine 01.04.2016 в 22:51
source

1 answer

1

This will not be achieved with List#contains . What you can do is use a group of regexps patterns and find if any of them matches the string (url) you need to evaluate.

Here is an example:

List<Pattern> listaRegex = asList(
    Pattern.compile(ApiResources.Account_LOGIN),
    Pattern.compile(ApiResources.Account.URI_GET_MERCHANT),
    Pattern.compile(ApiResources.Account.URI_GET_ACCOUNT)
);

Assuming that these chains have values similar to (the regex can be more complex, depending on what you need)):

[/user/login, /api/merchants/\d*, /api/accounts/\d*]

And replace this part in your method:

if (!excludeFromFilter.contains(path)) {
    ...
}

by:

if (shouldBeExcluded(path)) {
    //...
}

Also, you declare the shouldBeExcluded method.

Java 8:

boolean shouldBeExcluded(String path) {
    return listaRegex.stream() //convertir a Stream
        .filter(p -> p.matcher(path).matches() ) //filtrar aquellos que cumplan con el patrón de regex y evaluar que el regex hace match con la url que indicas
        .findFirst() //devuelve el primer elemento en el stream luego de aplicar el filtro. Acción lazy, es decir, no evalúa todos los elementos para obtener el primero, al encontrar uno se detiene. Esto devuelve un Optional.
        .isPresent(); //verificar si existe un elemento devuelvo
}

Java 7 or pre:

boolean shouldBeExcluded(String path) {
    boolean result = false;
    for (Pattern pattern : listaRegex) {
        if (pattern.matcher(path).matches()) {
            result = true;
            break;
        }
    }
    return result;
}
    
answered by 01.04.2016 / 23:22
source