Search in sources :

Example 16 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitContentRepository method writeContent.

@Override
public String writeContent(String site, String path, InputStream content) {
    // Write content to git and commit it
    String commitId = null;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
            Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
            if (repo != null) {
                if (helper.writeFile(repo, site, path, content)) {
                    PersonIdent user = helper.getCurrentUserIdent();
                    String username = securityService.getCurrentUser();
                    String comment = helper.getCommitMessage(REPO_SANDBOX_WRITE_COMMIT_MESSAGE).replace(REPO_COMMIT_MESSAGE_USERNAME_VAR, username).replace(REPO_COMMIT_MESSAGE_PATH_VAR, path);
                    commitId = helper.commitFile(repo, site, path, comment, user);
                } else {
                    logger.error("Failed to write content site: " + site + " path: " + path);
                }
            } else {
                logger.error("Missing repository during write for site: " + site + " path: " + path);
            }
        }
    } catch (ServiceLayerException | UserNotFoundException | CryptoException e) {
        logger.error("Unknown service error during write for site: " + site + " path: " + path, e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return commitId;
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 17 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitContentRepository method deleteContent.

@Override
public String deleteContent(String site, String path, String approver) {
    String commitId = null;
    boolean isPage = path.endsWith(FILE_SEPARATOR + INDEX_FILE);
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
            Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
            try (Git git = new Git(repo)) {
                String pathToDelete = helper.getGitPath(path);
                Path parentToDelete = Paths.get(pathToDelete).getParent();
                RmCommand rmCommand = git.rm().addFilepattern(pathToDelete).setCached(false);
                retryingRepositoryOperationFacade.call(rmCommand);
                String pathToCommit = pathToDelete;
                if (isPage) {
                    pathToCommit = deleteParentFolder(git, parentToDelete, true);
                }
                // TODO: SJ: we need to define messages in a string table of sorts
                commitId = helper.commitFile(repo, site, pathToCommit, helper.getCommitMessage(REPO_DELETE_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_PATH, path), StringUtils.isEmpty(approver) ? helper.getCurrentUserIdent() : helper.getAuthorIdent(approver));
            }
        }
    } catch (GitAPIException | UserNotFoundException | IOException e) {
        logger.error("Error while deleting content for site: " + site + " path: " + path, e);
    } catch (ServiceLayerException | CryptoException e) {
        logger.error("Unknown service error during delete for site: " + site + " path: " + path, e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return commitId;
}
Also used : Path(java.nio.file.Path) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) RmCommand(org.eclipse.jgit.api.RmCommand) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 18 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitContentRepository method createFolder.

@Override
public String createFolder(String site, String path, String name) {
    // SJ: Git doesn't care about empty folders, so we will create the folders and put a 0 byte file in them
    String commitId = null;
    boolean result;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
            Path emptyFilePath = Paths.get(path, name, EMPTY_FILE);
            Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
            try {
                // Create basic file
                File file = new File(repo.getDirectory().getParent(), emptyFilePath.toString());
                // Create parent folders
                File folder = file.getParentFile();
                if (folder != null) {
                    if (!folder.exists()) {
                        folder.mkdirs();
                    }
                }
                // Create the file
                if (!file.createNewFile()) {
                    logger.error("error writing file: site: " + site + " path: " + emptyFilePath);
                    result = false;
                } else {
                    // Add the file to git
                    try (Git git = new Git(repo)) {
                        AddCommand addCommand = git.add().addFilepattern(helper.getGitPath(emptyFilePath.toString()));
                        retryingRepositoryOperationFacade.call(addCommand);
                        git.close();
                        result = true;
                    } catch (GitAPIException e) {
                        logger.error("error adding file to git: site: " + site + " path: " + emptyFilePath, e);
                        result = false;
                    }
                }
            } catch (IOException e) {
                logger.error("error writing file: site: " + site + " path: " + emptyFilePath, e);
                result = false;
            }
            if (result) {
                try {
                    commitId = helper.commitFile(repo, site, emptyFilePath.toString(), helper.getCommitMessage(REPO_CREATE_FOLDER_COMMIT_MESSAGE).replaceAll(PATTERN_SITE, site).replaceAll(PATTERN_PATH, path + FILE_SEPARATOR + name), helper.getCurrentUserIdent());
                } catch (ServiceLayerException | UserNotFoundException e) {
                    logger.error("Unknown service error during commit for site: " + site + " path: " + emptyFilePath, e);
                }
            }
        }
    } catch (CryptoException e) {
        logger.error("Unexpected service error creating folder " + name + " for site: " + site + " path: " + path, e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return commitId;
}
Also used : Path(java.nio.file.Path) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) File(java.io.File) LockFile(org.eclipse.jgit.internal.storage.file.LockFile) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) AddCommand(org.eclipse.jgit.api.AddCommand)

Example 19 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitContentRepository method createVersion.

@Override
public String createVersion(String site, String path, String comment, boolean majorVersion) {
    // SJ: Will ignore minor revisions since git handles that via write/commit
    // SJ: Major revisions become git tags
    // TODO: SJ: Redesign/refactor the whole approach in 3.1+
    String toReturn = EMPTY;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        if (majorVersion) {
            synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : PUBLISHED)) {
                Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : PUBLISHED);
                // Tag the repository with a date-time based version label
                String gitPath = helper.getGitPath(path);
                try (Git git = new Git(repo)) {
                    PersonIdent currentUserIdent = helper.getCurrentUserIdent();
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HHmmssX");
                    Calendar cal = Calendar.getInstance();
                    String versionLabel = dateFormat.format(cal.getTime());
                    TagCommand tagCommand = git.tag().setName(versionLabel).setMessage(comment).setTagger(currentUserIdent);
                    retryingRepositoryOperationFacade.call(tagCommand);
                    toReturn = versionLabel;
                } catch (GitAPIException | ServiceLayerException | UserNotFoundException err) {
                    logger.error("error creating new version for site:  " + site + " path: " + path, err);
                }
            }
        } else {
            logger.info("request to create minor revision ignored for site: " + site + " path: " + path);
        }
    } catch (CryptoException e) {
        logger.error("Unexpected error creating new version for site:  " + site + " path: " + path, e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return toReturn;
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) Calendar(java.util.Calendar) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) TagCommand(org.eclipse.jgit.api.TagCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) PersonIdent(org.eclipse.jgit.lib.PersonIdent) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 20 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitContentRepository method deleteSite.

@Override
public boolean deleteSite(String site) {
    boolean toReturn;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repository = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
        if (repository != null) {
            synchronized (repository) {
                Repository publishedRepository = helper.getRepository(site, PUBLISHED);
                if (publishedRepository != null) {
                    synchronized (publishedRepository) {
                        toReturn = helper.deleteSiteGitRepo(site);
                    }
                } else {
                    toReturn = helper.deleteSiteGitRepo(site);
                }
            }
        } else {
            Path sitePath = Paths.get(studioConfiguration.getProperty(REPO_BASE_PATH), studioConfiguration.getProperty(SITES_REPOS_PATH), site);
            try {
                FileUtils.deleteDirectory(sitePath.toFile());
                toReturn = true;
            } catch (IOException e) {
                logger.error("Error while deleting site " + site, e);
                toReturn = false;
            }
        }
    } catch (CryptoException e) {
        logger.error("Error while deleting site " + site, e);
        toReturn = false;
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return toReturn;
}
Also used : Path(java.nio.file.Path) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

Aggregations

Repository (org.eclipse.jgit.lib.Repository)25 Git (org.eclipse.jgit.api.Git)23 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)23 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)21 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)21 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)20 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)20 IOException (java.io.IOException)17 CryptoException (org.craftercms.commons.crypto.CryptoException)16 Path (java.nio.file.Path)14 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)12 File (java.io.File)7 CommitCommand (org.eclipse.jgit.api.CommitCommand)6 Status (org.eclipse.jgit.api.Status)6 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)5 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)5 InvalidRemoteRepositoryCredentialsException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException)4 InvalidRemoteRepositoryException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException)4 RemoteRepositoryNotFoundException (org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException)4 AddCommand (org.eclipse.jgit.api.AddCommand)4