Remove unneeded use of AccountCache from Set*Preferences REST endpoints

As result of the account update we get the updated AccountState back.
There is no need to retrieve it from the account cache. If the account
update returns an empty Optional it means that the account couldn't be
updated because it was missing. Throw an exception in this case.

Change-Id: Icdc23cc4abdb827d696f7c5e019421cdb9fab988
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-01-24 13:50:23 +01:00
parent 5ec99ec969
commit 7621aebb69
3 changed files with 30 additions and 30 deletions

View File

@@ -15,13 +15,15 @@
package com.google.gerrit.server.restapi.account;
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.permissions.GlobalPermission;
import com.google.gerrit.server.permissions.PermissionBackend;
@@ -38,25 +40,22 @@ import org.eclipse.jgit.errors.RepositoryNotFoundException;
public class SetDiffPreferences implements RestModifyView<AccountResource, DiffPreferencesInfo> {
private final Provider<CurrentUser> self;
private final PermissionBackend permissionBackend;
private final AccountCache accountCache;
private final AccountsUpdate.User accountsUpdate;
@Inject
SetDiffPreferences(
Provider<CurrentUser> self,
PermissionBackend permissionBackend,
AccountCache accountCache,
AccountsUpdate.User accountsUpdate) {
this.self = self;
this.permissionBackend = permissionBackend;
this.accountCache = accountCache;
this.accountsUpdate = accountsUpdate;
}
@Override
public DiffPreferencesInfo apply(AccountResource rsrc, DiffPreferencesInfo input)
throws AuthException, BadRequestException, ConfigInvalidException,
RepositoryNotFoundException, IOException, PermissionBackendException, OrmException {
throws RestApiException, ConfigInvalidException, RepositoryNotFoundException, IOException,
PermissionBackendException, OrmException {
if (self.get() != rsrc.getUser()) {
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
}
@@ -66,9 +65,10 @@ public class SetDiffPreferences implements RestModifyView<AccountResource, DiffP
}
Account.Id id = rsrc.getUser().getAccountId();
accountsUpdate
return accountsUpdate
.create()
.update("Set Diff Preferences via API", id, u -> u.setDiffPreferences(input));
return accountCache.get(id).getDiffPreferences();
.update("Set Diff Preferences via API", id, u -> u.setDiffPreferences(input))
.map(AccountState::getDiffPreferences)
.orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString())));
}
}

View File

@@ -15,13 +15,15 @@
package com.google.gerrit.server.restapi.account;
import com.google.gerrit.extensions.client.EditPreferencesInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.permissions.GlobalPermission;
import com.google.gerrit.server.permissions.PermissionBackend;
@@ -39,25 +41,22 @@ public class SetEditPreferences implements RestModifyView<AccountResource, EditP
private final Provider<CurrentUser> self;
private final PermissionBackend permissionBackend;
private final AccountCache accountCache;
private final AccountsUpdate.User accountsUpdate;
@Inject
SetEditPreferences(
Provider<CurrentUser> self,
PermissionBackend permissionBackend,
AccountCache accountCache,
AccountsUpdate.User accountsUpdate) {
this.self = self;
this.permissionBackend = permissionBackend;
this.accountCache = accountCache;
this.accountsUpdate = accountsUpdate;
}
@Override
public EditPreferencesInfo apply(AccountResource rsrc, EditPreferencesInfo input)
throws AuthException, BadRequestException, RepositoryNotFoundException, IOException,
ConfigInvalidException, PermissionBackendException, OrmException {
throws RestApiException, RepositoryNotFoundException, IOException, ConfigInvalidException,
PermissionBackendException, OrmException {
if (self.get() != rsrc.getUser()) {
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
}
@@ -67,9 +66,10 @@ public class SetEditPreferences implements RestModifyView<AccountResource, EditP
}
Account.Id id = rsrc.getUser().getAccountId();
accountsUpdate
return accountsUpdate
.create()
.update("Set Edit Preferences via API", id, u -> u.setEditPreferences(input));
return accountCache.get(id).getEditPreferences();
.update("Set Edit Preferences via API", id, u -> u.setEditPreferences(input))
.map(AccountState::getEditPreferences)
.orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString())));
}
}

View File

@@ -18,13 +18,15 @@ import com.google.common.base.Strings;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
import com.google.gerrit.extensions.config.DownloadScheme;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.account.Preferences;
import com.google.gerrit.server.permissions.GlobalPermission;
@@ -40,7 +42,6 @@ import org.eclipse.jgit.errors.ConfigInvalidException;
@Singleton
public class SetPreferences implements RestModifyView<AccountResource, GeneralPreferencesInfo> {
private final Provider<CurrentUser> self;
private final AccountCache cache;
private final PermissionBackend permissionBackend;
private final AccountsUpdate.User accountsUpdate;
private final DynamicMap<DownloadScheme> downloadSchemes;
@@ -48,12 +49,10 @@ public class SetPreferences implements RestModifyView<AccountResource, GeneralPr
@Inject
SetPreferences(
Provider<CurrentUser> self,
AccountCache cache,
PermissionBackend permissionBackend,
AccountsUpdate.User accountsUpdate,
DynamicMap<DownloadScheme> downloadSchemes) {
this.self = self;
this.cache = cache;
this.permissionBackend = permissionBackend;
this.accountsUpdate = accountsUpdate;
this.downloadSchemes = downloadSchemes;
@@ -61,8 +60,8 @@ public class SetPreferences implements RestModifyView<AccountResource, GeneralPr
@Override
public GeneralPreferencesInfo apply(AccountResource rsrc, GeneralPreferencesInfo input)
throws AuthException, BadRequestException, IOException, ConfigInvalidException,
PermissionBackendException, OrmException {
throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException,
OrmException {
if (self.get() != rsrc.getUser()) {
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
}
@@ -71,10 +70,11 @@ public class SetPreferences implements RestModifyView<AccountResource, GeneralPr
Preferences.validateMy(input.my);
Account.Id id = rsrc.getUser().getAccountId();
accountsUpdate
return accountsUpdate
.create()
.update("Set General Preferences via API", id, u -> u.setGeneralPreferences(input));
return cache.get(id).getGeneralPreferences();
.update("Set General Preferences via API", id, u -> u.setGeneralPreferences(input))
.map(AccountState::getGeneralPreferences)
.orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString())));
}
private void checkDownloadScheme(String downloadScheme) throws BadRequestException {