Search in sources :

Example 36 with MethodNotAllowedException

use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.

the class PreviewSubmit method apply.

@Override
public BinaryResult apply(RevisionResource rsrc) throws UpdateException, RestApiException {
    // to get access to a BatchUpdate.Factory.
    return retryHelper.execute((updateFactory) -> {
        if (Strings.isNullOrEmpty(format)) {
            throw new BadRequestException("format is not specified");
        }
        ArchiveFormat f = allowedFormats.extensions.get("." + format);
        if (f == null && format.equals("tgz")) {
            // Always allow tgz, even when the allowedFormats doesn't contain it.
            // Then we allow at least one format even if the list of allowed
            // formats is empty.
            f = ArchiveFormat.TGZ;
        }
        if (f == null) {
            throw new BadRequestException("unknown archive format");
        }
        Change change = rsrc.getChange();
        if (!change.getStatus().isOpen()) {
            throw new PreconditionFailedException("change is " + ChangeUtil.status(change));
        }
        ChangeControl control = rsrc.getControl();
        if (!control.getUser().isIdentifiedUser()) {
            throw new MethodNotAllowedException("Anonymous users cannot submit");
        }
        return getBundles(updateFactory, rsrc, f);
    });
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) ChangeControl(com.google.gerrit.server.project.ChangeControl) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PreconditionFailedException(com.google.gerrit.extensions.restapi.PreconditionFailedException) Change(com.google.gerrit.reviewdb.client.Change)

Example 37 with MethodNotAllowedException

use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.

the class DeleteVote method applyImpl.

@Override
protected Response<?> applyImpl(BatchUpdate.Factory updateFactory, VoteResource rsrc, DeleteVoteInput input) throws RestApiException, UpdateException {
    if (input == null) {
        input = new DeleteVoteInput();
    }
    if (input.label != null && !rsrc.getLabel().equals(input.label)) {
        throw new BadRequestException("label must match URL");
    }
    if (input.notify == null) {
        input.notify = NotifyHandling.ALL;
    }
    ReviewerResource r = rsrc.getReviewer();
    Change change = r.getChange();
    if (r.getRevisionResource() != null && !r.getRevisionResource().isCurrent()) {
        throw new MethodNotAllowedException("Cannot delete vote on non-current patch set");
    }
    try (BatchUpdate bu = updateFactory.create(db.get(), change.getProject(), r.getControl().getUser(), TimeUtil.nowTs())) {
        bu.addOp(change.getId(), new Op(r.getReviewerUser().getAccountId(), rsrc.getLabel(), input));
        bu.execute();
    }
    return Response.none();
}
Also used : DeleteVoteInput(com.google.gerrit.extensions.api.changes.DeleteVoteInput) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Change(com.google.gerrit.reviewdb.client.Change) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 38 with MethodNotAllowedException

use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.

the class DeleteChange method applyImpl.

@Override
protected Response<?> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, Input input) throws RestApiException, UpdateException, PermissionBackendException {
    if (rsrc.getChange().getStatus() == Change.Status.MERGED) {
        throw new MethodNotAllowedException("delete not permitted");
    } else if (!allowDrafts && rsrc.getChange().getStatus() == Change.Status.DRAFT) {
        // If drafts are disabled, only an administrator can delete a draft.
        try {
            permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
        } catch (AuthException e) {
            throw new MethodNotAllowedException("Draft workflow is disabled");
        }
    } else {
        rsrc.permissions().database(db).check(ChangePermission.DELETE);
    }
    try (BatchUpdate bu = updateFactory.create(db.get(), rsrc.getProject(), rsrc.getUser(), TimeUtil.nowTs())) {
        Change.Id id = rsrc.getChange().getId();
        bu.setOrder(Order.DB_BEFORE_REPO);
        bu.addOp(id, opProvider.get());
        bu.execute();
    }
    return Response.none();
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.reviewdb.client.Change) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 39 with MethodNotAllowedException

use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.

the class PutAgreement method apply.

@Override
public Response<String> apply(AccountResource resource, AgreementInput input) throws IOException, OrmException, RestApiException {
    if (!agreementsEnabled) {
        throw new MethodNotAllowedException("contributor agreements disabled");
    }
    if (self.get() != resource.getUser()) {
        throw new AuthException("not allowed to enter contributor agreement");
    }
    String agreementName = Strings.nullToEmpty(input.name);
    ContributorAgreement ca = projectCache.getAllProjects().getConfig().getContributorAgreement(agreementName);
    if (ca == null) {
        throw new UnprocessableEntityException("contributor agreement not found");
    }
    if (ca.getAutoVerify() == null) {
        throw new BadRequestException("cannot enter a non-autoVerify agreement");
    }
    AccountGroup.UUID uuid = ca.getAutoVerify().getUUID();
    if (uuid == null) {
        throw new ResourceConflictException("autoverify group uuid not found");
    }
    AccountGroup group = groupCache.get(uuid);
    if (group == null) {
        throw new ResourceConflictException("autoverify group not found");
    }
    Account account = self.get().getAccount();
    addMembers.addMembers(group.getId(), ImmutableList.of(account.getId()));
    agreementSignup.fire(account, agreementName);
    return Response.ok(agreementName);
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Account(com.google.gerrit.reviewdb.client.Account) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) ContributorAgreement(com.google.gerrit.common.data.ContributorAgreement) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 40 with MethodNotAllowedException

use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.

the class PutName method apply.

public Response<String> apply(IdentifiedUser user, Input input) throws MethodNotAllowedException, ResourceNotFoundException, OrmException, IOException {
    if (input == null) {
        input = new Input();
    }
    if (!realm.allowsEdit(AccountFieldName.FULL_NAME)) {
        throw new MethodNotAllowedException("realm does not allow editing name");
    }
    String newName = input.name;
    Account a = dbProvider.get().accounts().atomicUpdate(user.getAccountId(), new AtomicUpdate<Account>() {

        @Override
        public Account update(Account a) {
            a.setFullName(newName);
            return a;
        }
    });
    if (a == null) {
        throw new ResourceNotFoundException("account not found");
    }
    byIdCache.evict(a.getId());
    return Strings.isNullOrEmpty(a.getFullName()) ? Response.<String>none() : Response.ok(a.getFullName());
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) DefaultInput(com.google.gerrit.extensions.restapi.DefaultInput) Input(com.google.gerrit.server.account.PutName.Input) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Aggregations

MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)66 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)27 AuthException (com.google.gerrit.extensions.restapi.AuthException)23 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)16 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)16 Test (org.junit.Test)16 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)15 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)10 Account (com.google.gerrit.reviewdb.client.Account)9 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)8 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)7 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)7 Account (com.google.gerrit.entities.Account)6 GroupInfo (com.google.gerrit.extensions.common.GroupInfo)5 Response (com.google.gerrit.extensions.restapi.Response)5 Change (com.google.gerrit.reviewdb.client.Change)5 CurrentUser (com.google.gerrit.server.CurrentUser)5