Skip to content

Commit

Permalink
Remove non-idiomatic use of Objects.isNull
Browse files Browse the repository at this point in the history
This method exists to be used as a Predicate, filter(Objects::isNull)
  • Loading branch information
Luca Bassi authored and enricovianello committed Nov 14, 2024
1 parent a91997b commit d5dd04e
Show file tree
Hide file tree
Showing 35 changed files with 88 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
package org.italiangrid.storm.webdav.authn;

import static java.util.Objects.isNull;

import java.util.Map;
import java.util.Objects;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
Expand All @@ -33,15 +30,15 @@ private AuthenticationUtils() {
}

public static String getPalatableSubject(Authentication authn) {
if (Objects.isNull(authn) || authn instanceof AnonymousAuthenticationToken) {
if (authn == null || authn instanceof AnonymousAuthenticationToken) {
return "Anonymous user";
} else if (authn instanceof OAuth2AuthenticationToken) {
OAuth2AuthenticationToken authToken = (OAuth2AuthenticationToken) authn;
Map<String, Object> attributes = authToken.getPrincipal().getAttributes();

String subjectIssuer = String.format("%s @ %s", attributes.get("sub"), attributes.get("iss"));

if (!isNull(attributes.get("name"))) {
if (attributes.get("name") != null) {
return (String) attributes.get("name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.italiangrid.storm.webdav.config.ServiceConfigurationProperties;
Expand All @@ -43,7 +42,7 @@ public PrincipalHelper(ServiceConfigurationProperties config) throws MalformedUR
}

public String getPrincipalAsString(Authentication authn) {
if (Objects.isNull(authn) || authn instanceof AnonymousAuthenticationToken) {
if (authn == null || authn instanceof AnonymousAuthenticationToken) {
return "anonymous";
} else if (authn instanceof OAuth2AuthenticationToken) {
OAuth2AuthenticationToken authToken = (OAuth2AuthenticationToken) authn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.italiangrid.storm.webdav.authz;

import static java.util.Objects.isNull;
import static org.italiangrid.storm.webdav.authz.SAPermission.canRead;
import static org.italiangrid.storm.webdav.authz.SAPermission.canWrite;

Expand Down Expand Up @@ -49,7 +48,7 @@ public VOMSPolicyService(StorageAreaConfiguration saConfig) {
voMapPerms = ArrayListMultimap.create();

for (StorageAreaInfo sa : saConfig.getStorageAreaInfo()) {
if (!isNull(sa.vos())) {
if (sa.vos() != null) {
for (String vo : sa.vos()) {
voPerms.put(vo, canRead(sa.name()));
voPerms.put(vo, canWrite(sa.name()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.italiangrid.storm.webdav.authz.pdp;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -158,7 +157,7 @@ public Builder withPrincipalMatcher(PrincipalMatcher m) {
}

public PathAuthorizationPolicy build() {
if (Objects.isNull(id)) {
if (id == null) {
id = UUID.randomUUID().toString();
}
return new PathAuthorizationPolicy(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.italiangrid.storm.webdav.authz.pdp;

import static java.lang.String.format;
import static java.util.Objects.isNull;
import static java.util.stream.Collectors.toList;
import static org.italiangrid.storm.webdav.authz.pdp.PathAuthorizationResult.deny;
import static org.italiangrid.storm.webdav.authz.pdp.PathAuthorizationResult.indeterminate;
Expand Down Expand Up @@ -181,13 +180,13 @@ public PathAuthorizationResult authorizeRequest(PathAuthorizationRequest authzRe

JwtAuthenticationToken jwtAuth = (JwtAuthenticationToken) authentication;

if (isNull(jwtAuth.getToken().getClaimAsString(SCOPE_CLAIM))) {
if (jwtAuth.getToken().getClaimAsString(SCOPE_CLAIM) == null) {
return indeterminate(ERROR_INVALID_TOKEN_NO_SCOPE);
}

StorageAreaInfo sa = pathResolver.resolveStorageArea(requestPath);

if (isNull(sa)) {
if (sa == null) {
return indeterminate(ERROR_SA_NOT_FOUND);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
*/
package org.italiangrid.storm.webdav.authz.pdp.principal;

import static java.util.Objects.isNull;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;

public class AnonymousUser implements PrincipalMatcher {

@Override
public boolean matchesPrincipal(Authentication authentication) {
return isNull(authentication) || authentication instanceof AnonymousAuthenticationToken;
return authentication == null || authentication instanceof AnonymousAuthenticationToken;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
*/
package org.italiangrid.storm.webdav.authz.pdp.principal;

import static java.util.Objects.isNull;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;

public class AnyAuthenticatedUser implements PrincipalMatcher {

@Override
public boolean matchesPrincipal(Authentication authentication) {
return !isNull(authentication) && !(authentication instanceof AnonymousAuthenticationToken)
return authentication != null && !(authentication instanceof AnonymousAuthenticationToken)
&& authentication.isAuthenticated();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.italiangrid.storm.webdav.authz.pdp.principal;

import java.util.Objects;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

Expand All @@ -39,7 +37,7 @@ public static AuthorityHolder fromAuthority(GrantedAuthority authority) {
@Override
public boolean matchesPrincipal(Authentication authentication) {

return !Objects.isNull(authentication) && authentication.getAuthorities()
return authentication != null && authentication.getAuthorities()
.stream()
.anyMatch(a -> a.getAuthority().equals(authority.getAuthority()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Objects.nonNull;

import java.nio.file.Path;
import java.util.regex.Matcher;
Expand All @@ -33,7 +32,7 @@ public class StructuredPathScopeMatcher implements ScopeMatcher {
public static final Logger LOG = LoggerFactory.getLogger(StructuredPathScopeMatcher.class);

private static final Pattern POINT_DIR_MATCHER = Pattern.compile("\\.\\./?");

private static final Character SEP = ':';
private static final String SEP_STR = SEP.toString();

Expand All @@ -55,24 +54,24 @@ private StructuredPathScopeMatcher(String prefix, String path) {

@Override
public boolean matchesScope(String scope) {
checkArgument(nonNull(scope), "scope must be non-null");
checkArgument(scope != null, "scope must be non-null");

if (POINT_DIR_MATCHER.matcher(scope).find()) {
throw new IllegalArgumentException("Scope contains relative path references");
}

Matcher prefixMatcher = prefixMatchPattern.matcher(scope);

boolean prefixMatches = prefixMatcher.find();

if (prefixMatches) {
final String scopePath = scope.substring(prefix.length() + 1);
return pathMatchPattern.matcher(scopePath).matches();
return pathMatchPattern.matcher(scopePath).matches();
} else {
return false;
}
}

public boolean matchesPath(String path) {
return pathMatchPattern.matcher(path).matches();
}
Expand All @@ -81,7 +80,7 @@ public boolean matchesPathIncludingParents(String path) {
Path targetPath = Path.of(path);
return this.path.startsWith(targetPath) || matchesPath(path);
}

public static StructuredPathScopeMatcher fromString(String scope) {
final int sepIndex = scope.indexOf(SEP);
final String prefix = scope.substring(0, sepIndex);
Expand Down Expand Up @@ -143,5 +142,5 @@ public String getPrefix() {
public String getPath() {
return path.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.italiangrid.storm.webdav.authz.voters;

import static java.util.Objects.isNull;
import static org.italiangrid.storm.webdav.authz.pdp.PathAuthorizationRequest.newAuthorizationRequest;

import java.util.Collection;
Expand Down Expand Up @@ -48,7 +47,7 @@ public int vote(Authentication authentication, FilterInvocation filter,
final String requestPath = getRequestPath(filter.getHttpRequest());
StorageAreaInfo sa = resolver.resolveStorageArea(requestPath);

if (isNull(sa) || !sa.fineGrainedAuthzEnabled()) {
if (sa == null || !sa.fineGrainedAuthzEnabled()) {
return ACCESS_ABSTAIN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.italiangrid.storm.webdav.authz.voters;

import static java.util.Objects.isNull;
import static org.italiangrid.storm.webdav.server.servlet.WebDAVMethod.COPY;
import static org.italiangrid.storm.webdav.server.servlet.WebDAVMethod.PUT;

Expand Down Expand Up @@ -68,7 +67,7 @@ && requestHasRemoteDestinationHeader(filter.getRequest(), localUrlService)) {
String destinationPath = getSanitizedPathFromUrl(destination);
StorageAreaInfo sa = resolver.resolveStorageArea(destinationPath);

if (isNull(sa)) {
if (sa == null) {
return ACCESS_ABSTAIN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.italiangrid.storm.webdav.authz.voters;

import static java.util.Objects.isNull;
import static org.italiangrid.storm.webdav.server.servlet.WebDAVMethod.COPY;
import static org.italiangrid.storm.webdav.server.servlet.WebDAVMethod.PUT;

Expand Down Expand Up @@ -74,7 +73,7 @@ && requestHasRemoteDestinationHeader(filter.getRequest(), localUrlService)) {
String destinationPath = getSanitizedPathFromUrl(destination);
StorageAreaInfo sa = resolver.resolveStorageArea(destinationPath);

if (isNull(sa)) {
if (sa == null) {
return ACCESS_ABSTAIN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.italiangrid.storm.webdav.authz.voters;

import static java.util.Objects.isNull;
import static org.italiangrid.storm.webdav.authz.pdp.PathAuthorizationRequest.newAuthorizationRequest;

import java.util.Collection;
Expand Down Expand Up @@ -53,7 +52,7 @@ public int vote(Authentication authentication, FilterInvocation filter,
final String requestPath = getRequestPath(filter.getHttpRequest());
StorageAreaInfo sa = resolver.resolveStorageArea(requestPath);

if (isNull(sa)) {
if (sa == null) {
return ACCESS_ABSTAIN;
}

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

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
import static java.util.Objects.isNull;
import static org.italiangrid.storm.webdav.config.FineGrainedAuthzPolicyProperties.PrincipalProperties.PrincipalType.FQAN;
import static org.italiangrid.storm.webdav.config.FineGrainedAuthzPolicyProperties.PrincipalProperties.PrincipalType.JWT_GROUP;
import static org.italiangrid.storm.webdav.config.FineGrainedAuthzPolicyProperties.PrincipalProperties.PrincipalType.JWT_SCOPE;
Expand Down Expand Up @@ -63,8 +62,8 @@ public boolean isValid(
ConstraintValidatorContext context) {

Collection<String> requiredArgs = REQUIRED_ARGS.get(value.getType());
if (isNull(requiredArgs) || requiredArgs.isEmpty()) {

if (requiredArgs == null || requiredArgs.isEmpty()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.italiangrid.storm.webdav.metrics;

import static com.codahale.metrics.MetricRegistry.name;
import static java.util.Objects.isNull;

import java.io.IOException;

Expand Down Expand Up @@ -52,7 +51,7 @@ public StorageAreaStatsFilter(MetricRegistry registry, PathResolver resolver) {
private void updateStats(HttpServletRequest request, HttpServletResponse response) {
StorageAreaInfo sa = resolver.resolveStorageArea(getSerlvetRequestPath(request));

if (!isNull(sa)) {
if (sa != null) {
String prefix = name(SA_KEY, sa.name(), REQUESTS_KEY);
registry.meter(prefix).mark();
if (response.getStatus() >= 400) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.italiangrid.storm.webdav.milton;

import static io.milton.property.PropertySource.PropertyAccessibility.READ_ONLY;
import static java.util.Objects.isNull;

import java.io.BufferedInputStream;
import java.io.File;
Expand Down Expand Up @@ -126,15 +125,15 @@ public void replaceContent(InputStream in, Long length)
protected void validateRange(Range range) {
long fileSize = getFile().length();

if (isNull(range.getStart())) {
if (range.getStart() == null) {
throw new StoRMWebDAVError("Invalid range: range start not defined");
}

if (range.getStart() >= fileSize) {
throw new StoRMWebDAVError("Invalid range: range start out of bounds");
}

if (!isNull(range.getFinish()) && range.getFinish() > fileSize) {
if (range.getFinish() != null && range.getFinish() > fileSize) {
throw new StoRMWebDAVError("Invalid range: range end out of bounds");
}
}
Expand All @@ -147,7 +146,7 @@ public void replacePartialContent(Range range, InputStream in) {
long rangeStart = range.getStart();
long rangeEnd = getFile().length();

if (!isNull(range.getFinish()) && range.getFinish() < rangeEnd) {
if (range.getFinish() != null && range.getFinish() < rangeEnd) {
rangeEnd = range.getFinish();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.italiangrid.storm.webdav.oauth;

import static java.util.Objects.isNull;

import java.util.Collection;
import java.util.Set;

Expand Down Expand Up @@ -48,7 +46,7 @@ public GrantedAuthoritiesMapperSupport(StorageAreaConfiguration conf,
authzServerProperties = props.getAuthzServer();

for (StorageAreaInfo sa : conf.getStorageAreaInfo()) {
if (!isNull(sa.orgs())) {
if (sa.orgs() != null) {
sa.orgs().forEach(i -> addSaGrantedAuthorities(sa, i));
}
}
Expand Down
Loading

0 comments on commit d5dd04e

Please sign in to comment.