Skip to content

Commit 7c08a82

Browse files
[ZEPPELIN-6262] Refactor ElasticsearchClientTypeBuilder for safer enum handling and blank input support
### What is this PR for? This PR refactors the ElasticsearchClientTypeBuilder class to improve its robustness and readability: - Replaces StringUtils.isEmpty() with StringUtils.isBlank() to treat whitespace-only strings as invalid input - Simplifies enum parsing by removing redundant checks and using a try-catch block around Enum.valueOf(...) - Ensures that invalid or null inputs gracefully fall back to ElasticsearchClientType.UNKNOWN ### What type of PR is it? Refactoring ### Todos * [x] - Verify existing tests ### What is the Jira issue? * Jira: https://issues.apache.org/jira/browse/ZEPPELIN-6262 ### How should this be tested? N/A ### Screenshots (if appropriate) N/A ### Questions: * Does the license files need to update? No. * Is there breaking changes for older versions? No. * Does this needs documentation? No. Closes #5002 from ParkGyeongTae/ZEPPELIN-6262. Signed-off-by: Philipp Dallig <philipp.dallig@gmail.com>
1 parent 91a8565 commit 7c08a82

1 file changed

Lines changed: 8 additions & 13 deletions

File tree

elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/client/ElasticsearchClientTypeBuilder.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919

2020
import org.apache.commons.lang3.StringUtils;
2121

22-
import java.util.Arrays;
23-
2422
import static org.apache.zeppelin.elasticsearch.client.ElasticsearchClientType.TRANSPORT;
2523
import static org.apache.zeppelin.elasticsearch.client.ElasticsearchClientType.UNKNOWN;
26-
import static org.apache.zeppelin.elasticsearch.client.ElasticsearchClientType.valueOf;
27-
import static org.apache.zeppelin.elasticsearch.client.ElasticsearchClientType.values;
2824

2925
public class ElasticsearchClientTypeBuilder {
3026

@@ -47,18 +43,17 @@ private Builder(String propertyValue) {
4743

4844
@Override
4945
public ElasticsearchClientType build() {
50-
boolean isEmpty = StringUtils.isEmpty(propertyValue);
51-
return isEmpty ?
52-
DEFAULT_ELASTICSEARCH_CLIENT_TYPE :
53-
getElasticsearchClientType(propertyValue);
46+
return StringUtils.isBlank(propertyValue) ?
47+
DEFAULT_ELASTICSEARCH_CLIENT_TYPE :
48+
getElasticsearchClientType(propertyValue);
5449
}
5550

5651
private ElasticsearchClientType getElasticsearchClientType(String propertyValue){
57-
boolean isExistingValue =
58-
Arrays
59-
.stream(values())
60-
.anyMatch(clientType -> clientType.toString().equalsIgnoreCase(propertyValue));
61-
return isExistingValue ? valueOf(propertyValue.toUpperCase()) : UNKNOWN;
52+
try {
53+
return ElasticsearchClientType.valueOf(propertyValue.toUpperCase());
54+
} catch (IllegalArgumentException e) {
55+
return UNKNOWN;
56+
}
6257
}
6358
}
6459
}

0 commit comments

Comments
 (0)