Example usage for org.eclipse.jgit.lfs.errors LfsException LfsException

List of usage examples for org.eclipse.jgit.lfs.errors LfsException LfsException

Introduction

In this page you can find the example usage for org.eclipse.jgit.lfs.errors LfsException LfsException.

Prototype

public LfsException(String message) 

Source Link

Document

Constructor for LfsException.

Usage

From source file:com.googlesource.gerrit.plugins.lfs.LfsApiServlet.java

License:Apache License

@Override
protected LargeFileRepository getLargeFileRepository(LfsRequest request, String path, String auth)
        throws LfsException {
    String pathInfo = path.startsWith("/") ? path : "/" + path;
    Matcher matcher = URL_PATTERN.matcher(pathInfo);
    if (!matcher.matches()) {
        throw new LfsException("no repository at " + pathInfo);
    }//from w  ww . j  a v  a2  s .  c o m
    String projName = matcher.group(1);
    Project.NameKey project = Project.NameKey.parse(ProjectUtil.stripGitSuffix(projName));
    ProjectState state = projectCache.get(project);
    if (state == null || state.getProject().getState() == HIDDEN) {
        throw new LfsRepositoryNotFound(project.get());
    }
    authorizeUser(userProvider.getUser(auth, projName, request.getOperation()), state, request);

    if (request.isUpload() && state.getProject().getState() == READ_ONLY) {
        throw new LfsRepositoryReadOnly(project.get());
    }

    LfsProjectConfigSection config = lfsConfigFactory.getProjectsConfig().getForProject(project);
    // Only accept requests for projects where LFS is enabled.
    // No config means we default to "not enabled".
    if (config != null && config.isEnabled()) {
        // For uploads, check object sizes against limit if configured
        if (request.isUpload()) {
            if (config.isReadOnly()) {
                throw new LfsRepositoryReadOnly(project.get());
            }

            long maxObjectSize = config.getMaxObjectSize();
            if (maxObjectSize > 0) {
                for (LfsObject object : request.getObjects()) {
                    if (object.getSize() > maxObjectSize) {
                        throw new LfsValidationError(
                                String.format("size of object %s (%d bytes) exceeds limit (%d bytes)",
                                        object.getOid(), object.getSize(), maxObjectSize));
                    }
                }
            }
        }

        return repoResolver.get(project, config.getBackend());
    }

    throw new LfsUnavailable(project.get());
}

From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsGetLocksAction.java

License:Apache License

@Override
protected String getProjectName() throws LfsException {
    Matcher matcher = LFS_LOCKS_URL_PATTERN.matcher(context.path);
    if (matcher.matches()) {
        return matcher.group(1);
    }//from w  w  w . j  av a  2s .  c om

    throw new LfsException("no repository at " + context.path);
}

From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsLocksHandler.java

License:Apache License

LfsLockResponse deleteLock(Project.NameKey project, CurrentUser user, String lockId, LfsDeleteLockInput input)
        throws LfsException {
    log.atFine().log("Delete (-f %s) lock for %s in project %s", Boolean.TRUE.equals(input.force), lockId,
            project);/*w ww .  j  a  va  2 s  .  com*/
    LfsProjectLocks locks = projectLocks.getUnchecked(project);
    Optional<LfsLock> hasLock = locks.getLock(lockId);
    if (!hasLock.isPresent()) {
        throw new LfsException(String.format("there is no lock id %s in project %s", lockId, project));
    }

    LfsLock lock = hasLock.get();
    if (lock.owner.name.equals(user.getUserName().get())) {
        locks.deleteLock(lock);
        return new LfsLockResponse(lock);
    } else if (input.force) {
        locks.deleteLock(lock);
        return new LfsLockResponse(lock);
    }

    throw new LfsException(String.format("Lock %s is owned by different user %s", lockId, lock.owner.name));
}

From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsProjectLocks.java

License:Apache License

LfsLock createLock(CurrentUser user, LfsCreateLockInput input) throws LfsException {
    log.atFine().log("Create lock for %s in project %s", input.path, project);
    String lockId = toLockId.apply(input.path);
    LfsLock lock = locks.getIfPresent(lockId);
    if (lock != null) {
        throw new LfsLockExistsException(lock);
    }//from w ww .j  a va  2 s .c o  m

    lock = new LfsLock(lockId, input.path, LfsDateTime.now(), new LfsLockOwner(user.getUserName().get()));
    LockFile fileLock = new LockFile(locksPath.resolve(lockId).toFile());
    try {
        if (!fileLock.lock()) {
            log.atWarning().log("Cannot lock path [%s] in project %s", input.path, project);
            throw new LfsLockExistsException(lock);
        }
    } catch (IOException e) {
        String error = String.format("Locking path [%s] in project %s failed with error %s", input.path,
                project, e.getMessage());
        log.atWarning().log(error);
        throw new LfsException(error);
    }

    try {
        try (OutputStreamWriter out = new OutputStreamWriter(fileLock.getOutputStream())) {
            gson.toJson(lock, out);
        } catch (IOException e) {
            String error = String.format("Locking path [%s] in project %s failed during write with error %s",
                    input.path, project, e.getMessage());
            log.atWarning().log(error);
            throw new LfsException(error);
        }
        if (!fileLock.commit()) {
            String error = String.format("Committing lock to path [%s] in project %s failed", input.path,
                    project);
            log.atWarning().log(error);
            throw new LfsException(error);
        }
        // put lock object to cache while file lock is being hold so that
        // there is no chance that other process performs lock operation
        // in the meantime (either cache returns with existing object or
        // LockFile.lock fails on locking attempt)
        locks.put(lockId, lock);
    } finally {
        fileLock.unlock();
    }

    return lock;
}

From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsProjectLocks.java

License:Apache License

void deleteLock(LfsLock lock) throws LfsException {
    LockFile fileLock = new LockFile(locksPath.resolve(lock.id).toFile());
    try {/*  w ww .  ja  v  a 2s  .  c  om*/
        if (!fileLock.lock()) {
            String error = String.format("Deleting lock on path [%s] in project %s is not possible", lock.path,
                    project);
            log.atWarning().log(error);
            throw new LfsException(error);
        }
    } catch (IOException e) {
        String error = String.format("Getting lock on path [%s] in project %s failed with error %s", lock.path,
                project, e.getMessage());
        log.atWarning().log(error);
        throw new LfsException(error);
    }

    try {
        Files.deleteIfExists(locksPath.resolve(lock.id));
        locks.invalidate(lock.id);
    } catch (IOException e) {
        String error = String.format("Deleting lock on path [%s] in project %s failed with error %s", lock.path,
                project, e.getMessage());
        log.atWarning().log(error);
        throw new LfsException(error);
    } finally {
        fileLock.unlock();
    }
}

From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsPutLocksAction.java

License:Apache License

@Override
protected String getProjectName() throws LfsException {
    Matcher matcher = LFS_LOCKS_URL_PATTERN.matcher(context.path);
    if (matcher.matches()) {
        String project = matcher.group(1);
        String lockId = matcher.group(2);
        if (Strings.isNullOrEmpty(lockId)) {
            action = new CreateLock();
        } else {/*from  w  w  w  .  j av a  2s  .  c  o  m*/
            action = new DeleteLock(lockId);
        }
        return project;
    }

    matcher = LFS_VERIFICATION_URL_PATTERN.matcher(context.path);
    if (matcher.matches()) {
        action = new VerifyLock();
        return matcher.group(1);
    }

    throw new LfsException(String.format("Unsupported path %s was provided", context.path));
}