mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 16:07:15 +02:00
Make Registrar load methods return Optionals instead of Nullables
This makes the code more understandable from callsites, and also forces users of this function to deal with the situation where the registrar with a given client ID might not be present (it was previously silently NPEing from some of the callsites). This also adds a test helper method loadRegistrar(clientId) that retains the old functionality for terseness in tests. It also fixes some instances of using the load method with the wrong cachedness -- some uses in high- traffic situations (WHOIS) that should have caching, but also low-traffic reporting that don't benefit from caching so might as well always be current. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=162990468
This commit is contained in:
parent
84fdeebc2f
commit
d536cef20f
81 changed files with 707 additions and 602 deletions
|
@ -16,6 +16,7 @@ package google.registry.ui.server.registrar;
|
|||
|
||||
import static com.google.common.net.HttpHeaders.LOCATION;
|
||||
import static com.google.common.net.HttpHeaders.X_FRAME_OPTIONS;
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentPresent;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_MOVED_TEMPORARILY;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
|
||||
|
@ -118,9 +119,12 @@ public final class ConsoleUiAction implements Runnable {
|
|||
.render());
|
||||
return;
|
||||
}
|
||||
Registrar registrar = Registrar.loadByClientIdCached(sessionUtils.getRegistrarClientId(req));
|
||||
String clientId = sessionUtils.getRegistrarClientId(req);
|
||||
Registrar registrar =
|
||||
checkArgumentPresent(
|
||||
Registrar.loadByClientIdCached(clientId), "Registrar %s does not exist", clientId);
|
||||
data.put("xsrfToken", xsrfTokenManager.generateToken(user.getEmail()));
|
||||
data.put("clientId", registrar.getClientId());
|
||||
data.put("clientId", clientId);
|
||||
data.put("showPaymentLink", registrar.getBillingMethod() == Registrar.BillingMethod.BRAINTREE);
|
||||
|
||||
String payload = TOFU_SUPPLIER.get()
|
||||
|
|
|
@ -17,6 +17,7 @@ package google.registry.ui.server.registrar;
|
|||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static com.google.common.base.Verify.verify;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentPresent;
|
||||
|
||||
import com.google.appengine.api.users.User;
|
||||
import com.google.common.base.Optional;
|
||||
|
@ -60,7 +61,9 @@ public class SessionUtils {
|
|||
if (!checkRegistrarConsoleLogin(request, authResult.userAuthInfo().get().user())) {
|
||||
throw new ForbiddenException("Not authorized to access Registrar Console");
|
||||
}
|
||||
return Registrar.loadByClientIdCached(getRegistrarClientId(request));
|
||||
String clientId = getRegistrarClientId(request);
|
||||
return checkArgumentPresent(
|
||||
Registrar.loadByClientIdCached(clientId), "Registrar %s not found", clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -129,8 +132,7 @@ public class SessionUtils {
|
|||
return Optional.absent();
|
||||
}
|
||||
String registrarClientId = contact.getParent().getName();
|
||||
Optional<Registrar> result =
|
||||
Optional.fromNullable(Registrar.loadByClientIdCached(registrarClientId));
|
||||
Optional<Registrar> result = Registrar.loadByClientIdCached(registrarClientId);
|
||||
if (!result.isPresent()) {
|
||||
logger.severefmt(
|
||||
"A contact record exists for non-existent registrar: %s.", Key.create(contact));
|
||||
|
@ -140,12 +142,12 @@ public class SessionUtils {
|
|||
|
||||
/** @see #hasAccessToRegistrar(Registrar, String) */
|
||||
private static boolean hasAccessToRegistrar(String clientId, final String gaeUserId) {
|
||||
Registrar registrar = Registrar.loadByClientIdCached(clientId);
|
||||
if (registrar == null) {
|
||||
Optional<Registrar> registrar = Registrar.loadByClientIdCached(clientId);
|
||||
if (!registrar.isPresent()) {
|
||||
logger.warningfmt("Registrar '%s' disappeared from Datastore!", clientId);
|
||||
return false;
|
||||
}
|
||||
return hasAccessToRegistrar(registrar, gaeUserId);
|
||||
return hasAccessToRegistrar(registrar.get(), gaeUserId);
|
||||
}
|
||||
|
||||
/** Returns {@code true} if {@code gaeUserId} is listed in contacts. */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue