Skip to content

Commit f014520

Browse files
rishabhdaimRishabh Kumar
andauthored
OAK-11411 : removed usage of Guava Strings.emptyToNull (#2015)
Co-authored-by: Rishabh Kumar <diam@adobe.com>
1 parent 496ca46 commit f014520

File tree

17 files changed

+102
-18
lines changed

17 files changed

+102
-18
lines changed

oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/EntryCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.jackrabbit.oak.api.Tree;
2323
import org.apache.jackrabbit.oak.api.Type;
2424
import org.apache.jackrabbit.oak.commons.PathUtils;
25+
import org.apache.jackrabbit.oak.commons.StringUtils;
2526
import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
2627
import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionPattern;
2728
import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionProvider;
@@ -85,7 +86,7 @@ private final class PermissionEntryImpl implements PermissionEntry {
8586
private final RestrictionPattern pattern;
8687

8788
private PermissionEntryImpl(@NotNull Tree entryTree) {
88-
effectivePath = Strings.emptyToNull(TreeUtil.getString(entryTree, REP_EFFECTIVE_PATH));
89+
effectivePath = StringUtils.emptyToNull(TreeUtil.getString(entryTree, REP_EFFECTIVE_PATH));
8990
privilegeBits = bitsProvider.getBits(entryTree.getProperty(REP_PRIVILEGES).getValue(Type.NAMES));
9091
if (Utils.hasRestrictions(entryTree)) {
9192
pattern = restrictionProvider.getPattern(effectivePath, restrictionProvider.readRestrictions(effectivePath, entryTree));

oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalBasedAccessControlManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ private AbstractEntry createEffectiveEntry(@NotNull Tree entryTree) throws Acces
424424
if (principal == null || !filter.canHandle(Collections.singleton(principal))) {
425425
return null;
426426
}
427-
String oakPath = Strings.emptyToNull(TreeUtil.getString(entryTree, REP_EFFECTIVE_PATH));
427+
String oakPath = org.apache.jackrabbit.oak.commons.StringUtils.emptyToNull(TreeUtil.getString(entryTree, REP_EFFECTIVE_PATH));
428428
PrivilegeBits bits = privilegeBitsProvider.getBits(entryTree.getProperty(Constants.REP_PRIVILEGES).getValue(Type.NAMES));
429429

430430
RestrictionProvider rp = mgrProvider.getRestrictionProvider();

oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.jackrabbit.oak.api.Tree;
2323
import org.apache.jackrabbit.oak.api.Type;
2424
import org.apache.jackrabbit.oak.commons.PathUtils;
25+
import org.apache.jackrabbit.oak.commons.StringUtils;
2526
import org.apache.jackrabbit.oak.commons.collections.MapUtils;
2627
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
2728
import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
@@ -75,7 +76,7 @@ class PrincipalPolicyImpl extends AbstractAccessControlList implements Principal
7576
}
7677

7778
boolean addEntry(@NotNull Tree entryTree, @NotNull Collection<String> oakPathToFilter) throws AccessControlException {
78-
String oakPath = Strings.emptyToNull(TreeUtil.getString(entryTree, REP_EFFECTIVE_PATH));
79+
String oakPath = StringUtils.emptyToNull(TreeUtil.getString(entryTree, REP_EFFECTIVE_PATH));
7980
if (Utils.hasValidRestrictions(oakPath, entryTree, restrictionProvider)) {
8081
PrivilegeBits bits = privilegeBitsProvider.getBits(entryTree.getProperty(Constants.REP_PRIVILEGES).getValue(Type.NAMES));
8182
Set<Restriction> restrictions = Utils.readRestrictions(restrictionProvider, oakPath, entryTree);
@@ -246,7 +247,7 @@ private static String extractPathFromRestrictions(@Nullable Map<String, Value> r
246247
}
247248

248249
// retrieve path from restrictions and filter that restriction entry for further processing
249-
return Strings.emptyToNull(restrictions.get(jcrName).getString());
250+
return StringUtils.emptyToNull(restrictions.get(jcrName).getString());
250251
}
251252

252253
@NotNull

oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImporter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.jackrabbit.oak.api.Root;
2323
import org.apache.jackrabbit.oak.api.Tree;
2424
import org.apache.jackrabbit.oak.commons.PathUtils;
25+
import org.apache.jackrabbit.oak.commons.StringUtils;
2526
import org.apache.jackrabbit.oak.commons.conditions.Validate;
2627
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
2728
import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
@@ -308,7 +309,7 @@ private void applyTo(@NotNull PrincipalPolicyImpl policy) throws RepositoryExcep
308309
log.error("Missing rep:effectivePath for entry {} of policy at {}", this, policy.getOakPath());
309310
throw new ConstraintViolationException("Entries for PrincipalAccessControlList must specify an effective path.");
310311
}
311-
policy.addEntry(Strings.emptyToNull(effectivePath), Iterables.toArray(privileges, Privilege.class), restrictions, mvRestrictions);
312+
policy.addEntry(StringUtils.emptyToNull(effectivePath), Iterables.toArray(privileges, Privilege.class), restrictions, mvRestrictions);
312313
}
313314
}
314315
}

oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyValidatorProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.jackrabbit.oak.api.Tree;
2727
import org.apache.jackrabbit.oak.api.Type;
2828
import org.apache.jackrabbit.oak.commons.PathUtils;
29+
import org.apache.jackrabbit.oak.commons.StringUtils;
2930
import org.apache.jackrabbit.oak.commons.conditions.Validate;
3031
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
3132
import org.apache.jackrabbit.oak.plugins.nodetype.TypePredicate;
@@ -199,7 +200,7 @@ private void validateRestrictions(@NotNull NodeState nodeState) throws CommitFai
199200
Tree parent = verifyNotNull(parentAfter);
200201
if (NT_REP_PRINCIPAL_ENTRY.equals(TreeUtil.getPrimaryTypeName(parent))) {
201202
try {
202-
String oakPath = Strings.emptyToNull(TreeUtil.getString(parent, REP_EFFECTIVE_PATH));
203+
String oakPath = StringUtils.emptyToNull(TreeUtil.getString(parent, REP_EFFECTIVE_PATH));
203204
mgrProvider.getRestrictionProvider().validateRestrictions(oakPath, parent);
204205
} catch (AccessControlException e) {
205206
throw new CommitFailedException(ACCESS_CONTROL, 35, "Invalid restrictions", e);

oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureBlobStoreBackend.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void init() throws DataStoreException {
181181
try {
182182
Utils.setProxyIfNeeded(properties);
183183
createBlobContainer = PropertiesUtil.toBoolean(
184-
Strings.emptyToNull(properties.getProperty(AzureConstants.AZURE_CREATE_CONTAINER)), true);
184+
org.apache.jackrabbit.oak.commons.StringUtils.emptyToNull(properties.getProperty(AzureConstants.AZURE_CREATE_CONTAINER)), true);
185185
initAzureDSConfig();
186186

187187
concurrentRequestCount = PropertiesUtil.toInteger(
@@ -205,7 +205,7 @@ public void init() throws DataStoreException {
205205
requestTimeout = PropertiesUtil.toInteger(properties.getProperty(AzureConstants.AZURE_BLOB_REQUEST_TIMEOUT), RetryPolicy.DEFAULT_CLIENT_RETRY_COUNT);
206206
}
207207
presignedDownloadURIVerifyExists = PropertiesUtil.toBoolean(
208-
Strings.emptyToNull(properties.getProperty(AzureConstants.PRESIGNED_HTTP_DOWNLOAD_URI_VERIFY_EXISTS)), true);
208+
org.apache.jackrabbit.oak.commons.StringUtils.emptyToNull(properties.getProperty(AzureConstants.PRESIGNED_HTTP_DOWNLOAD_URI_VERIFY_EXISTS)), true);
209209

210210
enableSecondaryLocation = PropertiesUtil.toBoolean(
211211
properties.getProperty(AzureConstants.AZURE_BLOB_ENABLE_SECONDARY_LOCATION_NAME),
@@ -243,7 +243,7 @@ public void init() throws DataStoreException {
243243

244244
// Initialize reference key secret
245245
boolean createRefSecretOnInit = PropertiesUtil.toBoolean(
246-
Strings.emptyToNull(properties.getProperty(AzureConstants.AZURE_REF_ON_INIT)), true);
246+
org.apache.jackrabbit.oak.commons.StringUtils.emptyToNull(properties.getProperty(AzureConstants.AZURE_REF_ON_INIT)), true);
247247

248248
if (createRefSecretOnInit) {
249249
getOrCreateReferenceKey();

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/StringUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.apache.jackrabbit.oak.commons.conditions.Validate.checkArgument;
2121

2222
import org.jetbrains.annotations.NotNull;
23+
import org.jetbrains.annotations.Nullable;
2324
import org.slf4j.Logger;
2425
import org.slf4j.LoggerFactory;
2526

@@ -108,4 +109,24 @@ public static int estimateMemoryUsage(String s) {
108109
}
109110
return (int) size;
110111
}
112+
113+
/**
114+
* Returns the string if it is not empty, or {@code null} otherwise.
115+
*
116+
* @param string the string to test and possibly return, may be {@code null}
117+
* @return the input string if it is not empty, or {@code null} if the input string is {@code null} or empty
118+
*/
119+
public static String emptyToNull(@Nullable String string) {
120+
return isNullOrEmpty(string) ? null : string;
121+
}
122+
123+
/**
124+
* Checks if a string is null or empty.
125+
*
126+
* @param string the string to check, may be {@code null}
127+
* @return {@code true} if the string is null or empty, {@code false} otherwise
128+
*/
129+
static boolean isNullOrEmpty(@Nullable String string) {
130+
return string == null || string.isEmpty();
131+
}
111132
}

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
@Version("2.3.0")
17+
@Version("2.4.0")
1818
package org.apache.jackrabbit.oak.commons;
1919

2020
import org.osgi.annotation.versioning.Version;

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/StringUtilsTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25+
import org.junit.Assert;
2526
import org.junit.Test;
2627

2728
/**
@@ -79,4 +80,55 @@ public void testEstimateMemoryUsage() {
7980
}
8081
}
8182

83+
84+
@Test
85+
public void testIsNullOrEmptyWithNull() {
86+
Assert.assertTrue(StringUtils.isNullOrEmpty(null));
87+
}
88+
89+
@Test
90+
public void testIsNullOrEmptyWithEmptyString() {
91+
Assert.assertTrue(StringUtils.isNullOrEmpty(""));
92+
}
93+
94+
@Test
95+
public void testIsNullOrEmptyWithNonEmptyString() {
96+
Assert.assertFalse(StringUtils.isNullOrEmpty("not empty"));
97+
}
98+
99+
@Test
100+
public void testIsNullOrEmptyWithWhitespaceString() {
101+
Assert.assertFalse(StringUtils.isNullOrEmpty(" "));
102+
}
103+
104+
@Test
105+
public void testIsNullOrEmptyWithNewlineString() {
106+
Assert.assertFalse(StringUtils.isNullOrEmpty("\n"));
107+
}
108+
109+
@Test
110+
public void testEmptyToNullWithNull() {
111+
Assert.assertNull(StringUtils.emptyToNull(null));
112+
}
113+
114+
@Test
115+
public void testEmptyToNullWithEmptyString() {
116+
Assert.assertNull(StringUtils.emptyToNull(""));
117+
}
118+
119+
@Test
120+
public void testEmptyToNullWithNonEmptyString() {
121+
Assert.assertEquals("not empty", StringUtils.emptyToNull("not empty"));
122+
}
123+
124+
@Test
125+
public void testEmptyToNullWithWhitespaceString() {
126+
Assert.assertEquals(" ", StringUtils.emptyToNull(" "));
127+
}
128+
129+
@Test
130+
public void testEmptyToNullWithNewlineString() {
131+
Assert.assertEquals("\n", StringUtils.emptyToNull("\n"));
132+
}
133+
82134
}

oak-core/src/main/java/org/apache/jackrabbit/oak/security/authorization/accesscontrol/AccessControlManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.apache.jackrabbit.oak.api.Tree;
5959
import org.apache.jackrabbit.oak.api.Type;
6060
import org.apache.jackrabbit.oak.commons.PathUtils;
61+
import org.apache.jackrabbit.oak.commons.StringUtils;
6162
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
6263
import org.apache.jackrabbit.oak.plugins.memory.PropertyBuilder;
6364
import org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager;
@@ -587,7 +588,7 @@ private String getNodePath(@NotNull ACE principalBasedAce) throws RepositoryExce
587588
if (v == null) {
588589
throw new AccessControlException("Missing mandatory restriction rep:nodePath");
589590
} else {
590-
return getOakPath(Strings.emptyToNull(v.getString()));
591+
return getOakPath(StringUtils.emptyToNull(v.getString()));
591592
}
592593
}
593594

0 commit comments

Comments
 (0)