Skip to content

Commit 1bfce16

Browse files
authored
MINOR: add Utils.isBlank to check whitespace character string and empty string (apache#10012)
Reviewers: Chia-Ping Tsai <[email protected]>
1 parent 3769bc2 commit 1bfce16

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

clients/src/main/java/org/apache/kafka/common/security/oauthbearer/internals/unsecured/OAuthBearerUnsecuredValidatorCallbackHandler.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule;
3131
import org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallback;
3232
import org.apache.kafka.common.utils.Time;
33+
import org.apache.kafka.common.utils.Utils;
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
3536

@@ -171,32 +172,23 @@ private void handleCallback(OAuthBearerValidatorCallback callback) {
171172
OAuthBearerValidationUtils.validateTimeConsistency(unsecuredJwt).throwExceptionIfFailed();
172173
OAuthBearerValidationUtils.validateScope(unsecuredJwt, requiredScope).throwExceptionIfFailed();
173174
log.info("Successfully validated token with principal {}: {}", unsecuredJwt.principalName(),
174-
unsecuredJwt.claims().toString());
175+
unsecuredJwt.claims());
175176
callback.token(unsecuredJwt);
176177
}
177178

178179
private String principalClaimName() {
179180
String principalClaimNameValue = option(PRINCIPAL_CLAIM_NAME_OPTION);
180-
String principalClaimName = principalClaimNameValue != null && !principalClaimNameValue.trim().isEmpty()
181-
? principalClaimNameValue.trim()
182-
: "sub";
183-
return principalClaimName;
181+
return Utils.isBlank(principalClaimNameValue) ? "sub" : principalClaimNameValue.trim();
184182
}
185183

186184
private String scopeClaimName() {
187185
String scopeClaimNameValue = option(SCOPE_CLAIM_NAME_OPTION);
188-
String scopeClaimName = scopeClaimNameValue != null && !scopeClaimNameValue.trim().isEmpty()
189-
? scopeClaimNameValue.trim()
190-
: "scope";
191-
return scopeClaimName;
186+
return Utils.isBlank(scopeClaimNameValue) ? "scope" : scopeClaimNameValue.trim();
192187
}
193188

194189
private List<String> requiredScope() {
195190
String requiredSpaceDelimitedScope = option(REQUIRED_SCOPE_OPTION);
196-
List<String> requiredScope = requiredSpaceDelimitedScope == null || requiredSpaceDelimitedScope.trim().isEmpty()
197-
? Collections.emptyList()
198-
: OAuthBearerScopeUtils.parseScope(requiredSpaceDelimitedScope.trim());
199-
return requiredScope;
191+
return Utils.isBlank(requiredSpaceDelimitedScope) ? Collections.emptyList() : OAuthBearerScopeUtils.parseScope(requiredSpaceDelimitedScope.trim());
200192
}
201193

202194
private int allowableClockSkewMs() {

clients/src/main/java/org/apache/kafka/common/utils/Utils.java

+9
Original file line numberDiff line numberDiff line change
@@ -1334,4 +1334,13 @@ public static long getDateTime(String timestamp) throws ParseException, IllegalA
13341334
public static <S> Iterator<S> covariantCast(Iterator<? extends S> iterator) {
13351335
return (Iterator<S>) iterator;
13361336
}
1337+
1338+
/**
1339+
* Checks if a string is null, empty or whitespace only.
1340+
* @param str a string to be checked
1341+
* @return true if the string is null, empty or whitespace only; otherwise, return false.
1342+
*/
1343+
public static boolean isBlank(String str) {
1344+
return str == null || str.trim().isEmpty();
1345+
}
13371346
}

clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -851,4 +851,12 @@ private void invokeGetDateTimeMethod(final SimpleDateFormat format) throws Parse
851851
Utils.getDateTime(formattedCheckpoint);
852852
}
853853

854+
@Test
855+
void testIsBlank() {
856+
assertTrue(Utils.isBlank(null));
857+
assertTrue(Utils.isBlank(""));
858+
assertTrue(Utils.isBlank(" "));
859+
assertFalse(Utils.isBlank("bob"));
860+
assertFalse(Utils.isBlank(" bob "));
861+
}
854862
}

0 commit comments

Comments
 (0)