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

0

1 Answer 1

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]");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just tried. It prints both the strings
I edited the answer just now. Try the latest version.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.