Search in sources :

Example 26 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class JdbcAccountPatchReviewStore method markReviewed.

@Override
public void markReviewed(PatchSet.Id psId, Account.Id accountId, Collection<String> paths) throws OrmException {
    if (paths == null || paths.isEmpty()) {
        return;
    }
    try (Connection con = ds.getConnection();
        PreparedStatement stmt = con.prepareStatement("INSERT INTO account_patch_reviews " + "(account_id, change_id, patch_set_id, file_name) VALUES " + "(?, ?, ?, ?)")) {
        for (String path : paths) {
            stmt.setInt(1, accountId.get());
            stmt.setInt(2, psId.getParentKey().get());
            stmt.setInt(3, psId.get());
            stmt.setString(4, path);
            stmt.addBatch();
        }
        stmt.executeBatch();
    } catch (SQLException e) {
        OrmException ormException = convertError("insert", e);
        if (ormException instanceof OrmDuplicateKeyException) {
            return;
        }
        throw ormException;
    }
}
Also used : SQLException(java.sql.SQLException) OrmException(com.google.gwtorm.server.OrmException) OrmDuplicateKeyException(com.google.gwtorm.server.OrmDuplicateKeyException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 27 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class RepoSequenceTest method failOnWrongType.

@Test
public void failOnWrongType() throws Exception {
    try (Repository repo = repoManager.openRepository(project)) {
        TestRepository<Repository> tr = new TestRepository<>(repo);
        tr.branch(RefNames.REFS_SEQUENCES + "id").commit().create();
        try {
            newSequence("id", 1, 3).next();
            fail();
        } catch (OrmException e) {
            assertThat(e.getCause()).isInstanceOf(ExecutionException.class);
            assertThat(e.getCause().getCause()).isInstanceOf(IncorrectObjectTypeException.class);
        }
    }
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestRepository(org.eclipse.jgit.junit.TestRepository) OrmException(com.google.gwtorm.server.OrmException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 28 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class ElasticChangeIndex method replace.

@Override
public void replace(ChangeData cd) throws IOException {
    String deleteIndex;
    String insertIndex;
    try {
        if (cd.change().getStatus().isOpen()) {
            insertIndex = OPEN_CHANGES;
            deleteIndex = CLOSED_CHANGES;
        } else {
            insertIndex = CLOSED_CHANGES;
            deleteIndex = OPEN_CHANGES;
        }
    } catch (OrmException e) {
        throw new IOException(e);
    }
    Bulk bulk = new Bulk.Builder().defaultIndex(indexName).defaultType("changes").addAction(insert(insertIndex, cd)).addAction(delete(deleteIndex, cd.getId())).refresh(true).build();
    JestResult result = client.execute(bulk);
    if (!result.isSucceeded()) {
        throw new IOException(String.format("Failed to replace change %s in index %s: %s", cd.getId(), indexName, result.getErrorMessage()));
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) Builder(io.searchbox.core.Bulk.Builder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) Bulk(io.searchbox.core.Bulk) JestResult(io.searchbox.client.JestResult)

Example 29 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class OAuthSessionOverOpenID method authenticateAndRedirect.

private void authenticateAndRedirect(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
    com.google.gerrit.server.account.AuthRequest areq = new com.google.gerrit.server.account.AuthRequest(ExternalId.Key.parse(user.getExternalId()));
    AuthResult arsp = null;
    try {
        String claimedIdentifier = user.getClaimedIdentity();
        Optional<Account.Id> actualId = accountManager.lookup(user.getExternalId());
        Optional<Account.Id> claimedId = Optional.empty();
        // That why we query it here, not to lose linking mode.
        if (!Strings.isNullOrEmpty(claimedIdentifier)) {
            claimedId = accountManager.lookup(claimedIdentifier);
            if (!claimedId.isPresent()) {
                log.debug("Claimed identity is unknown");
            }
        }
        // and user account exists for this identity
        if (claimedId.isPresent()) {
            log.debug("Claimed identity is set and is known");
            if (actualId.isPresent()) {
                if (claimedId.get().equals(actualId.get())) {
                    // Both link to the same account, that's what we expected.
                    log.debug("Both link to the same account. All is fine.");
                } else {
                    // This is (for now) a fatal error. There are two records
                    // for what might be the same user. The admin would have to
                    // link the accounts manually.
                    log.error("OAuth accounts disagree over user identity:\n" + "  Claimed ID: " + claimedId.get() + " is " + claimedIdentifier + "\n" + "  Delgate ID: " + actualId.get() + " is " + user.getExternalId());
                    rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
                    return;
                }
            } else {
                // Claimed account already exists: link to it.
                log.debug("Claimed account already exists: link to it.");
                try {
                    accountManager.link(claimedId.get(), areq);
                } catch (OrmException | ConfigInvalidException e) {
                    log.error("Cannot link: " + user.getExternalId() + " to user identity:\n" + "  Claimed ID: " + claimedId.get() + " is " + claimedIdentifier);
                    rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
                    return;
                }
            }
        } else if (linkMode) {
            // Use case 2: link mode activated from the UI
            Account.Id accountId = identifiedUser.get().getAccountId();
            try {
                log.debug("Linking \"{}\" to \"{}\"", user.getExternalId(), accountId);
                accountManager.link(accountId, areq);
            } catch (OrmException | ConfigInvalidException e) {
                log.error("Cannot link: " + user.getExternalId() + " to user identity: " + accountId);
                rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
                return;
            } finally {
                linkMode = false;
            }
        }
        areq.setUserName(user.getUserName());
        areq.setEmailAddress(user.getEmailAddress());
        areq.setDisplayName(user.getDisplayName());
        arsp = accountManager.authenticate(areq);
    } catch (AccountException e) {
        log.error("Unable to authenticate user \"" + user + "\"", e);
        rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    webSession.get().login(arsp, true);
    StringBuilder rdr = new StringBuilder(urlProvider.get(req));
    rdr.append(Url.decode(redirectToken));
    rsp.sendRedirect(rdr.toString());
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) AuthResult(com.google.gerrit.server.account.AuthResult) AccountException(com.google.gerrit.server.account.AccountException) OrmException(com.google.gwtorm.server.OrmException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId)

Example 30 with OrmException

use of com.google.gwtorm.server.OrmException in project gerrit by GerritCodeReview.

the class LuceneChangeIndex method replace.

@Override
public void replace(ChangeData cd) throws IOException {
    Term id = LuceneChangeIndex.idTerm(cd);
    // toDocument is essentially static and doesn't depend on the specific
    // sub-index, so just pick one.
    Document doc = openIndex.toDocument(cd, fillArgs);
    try {
        if (cd.change().getStatus().isOpen()) {
            Futures.allAsList(closedIndex.delete(id), openIndex.replace(id, doc)).get();
        } else {
            Futures.allAsList(openIndex.delete(id), closedIndex.replace(id, doc)).get();
        }
    } catch (OrmException | ExecutionException | InterruptedException e) {
        throw new IOException(e);
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

OrmException (com.google.gwtorm.server.OrmException)172 IOException (java.io.IOException)78 Change (com.google.gerrit.reviewdb.client.Change)50 Repository (org.eclipse.jgit.lib.Repository)41 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)33 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)31 ObjectId (org.eclipse.jgit.lib.ObjectId)29 Account (com.google.gerrit.reviewdb.client.Account)28 RevWalk (org.eclipse.jgit.revwalk.RevWalk)28 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)24 ChangeData (com.google.gerrit.server.query.change.ChangeData)24 Map (java.util.Map)22 ArrayList (java.util.ArrayList)21 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)20 Inject (com.google.inject.Inject)18 Provider (com.google.inject.Provider)17 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)16 Set (java.util.Set)16 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)15 CurrentUser (com.google.gerrit.server.CurrentUser)14