use of com.google.gerrit.extensions.restapi.ResourceNotFoundException in project gerrit by GerritCodeReview.
the class PutDescription method apply.
@Override
public Response<String> apply(GroupResource resource, Input input) throws AuthException, MethodNotAllowedException, ResourceNotFoundException, OrmException, IOException {
if (input == null) {
// Delete would set description to null.
input = new Input();
}
if (resource.toAccountGroup() == null) {
throw new MethodNotAllowedException();
} else if (!resource.getControl().isOwner()) {
throw new AuthException("Not group owner");
}
AccountGroup group = db.get().accountGroups().get(resource.toAccountGroup().getId());
if (group == null) {
throw new ResourceNotFoundException();
}
group.setDescription(Strings.emptyToNull(input.description));
db.get().accountGroups().update(Collections.singleton(group));
groupCache.evict(group);
return Strings.isNullOrEmpty(input.description) ? Response.<String>none() : Response.ok(input.description);
}
use of com.google.gerrit.extensions.restapi.ResourceNotFoundException in project gerrit by GerritCodeReview.
the class PutOwner method apply.
@Override
public GroupInfo apply(GroupResource resource, Input input) throws ResourceNotFoundException, MethodNotAllowedException, AuthException, BadRequestException, UnprocessableEntityException, OrmException, IOException {
AccountGroup group = resource.toAccountGroup();
if (group == null) {
throw new MethodNotAllowedException();
} else if (!resource.getControl().isOwner()) {
throw new AuthException("Not group owner");
}
if (input == null || Strings.isNullOrEmpty(input.owner)) {
throw new BadRequestException("owner is required");
}
group = db.get().accountGroups().get(group.getId());
if (group == null) {
throw new ResourceNotFoundException();
}
GroupDescription.Basic owner = groupsCollection.parse(input.owner);
if (!group.getOwnerGroupUUID().equals(owner.getGroupUUID())) {
group.setOwnerGroupUUID(owner.getGroupUUID());
db.get().accountGroups().update(Collections.singleton(group));
groupCache.evict(group);
}
return json.format(owner);
}
use of com.google.gerrit.extensions.restapi.ResourceNotFoundException 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.ResourceNotFoundException in project gerrit by GerritCodeReview.
the class GroupsCollection method parse.
@Override
public GroupResource parse(TopLevelResource parent, IdString id) throws AuthException, ResourceNotFoundException {
final CurrentUser user = self.get();
if (user instanceof AnonymousUser) {
throw new AuthException("Authentication required");
} else if (!(user.isIdentifiedUser())) {
throw new ResourceNotFoundException(id);
}
GroupDescription.Basic group = parseId(id.get());
if (group == null) {
throw new ResourceNotFoundException(id.get());
}
GroupControl ctl = groupControlFactory.controlFor(group);
if (!ctl.isVisible()) {
throw new ResourceNotFoundException(id);
}
return new GroupResource(ctl);
}
use of com.google.gerrit.extensions.restapi.ResourceNotFoundException in project gerrit by GerritCodeReview.
the class SetHead method apply.
@Override
public String apply(final ProjectResource rsrc, Input input) throws AuthException, ResourceNotFoundException, BadRequestException, UnprocessableEntityException, IOException {
if (!rsrc.getControl().isOwner()) {
throw new AuthException("restricted to project owner");
}
if (input == null || Strings.isNullOrEmpty(input.ref)) {
throw new BadRequestException("ref required");
}
String ref = RefNames.fullName(input.ref);
try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
Map<String, Ref> cur = repo.getRefDatabase().exactRef(Constants.HEAD, ref);
if (!cur.containsKey(ref)) {
throw new UnprocessableEntityException(String.format("Ref Not Found: %s", ref));
}
final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
final String newHead = ref;
if (!oldHead.equals(newHead)) {
final RefUpdate u = repo.updateRef(Constants.HEAD, true);
u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
RefUpdate.Result res = u.link(newHead);
switch(res) {
case NO_CHANGE:
case RENAMED:
case FORCED:
case NEW:
break;
case FAST_FORWARD:
case IO_FAILURE:
case LOCK_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
default:
throw new IOException("Setting HEAD failed with " + res);
}
fire(rsrc.getNameKey(), oldHead, newHead);
}
return ref;
} catch (RepositoryNotFoundException e) {
throw new ResourceNotFoundException(rsrc.getName());
}
}
Aggregations