Skip to content

Commit

Permalink
replace isNullOrEmpty() check
Browse files Browse the repository at this point in the history
Signed-off-by: Surya Sashank Nistala <snistala@amazon.com>
  • Loading branch information
eirsep committed Jul 6, 2023
1 parent fca6d8e commit 99f835b
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 97 deletions.
7 changes: 3 additions & 4 deletions src/main/java/org/opensearch/commons/InjectSecurity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.commons.authuser.User;
Expand Down Expand Up @@ -111,7 +110,7 @@ public void inject(final String user, final List<String> roles) {
* @param user name
*/
public void injectUser(final String user) {
if (Strings.isNullOrEmpty(user)) {
if (user.isEmpty()) {
return;
}

Expand Down Expand Up @@ -148,7 +147,7 @@ public void injectUserInfo(final User user) {
joiner.add(java.lang.String.join(",", user.getBackendRoles()));
joiner.add(java.lang.String.join(",", user.getRoles()));
String requestedTenant = user.getRequestedTenant();
if (!Strings.isNullOrEmpty(requestedTenant)) {
if (!requestedTenant.isEmpty()) {
joiner.add(requestedTenant);
}
threadContext.putTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT, joiner.toString());
Expand Down Expand Up @@ -181,7 +180,7 @@ public void injectRoles(final List<String> roles) {
* @return boolean
*/
public boolean injectProperty(final String property, final Object value) {
if (Strings.isNullOrEmpty(property) || value == null || threadContext.getTransient(property) != null) {
if (property == null || property.isEmpty() || value == null || threadContext.getTransient(property) != null) {
log.debug("{}, InjectSecurity - cannot inject property: {}", Thread.currentThread().getName(), id);
return false;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

import org.opensearch.client.Request;
import org.opensearch.client.RequestOptions;
import org.opensearch.common.Strings;
import org.opensearch.commons.ConfigConstants;

public class AuthUserRequestBuilder {
private final String auth;

public AuthUserRequestBuilder(String auth) {
if (Strings.isNullOrEmpty(auth)) {
if (auth.isEmpty()) {
throw new IllegalArgumentException("Authorization token cannot be null");
}
this.auth = auth;
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/org/opensearch/commons/authuser/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.apache.http.util.EntityUtils;
import org.opensearch.client.Response;
import org.opensearch.common.Nullable;
import org.opensearch.common.Strings;
import org.opensearch.common.inject.internal.ToStringBuilder;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -89,7 +88,7 @@ public User(final Response response) throws IOException {

@SuppressWarnings("unchecked")
public User(String json) {
if (Strings.isNullOrEmpty(json)) {
if (json.isEmpty()) {
throw new IllegalArgumentException("Response json cannot be null");
}

Expand Down Expand Up @@ -158,12 +157,12 @@ public static User parse(XContentParser parser) throws IOException {
* @return
*/
public static User parse(final String userString) {
if (Strings.isNullOrEmpty(userString)) {
if (userString == null || userString.isEmpty()) {
return null;
}

String[] strs = userString.split("\\|");
if ((strs.length == 0) || (Strings.isNullOrEmpty(strs[0]))) {
if (strs.length == 0 || strs[0] == null || strs[0].isEmpty()) {
return null;
}

Expand All @@ -172,13 +171,13 @@ public static User parse(final String userString) {
List<String> roles = new ArrayList<>();
String requestedTenant = null;

if ((strs.length > 1) && !Strings.isNullOrEmpty(strs[1])) {
if ((strs.length > 1) && !strs[1].isEmpty()) {
backendRoles.addAll(Arrays.asList(strs[1].split(",")));
}
if ((strs.length > 2) && !Strings.isNullOrEmpty(strs[2])) {
if ((strs.length > 2) && !strs[2].isEmpty()) {
roles.addAll(Arrays.asList(strs[2].split(",")));
}
if ((strs.length > 3) && !Strings.isNullOrEmpty(strs[3])) {
if ((strs.length > 3) && !strs[3].isEmpty()) {
requestedTenant = strs[3].trim();
}
return new User(userName, backendRoles, roles, Arrays.asList(), requestedTenant);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Map;

import org.apache.http.client.utils.URIBuilder;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;
Expand All @@ -31,7 +30,7 @@ public abstract class LegacyBaseMessage implements Writeable {
if (destinationType == null) {
throw new IllegalArgumentException("Channel type must be defined");
}
if (!Strings.hasLength(destinationName)) {
if (destinationName == null || destinationName.isEmpty()) {
throw new IllegalArgumentException("Channel name must be defined");
}
this.destinationType = destinationType;
Expand Down Expand Up @@ -80,8 +79,8 @@ public URI getUri() {

protected URI buildUri(String endpoint, String scheme, String host, int port, String path, Map<String, String> queryParams) {
try {
if (Strings.isNullOrEmpty(endpoint)) {
if (Strings.isNullOrEmpty(scheme)) {
if (endpoint.isEmpty()) {
if (scheme.isEmpty()) {
scheme = "https";
}
URIBuilder uriBuilder = new URIBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.io.IOException;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;

/**
Expand All @@ -19,7 +18,7 @@ public class LegacyChimeMessage extends LegacyBaseMessage {
private LegacyChimeMessage(final String destinationName, final String url, final String message) {
super(LegacyDestinationType.LEGACY_CHIME, destinationName, message, url);

if (Strings.isNullOrEmpty(message)) {
if (message == null || message.isEmpty()) {
throw new IllegalArgumentException("Message content is missing");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;

Expand Down Expand Up @@ -45,28 +44,28 @@ private LegacyCustomWebhookMessage(
) {
super(LegacyDestinationType.LEGACY_CUSTOM_WEBHOOK, destinationName, message);

if (!Strings.isNullOrEmpty(url)) {
if (url != null && !url.isEmpty()) {
setUrl(url.trim());
}

if (Strings.isNullOrEmpty(message)) {
if (message == null || message.isEmpty()) {
throw new IllegalArgumentException("Message content is missing");
}

this.scheme = Strings.isNullOrEmpty(scheme) ? "https" : scheme;
this.scheme = scheme == null || scheme.isEmpty() ? "https" : scheme;
this.port = port == null ? -1 : port;

if (!Strings.isNullOrEmpty(path)) {
if (path != null && !path.isEmpty()) {
if (!path.startsWith("/")) {
this.path = "/" + path;
}
}

if (Strings.isNullOrEmpty(url) && Strings.isNullOrEmpty(host)) {
if ((url == null || url.isEmpty()) && (host == null || host.isEmpty())) {
throw new IllegalArgumentException("Either fully qualified URL or host name should be provided");
}

if (Strings.isNullOrEmpty(method)) {
if (method == null || method.isEmpty()) {
// Default to POST for backwards compatibility
this.method = "POST";
} else if (!HttpPost.METHOD_NAME.equals(method) && !HttpPut.METHOD_NAME.equals(method) && !HttpPatch.METHOD_NAME.equals(method)) {
Expand Down Expand Up @@ -239,7 +238,7 @@ public void writeTo(StreamOutput streamOutput) throws IOException {
// Making LegacyCustomWebhookMessage streamable is purely to support the new pass through API from Alerting/ISM -> Notification
// plugin
// and it only supports LegacyCustomWebhookMessage when the url is already constructed by Alerting/ISM.
if (Strings.isNullOrEmpty(getUrl())) {
if (getUrl() == null || getUrl().isEmpty()) {
throw new IllegalStateException("Cannot use LegacyCustomWebhookMessage across transport wire without defining full url.");
}
streamOutput.writeOptionalString(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.net.URI;
import java.util.List;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.commons.notifications.model.MethodType;
Expand Down Expand Up @@ -41,19 +40,19 @@ private LegacyEmailMessage(
) {
super(LegacyDestinationType.LEGACY_EMAIL, destinationName, message);

if (Strings.isNullOrEmpty(message)) {
if (message == null || message.isEmpty()) {
throw new IllegalArgumentException("Message content is missing");
}

if (Strings.isNullOrEmpty(accountName)) {
if (accountName == null || accountName.isEmpty()) {
throw new IllegalArgumentException("Account name should be provided");
}

if (Strings.isNullOrEmpty(host)) {
if (host == null || host.isEmpty()) {
throw new IllegalArgumentException("Host name should be provided");
}

if (Strings.isNullOrEmpty(from)) {
if (from == null || from.isEmpty()) {
throw new IllegalArgumentException("From address should be provided");
}

Expand All @@ -66,7 +65,7 @@ private LegacyEmailMessage(
this.host = host;
this.port = port == null ? 25 : port;

if (Strings.isNullOrEmpty(method)) {
if ((method.isEmpty())) {
// Default to "none"
this.method = "none";
} else if (!MethodType.NONE.toString().equals(method)
Expand All @@ -79,7 +78,7 @@ private LegacyEmailMessage(

this.from = from;
this.recipients = recipients;
this.subject = Strings.isNullOrEmpty(subject) ? destinationName : subject;
this.subject = (subject == null || subject.isEmpty()) ? destinationName : subject;
}

public LegacyEmailMessage(StreamInput streamInput) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.io.IOException;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.commons.destination.util.Util;
Expand All @@ -33,18 +32,18 @@ private LegacySNSMessage(
) {
super(LegacyDestinationType.LEGACY_SNS, destinationName, message);

if (Strings.isNullOrEmpty(message)) {
if (message.isEmpty()) {
throw new IllegalArgumentException("Message content is missing");
}
if (Strings.isNullOrEmpty(roleArn) || !Util.isValidIAMArn(roleArn)) {
if (roleArn.isEmpty() || !Util.isValidIAMArn(roleArn)) {
throw new IllegalArgumentException("Role arn is missing/invalid: " + roleArn);
}

if (Strings.isNullOrEmpty(topicArn) || !Util.isValidSNSArn(topicArn)) {
if (topicArn.isEmpty() || !Util.isValidSNSArn(topicArn)) {
throw new IllegalArgumentException("Topic arn is missing/invalid: " + topicArn);
}

if (Strings.isNullOrEmpty(message)) {
if (message.isEmpty()) {
throw new IllegalArgumentException("Message content is missing");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.io.IOException;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;

/**
Expand All @@ -19,11 +18,11 @@ public class LegacySlackMessage extends LegacyBaseMessage {
private LegacySlackMessage(final String destinationName, final String url, final String message) {
super(LegacyDestinationType.LEGACY_SLACK, destinationName, message, url);

if (Strings.isNullOrEmpty(url)) { // add URL validation
if ((url.isEmpty())) { // add URL validation
throw new IllegalArgumentException("Fully qualified URL is missing/invalid: " + url);
}

if (Strings.isNullOrEmpty(message)) {
if (message == null || message.isEmpty()) {
throw new IllegalArgumentException("Message content is missing");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.util.regex.Pattern;

import org.opensearch.common.Strings;
import org.opensearch.common.ValidationException;

public class Util {
Expand All @@ -26,10 +25,10 @@ public static String getRegion(String arn) {
}

public static boolean isValidIAMArn(String arn) {
return Strings.hasLength(arn) && IAM_ARN_REGEX.matcher(arn).find();
return !arn.isEmpty() && IAM_ARN_REGEX.matcher(arn).find();
}

public static boolean isValidSNSArn(String arn) throws ValidationException {
return Strings.hasLength(arn) && SNS_ARN_REGEX.matcher(arn).find();
return !arn.isEmpty() && SNS_ARN_REGEX.matcher(arn).find();
}
}
Loading

0 comments on commit 99f835b

Please sign in to comment.