mirror of
https://github.com/google/nomulus.git
synced 2025-07-03 01:33:29 +02:00
Switch from Guava Optionals to Java 8 Optionals
This was a surprisingly involved change. Some of the difficulties included java.util.Optional purposely not being Serializable (so I had to move a few Optionals in mapreduce classes to @Nullable) and having to add the Truth Java8 extension library for assertion support. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=171863777
This commit is contained in:
parent
184b2b56ac
commit
c0f8da0c6e
581 changed files with 1325 additions and 932 deletions
|
@ -20,12 +20,12 @@ import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
|
|||
import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import google.registry.request.auth.AuthResult;
|
||||
import google.registry.request.auth.RequestAuthenticator;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import google.registry.util.TypeUtils.TypeInstantiator;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Provider;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
|
|
@ -19,11 +19,11 @@ import static com.google.common.base.Strings.isNullOrEmpty;
|
|||
import static com.google.common.base.Strings.nullToEmpty;
|
||||
|
||||
import com.google.common.base.Ascii;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.net.InetAddresses;
|
||||
import google.registry.request.HttpException.BadRequestException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Optional;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.joda.time.DateTime;
|
||||
|
@ -60,7 +60,7 @@ public final class RequestParameters {
|
|||
|
||||
/** Returns the first GET or POST parameter associated with {@code name}. */
|
||||
public static Optional<String> extractOptionalParameter(HttpServletRequest req, String name) {
|
||||
return Optional.fromNullable(emptyToNull(req.getParameter(name)));
|
||||
return Optional.ofNullable(emptyToNull(req.getParameter(name)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,7 +72,7 @@ public final class RequestParameters {
|
|||
String stringParam = req.getParameter(name);
|
||||
try {
|
||||
return isNullOrEmpty(stringParam)
|
||||
? Optional.<Integer>absent()
|
||||
? Optional.<Integer>empty()
|
||||
: Optional.of(Integer.valueOf(stringParam));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new BadRequestException("Expected integer: " + name);
|
||||
|
@ -123,7 +123,7 @@ public final class RequestParameters {
|
|||
HttpServletRequest req, String name) {
|
||||
String stringParam = req.getParameter(name);
|
||||
return isNullOrEmpty(stringParam)
|
||||
? Optional.<Boolean>absent()
|
||||
? Optional.<Boolean>empty()
|
||||
: Optional.of(Boolean.valueOf(stringParam));
|
||||
}
|
||||
|
||||
|
@ -167,7 +167,7 @@ public final class RequestParameters {
|
|||
String stringParam = req.getParameter(name);
|
||||
try {
|
||||
return isNullOrEmpty(stringParam)
|
||||
? Optional.<DateTime>absent()
|
||||
? Optional.<DateTime>empty()
|
||||
: Optional.of(DateTime.parse(stringParam));
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new BadRequestException("Bad ISO 8601 timestamp: " + name);
|
||||
|
@ -212,7 +212,7 @@ public final class RequestParameters {
|
|||
HttpServletRequest req, String name) {
|
||||
Optional<String> paramVal = extractOptionalParameter(req, name);
|
||||
if (!paramVal.isPresent()) {
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
try {
|
||||
return Optional.of(InetAddresses.forString(paramVal.get()));
|
||||
|
@ -245,7 +245,7 @@ public final class RequestParameters {
|
|||
* @param name case insensitive header name
|
||||
*/
|
||||
public static Optional<String> extractOptionalHeader(HttpServletRequest req, String name) {
|
||||
return Optional.fromNullable(req.getHeader(name));
|
||||
return Optional.ofNullable(req.getHeader(name));
|
||||
}
|
||||
|
||||
private RequestParameters() {}
|
||||
|
|
|
@ -18,12 +18,12 @@ import static com.google.common.base.Preconditions.checkArgument;
|
|||
import static com.google.common.base.Throwables.throwIfUnchecked;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableSortedMap;
|
||||
import com.google.common.collect.Ordering;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
|
@ -62,7 +62,7 @@ final class Router {
|
|||
return Optional.of(floor.getValue());
|
||||
}
|
||||
}
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
static ImmutableSortedMap<String, Route> extractRoutesFromComponent(Class<?> componentClass) {
|
||||
|
|
|
@ -17,7 +17,7 @@ package google.registry.request.auth;
|
|||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import com.google.common.base.Optional;
|
||||
import java.util.Optional;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
|
@ -37,14 +37,14 @@ public abstract class AuthResult {
|
|||
}
|
||||
|
||||
public static AuthResult create(AuthLevel authLevel) {
|
||||
return new AutoValue_AuthResult(authLevel, Optional.<UserAuthInfo>absent());
|
||||
return new AutoValue_AuthResult(authLevel, Optional.<UserAuthInfo>empty());
|
||||
}
|
||||
|
||||
public static AuthResult create(AuthLevel authLevel, @Nullable UserAuthInfo userAuthInfo) {
|
||||
if (authLevel == AuthLevel.USER) {
|
||||
checkNotNull(userAuthInfo);
|
||||
}
|
||||
return new AutoValue_AuthResult(authLevel, Optional.fromNullable(userAuthInfo));
|
||||
return new AutoValue_AuthResult(authLevel, Optional.ofNullable(userAuthInfo));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,11 +18,11 @@ import static com.google.common.base.Preconditions.checkArgument;
|
|||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Ordering;
|
||||
import com.google.errorprone.annotations.Immutable;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import java.util.Optional;
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
|
@ -117,14 +117,14 @@ public class RequestAuthenticator {
|
|||
case APP:
|
||||
if (!authResult.isAuthenticated()) {
|
||||
logger.warning("Not authorized; no authentication found");
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
break;
|
||||
case USER:
|
||||
if (authResult.authLevel() != AuthLevel.USER) {
|
||||
logger.warning("Not authorized; no authenticated user");
|
||||
// TODO(mountford): change this so that the caller knows to return a more helpful error
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -132,7 +132,7 @@ public class RequestAuthenticator {
|
|||
case IGNORED:
|
||||
if (authResult.authLevel() == AuthLevel.USER) {
|
||||
logger.warning("Not authorized; user policy is IGNORED, but a user was found");
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
break;
|
||||
case PUBLIC:
|
||||
|
@ -142,7 +142,7 @@ public class RequestAuthenticator {
|
|||
if (authResult.userAuthInfo().isPresent()
|
||||
&& !authResult.userAuthInfo().get().isUserAdmin()) {
|
||||
logger.warning("Not authorized; user policy is ADMIN, but the user was not an admin");
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ package google.registry.request.auth;
|
|||
|
||||
import com.google.appengine.api.users.User;
|
||||
import com.google.auto.value.AutoValue;
|
||||
import com.google.common.base.Optional;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Extra information provided by the authentication mechanism about the user. */
|
||||
@AutoValue
|
||||
|
@ -39,7 +39,7 @@ public abstract class UserAuthInfo {
|
|||
|
||||
public static UserAuthInfo create(
|
||||
User user, boolean isUserAdmin) {
|
||||
return new AutoValue_UserAuthInfo(user, isUserAdmin, Optional.<OAuthTokenInfo>absent());
|
||||
return new AutoValue_UserAuthInfo(user, isUserAdmin, Optional.<OAuthTokenInfo>empty());
|
||||
}
|
||||
|
||||
public static UserAuthInfo create(
|
||||
|
|
|
@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkArgument;
|
|||
import static com.google.common.base.Throwables.throwIfUnchecked;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableSortedSet;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
|
@ -27,6 +26,7 @@ import google.registry.util.AppEngineTimeLimiter;
|
|||
import google.registry.util.FormattingLogger;
|
||||
import google.registry.util.RequestStatusChecker;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue