0

I have two arrays containing some strings like the following:

Array #1

abcd                    
efgh            
servicegroup_1
ijkl 

Array #2

servicegroup_3
servicegroup_1

I'd like to print a string like "VERIFY: OK" (on console) if the Array #1 contains at least one of the strings of Array #2. Otherwise, a string "VERIFY: KO" have to be printed.

I implemented some code like the following:

for (int i = 0; i < scopeFile.length; i++) {
                for (String element : scopeJWT) {
                    if (scopeFile[i].contains(element)) {
                        ctx.setSendZuulResponse(true);
                        System.out.println(now + " --- " + "[Check Signature: OK]" + " [Verify Scope: OK]" + " [Verify expTime: OK]");
                    } else {
                        ctx.setSendZuulResponse(false);
                        ctx.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
                        System.err.println(now + " --- " + "[VERIFY SCOPE: KO]");
                    }
                }
}

but, it returns both VERIFY SCOPE: OK and VERIFY SCOPE: KO, even if in considering the arrays I reported, I would have printed just VERIFY SCOPE: OK(because Array #1 contains servicegroup_1)

Any ideas to fix my code? Thank you

Riccardo
  • 289
  • 2
  • 5
  • 17

1 Answers1

2

You forgot to break the loop after the string is found in Array1. Try adding a break; after this line:

System.out.println(now + " --- " + "[Check Signature: OK]" + " [Verify Scope: OK]" + " [Verify expTime: OK]");

And check non-ok case outside the loop.

like this:

boolean isOk = false;
for (int i = 0; i < scopeFile.length; i++) {
    for (String element : scopeJWT) {
        if (scopeFile[i].contains(element)) {
            ctx.setSendZuulResponse(true);
            System.out.println(now + " --- " + "[Check Signature: OK]" + " [Verify Scope: OK]" + " [Verify expTime: OK]");
            isOk = true;
            break;
        }
    }
}
if (!isOk) {
    ctx.setSendZuulResponse(false);
    ctx.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
    System.err.println(now + " --- " + "[VERIFY SCOPE: KO]");
}
drowny
  • 2,067
  • 11
  • 19
Danny
  • 286
  • 2
  • 6