Files
gerrit/java/com/google/gerrit/server/restapi/account/StarredChanges.java
David Pursehouse b564a52689 Merge branch 'stable-2.15'
* stable-2.15:
  Update git submodules
  Use Logger's built-in string formatting where possible
  Doc: Fix code example in JS API
  Allow some sections of the change list to overflow
  Fix more comparisons of current user
  Revert "WorkQueue: Add metrics"
  Fix permissions checks on Gerrit API on current user
  GroupCacheImpl: Fix log message when UUID is not found
  Update git submodules
  Update git submodules
  WorkQueue.Executor#buildMetrics: Remove MetricMaker parameter
  ChangeQueryBuilder: Allow ownerin predicate to be evaluated by the index
  Update git submodules
  Update git submodules
  Update git submodules
  ldap.Helper: Use local logger and make logger in LdapRealm private
  Remove ValidationError#createLoggerSink to avoid passing around loggers
  LdapLoginServlet: Improve exception handling
  OperatingSystemMXBeanProvider: Log exception for ReflectiveOperationException
  HttpPluginServlet: Don't trim leading whitespace from about.md content
  ProjectConfig: Don't use JGit's StringUtils to convert to lower case
  Do not abort indexing if < 50% projects failed
  Revert "AllChangesIndexer: Don't abort when failing to open repository"
  WorkQueue: Don't fail when queue metric already exists
  WorkQueue: Sanitize metric name when queue is created
  DropWizardMetricMaker: Introduce method to sanitize metric name
  VersionedAccountDestinations: Remove unused createSink(String) method
  ProjectBasicAuthFilter: Add comment why cause is not logged
  BazelBuild: Fix exception message when command was interrupted
  GitwebServlet: Write only one log entry for CGI errors
  GitwebServlet: Log unexpected errors on error level
  PostGpgKeys: Remove unneeded use of Joiner
  Remove some logs for errors that are rethrown
  DropWizardMetricMaker: Improve error messages for invalid arguments
  DropWizardMetricMaker: Improve error message when metric name is invalid
  AllChangesIndexer: Don't abort when failing to open repository

Change-Id: I33e8a1c9498c6adecd4ffc7d8ef0cc78aeb28eea
2018-05-24 10:59:04 +09:00

207 lines
7.7 KiB
Java

// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.restapi.account;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AcceptsCreate;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.StarredChangesUtil;
import com.google.gerrit.server.StarredChangesUtil.IllegalLabelException;
import com.google.gerrit.server.StarredChangesUtil.MutuallyExclusiveLabelsException;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.change.ChangeResource;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.restapi.change.ChangesCollection;
import com.google.gerrit.server.restapi.change.QueryChanges;
import com.google.gwtorm.server.OrmDuplicateKeyException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class StarredChanges
implements ChildCollection<AccountResource, AccountResource.StarredChange>,
AcceptsCreate<AccountResource> {
private static final Logger log = LoggerFactory.getLogger(StarredChanges.class);
private final ChangesCollection changes;
private final DynamicMap<RestView<AccountResource.StarredChange>> views;
private final Provider<Create> createProvider;
private final StarredChangesUtil starredChangesUtil;
@Inject
StarredChanges(
ChangesCollection changes,
DynamicMap<RestView<AccountResource.StarredChange>> views,
Provider<Create> createProvider,
StarredChangesUtil starredChangesUtil) {
this.changes = changes;
this.views = views;
this.createProvider = createProvider;
this.starredChangesUtil = starredChangesUtil;
}
@Override
public AccountResource.StarredChange parse(AccountResource parent, IdString id)
throws RestApiException, OrmException, PermissionBackendException, IOException {
IdentifiedUser user = parent.getUser();
ChangeResource change = changes.parse(TopLevelResource.INSTANCE, id);
if (starredChangesUtil
.getLabels(user.getAccountId(), change.getId())
.contains(StarredChangesUtil.DEFAULT_LABEL)) {
return new AccountResource.StarredChange(user, change);
}
throw new ResourceNotFoundException(id);
}
@Override
public DynamicMap<RestView<AccountResource.StarredChange>> views() {
return views;
}
@Override
public RestView<AccountResource> list() throws ResourceNotFoundException {
return new RestReadView<AccountResource>() {
@Override
public Object apply(AccountResource self)
throws BadRequestException, AuthException, OrmException {
QueryChanges query = changes.list();
query.addQuery("starredby:" + self.getUser().getAccountId().get());
return query.apply(TopLevelResource.INSTANCE);
}
};
}
@Override
public RestModifyView<AccountResource, EmptyInput> create(AccountResource parent, IdString id)
throws RestApiException {
try {
return createProvider.get().setChange(changes.parse(TopLevelResource.INSTANCE, id));
} catch (ResourceNotFoundException e) {
throw new UnprocessableEntityException(String.format("change %s not found", id.get()));
} catch (OrmException | PermissionBackendException | IOException e) {
log.error("cannot resolve change", e);
throw new UnprocessableEntityException("internal server error");
}
}
@Singleton
public static class Create implements RestModifyView<AccountResource, EmptyInput> {
private final Provider<CurrentUser> self;
private final StarredChangesUtil starredChangesUtil;
private ChangeResource change;
@Inject
Create(Provider<CurrentUser> self, StarredChangesUtil starredChangesUtil) {
this.self = self;
this.starredChangesUtil = starredChangesUtil;
}
public Create setChange(ChangeResource change) {
this.change = change;
return this;
}
@Override
public Response<?> apply(AccountResource rsrc, EmptyInput in)
throws RestApiException, OrmException, IOException {
if (!self.get().hasSameAccountId(rsrc.getUser())) {
throw new AuthException("not allowed to add starred change");
}
try {
starredChangesUtil.star(
self.get().getAccountId(),
change.getProject(),
change.getId(),
StarredChangesUtil.DEFAULT_LABELS,
null);
} catch (MutuallyExclusiveLabelsException e) {
throw new ResourceConflictException(e.getMessage());
} catch (IllegalLabelException e) {
throw new BadRequestException(e.getMessage());
} catch (OrmDuplicateKeyException e) {
return Response.none();
}
return Response.none();
}
}
@Singleton
static class Put implements RestModifyView<AccountResource.StarredChange, EmptyInput> {
private final Provider<CurrentUser> self;
@Inject
Put(Provider<CurrentUser> self) {
this.self = self;
}
@Override
public Response<?> apply(AccountResource.StarredChange rsrc, EmptyInput in)
throws AuthException {
if (!self.get().hasSameAccountId(rsrc.getUser())) {
throw new AuthException("not allowed update starred changes");
}
return Response.none();
}
}
@Singleton
public static class Delete implements RestModifyView<AccountResource.StarredChange, EmptyInput> {
private final Provider<CurrentUser> self;
private final StarredChangesUtil starredChangesUtil;
@Inject
Delete(Provider<CurrentUser> self, StarredChangesUtil starredChangesUtil) {
this.self = self;
this.starredChangesUtil = starredChangesUtil;
}
@Override
public Response<?> apply(AccountResource.StarredChange rsrc, EmptyInput in)
throws AuthException, OrmException, IOException, IllegalLabelException {
if (!self.get().hasSameAccountId(rsrc.getUser())) {
throw new AuthException("not allowed remove starred change");
}
starredChangesUtil.star(
self.get().getAccountId(),
rsrc.getChange().getProject(),
rsrc.getChange().getId(),
null,
StarredChangesUtil.DEFAULT_LABELS);
return Response.none();
}
}
public static class EmptyInput {}
}