jeudi 28 mars 2019

How can I check if String contains the domain name listed in ArrayList having multiple domains in Java?

I'm trying to allow request from specific domains only which are listed in ArrayList, so I need to put a conditional check if URL contains the domains listed in ArrayList then allow otherwise throw an Exception.

Suppose my ArrayList = [abc.com, def.com, xyz.com] I want to check if my URL contains any of these domains from ArrayList then return true else return false.

I tried below code, but it checks the domain name one by one. However, it returns false if domain name is valid -

ArrayList = [abc.com, def.com, xyz.com]

public static boolean isListContainValidDomain(List<String> arraylist) {
String reqURL = "dev.def.com";
boolean isValid = true;

    for (String str : arraylist) {

        if(!reqURL.contains(str.toLowerCase())){
            isValid = false;
        }
    }
    return isValid;
}

Can anyone please help on this.

Aucun commentaire:

Enregistrer un commentaire