Search in sources :

Example 6 with ResourceNotFoundException

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);
}
Also used : DefaultInput(com.google.gerrit.extensions.restapi.DefaultInput) Input(com.google.gerrit.server.group.PutDescription.Input) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 7 with ResourceNotFoundException

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);
}
Also used : GroupDescription(com.google.gerrit.common.data.GroupDescription) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 8 with ResourceNotFoundException

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);
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 9 with ResourceNotFoundException

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);
}
Also used : GroupDescription(com.google.gerrit.common.data.GroupDescription) GroupControl(com.google.gerrit.server.account.GroupControl) CurrentUser(com.google.gerrit.server.CurrentUser) AuthException(com.google.gerrit.extensions.restapi.AuthException) AnonymousUser(com.google.gerrit.server.AnonymousUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 10 with ResourceNotFoundException

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());
    }
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Aggregations

ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)75 IdString (com.google.gerrit.extensions.restapi.IdString)18 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)17 AuthException (com.google.gerrit.extensions.restapi.AuthException)15 Repository (org.eclipse.jgit.lib.Repository)14 Project (com.google.gerrit.reviewdb.client.Project)13 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)12 Account (com.google.gerrit.reviewdb.client.Account)11 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 RevWalk (org.eclipse.jgit.revwalk.RevWalk)11 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)10 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)10 IOException (java.io.IOException)9 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)9 ObjectId (org.eclipse.jgit.lib.ObjectId)9 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)8 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)8 CurrentUser (com.google.gerrit.server.CurrentUser)7 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)7 ArrayList (java.util.ArrayList)7