use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class QueryGroups method apply.
@Override
public List<GroupInfo> apply(TopLevelResource resource) throws BadRequestException, MethodNotAllowedException, OrmException {
if (Strings.isNullOrEmpty(query)) {
throw new BadRequestException("missing query field");
}
GroupIndex searchIndex = indexes.getSearchIndex();
if (searchIndex == null) {
throw new MethodNotAllowedException("no group index");
}
if (start != 0) {
queryProcessor.setStart(start);
}
if (limit != 0) {
queryProcessor.setLimit(limit);
}
try {
QueryResult<AccountGroup> result = queryProcessor.query(queryBuilder.parse(query));
List<AccountGroup> groups = result.entities();
ArrayList<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groups.size());
json.addOptions(options);
for (AccountGroup group : groups) {
groupInfos.add(json.format(GroupDescriptions.forAccountGroup(group)));
}
if (!groupInfos.isEmpty() && result.more()) {
groupInfos.get(groupInfos.size() - 1)._moreGroups = true;
}
return groupInfos;
} catch (QueryParseException e) {
throw new BadRequestException(e.getMessage());
}
}
use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class ListIncludedGroups method apply.
@Override
public List<GroupInfo> apply(GroupResource rsrc) throws MethodNotAllowedException, OrmException {
if (rsrc.toAccountGroup() == null) {
throw new MethodNotAllowedException();
}
boolean ownerOfParent = rsrc.getControl().isOwner();
List<GroupInfo> included = new ArrayList<>();
for (AccountGroupById u : dbProvider.get().accountGroupById().byGroup(rsrc.toAccountGroup().getId())) {
try {
GroupControl i = controlFactory.controlFor(u.getIncludeUUID());
if (ownerOfParent || i.isVisible()) {
included.add(json.format(i.getGroup()));
}
} catch (NoSuchGroupException notFound) {
log.warn(String.format("Group %s no longer available, included into %s", u.getIncludeUUID(), rsrc.getGroup().getName()));
continue;
}
}
Collections.sort(included, new Comparator<GroupInfo>() {
@Override
public int compare(GroupInfo a, GroupInfo b) {
int cmp = nullToEmpty(a.name).compareTo(nullToEmpty(b.name));
if (cmp != 0) {
return cmp;
}
return nullToEmpty(a.id).compareTo(nullToEmpty(b.id));
}
});
return included;
}
use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class MembersCollection method parse.
@Override
public MemberResource parse(GroupResource parent, IdString id) throws MethodNotAllowedException, AuthException, ResourceNotFoundException, OrmException {
if (parent.toAccountGroup() == null) {
throw new MethodNotAllowedException();
}
IdentifiedUser user = accounts.parse(TopLevelResource.INSTANCE, id).getUser();
AccountGroupMember.Key key = new AccountGroupMember.Key(user.getAccountId(), parent.toAccountGroup().getId());
if (db.get().accountGroupMembers().get(key) != null && parent.getControl().canSeeMember(user.getAccountId())) {
return new MemberResource(parent, user);
}
throw new ResourceNotFoundException(id);
}
use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class DeleteMembers method apply.
@Override
public Response<?> apply(GroupResource resource, Input input) throws AuthException, MethodNotAllowedException, UnprocessableEntityException, OrmException, IOException {
AccountGroup internalGroup = resource.toAccountGroup();
if (internalGroup == null) {
throw new MethodNotAllowedException();
}
input = Input.init(input);
final GroupControl control = resource.getControl();
final Map<Account.Id, AccountGroupMember> members = getMembers(internalGroup.getId());
final List<AccountGroupMember> toRemove = new ArrayList<>();
for (final String nameOrEmail : input.members) {
Account a = accounts.parse(nameOrEmail).getAccount();
if (!control.canRemoveMember()) {
throw new AuthException("Cannot delete member: " + a.getFullName());
}
final AccountGroupMember m = members.remove(a.getId());
if (m != null) {
toRemove.add(m);
}
}
writeAudits(toRemove);
db.get().accountGroupMembers().delete(toRemove);
for (final AccountGroupMember m : toRemove) {
accountCache.evict(m.getAccountId());
}
return Response.none();
}
use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class QueryGroups method apply.
@Override
public Response<List<GroupInfo>> apply(TopLevelResource resource) throws BadRequestException, MethodNotAllowedException, PermissionBackendException {
if (Strings.isNullOrEmpty(query)) {
throw new BadRequestException("missing query field");
}
GroupQueryProcessor queryProcessor = queryProcessorProvider.get();
if (queryProcessor.isDisabled()) {
throw new MethodNotAllowedException("query disabled");
}
if (start != 0) {
queryProcessor.setStart(start);
}
if (limit != 0) {
queryProcessor.setUserProvidedLimit(limit);
}
try {
QueryResult<InternalGroup> result = queryProcessor.query(queryBuilder.parse(query));
List<InternalGroup> groups = result.entities();
ArrayList<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groups.size());
json.addOptions(options);
for (InternalGroup group : groups) {
groupInfos.add(json.format(new InternalGroupDescription(group)));
}
if (!groupInfos.isEmpty() && result.more()) {
groupInfos.get(groupInfos.size() - 1)._moreGroups = true;
}
return Response.ok(groupInfos);
} catch (QueryParseException e) {
throw new BadRequestException(e.getMessage());
}
}
Aggregations