Search in sources :

Example 96 with ServiceLayerException

use of org.craftercms.studio.api.v1.exception.ServiceLayerException in project studio by craftercms.

the class RepositoryManagementController method getDiffForConflictedFile.

@GetMapping(DIFF_CONFLICTED_FILE)
public ResponseBody getDiffForConflictedFile(@RequestParam(value = REQUEST_PARAM_SITEID) String siteId, @RequestParam(value = REQUEST_PARAM_PATH) String path) throws ServiceLayerException, CryptoException {
    if (!siteService.exists(siteId)) {
        throw new SiteNotFoundException(siteId);
    }
    String diffPath = path;
    if (!diffPath.startsWith(FILE_SEPARATOR)) {
        diffPath = FILE_SEPARATOR + diffPath;
    }
    DiffConflictedFile diff = repositoryManagementService.getDiffForConflictedFile(siteId, diffPath);
    ResponseBody responseBody = new ResponseBody();
    ResultOne<DiffConflictedFile> result = new ResultOne<DiffConflictedFile>();
    result.setEntity(RESULT_KEY_DIFF, diff);
    result.setResponse(OK);
    responseBody.setResult(result);
    return responseBody;
}
Also used : ResultOne(org.craftercms.studio.model.rest.ResultOne) DiffConflictedFile(org.craftercms.studio.api.v2.dal.DiffConflictedFile) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 97 with ServiceLayerException

use of org.craftercms.studio.api.v1.exception.ServiceLayerException 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 98 with ServiceLayerException

use of org.craftercms.studio.api.v1.exception.ServiceLayerException 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 99 with ServiceLayerException

use of org.craftercms.studio.api.v1.exception.ServiceLayerException in project studio by craftercms.

the class SitesController method getAvailableBlueprints.

@GetMapping("/available_blueprints")
public ResponseBody getAvailableBlueprints() throws ServiceLayerException {
    List<PluginDescriptor> blueprintDescriptors = null;
    try {
        blueprintDescriptors = sitesService.getAvailableBlueprints();
    } catch (Exception e) {
        throw new ServiceLayerException(e);
    }
    ResponseBody responseBody = new ResponseBody();
    ResultList<PluginDescriptor> result = new ResultList<>();
    result.setEntities(RESULT_KEY_BLUEPRINTS, blueprintDescriptors);
    result.setResponse(ApiResponse.OK);
    responseBody.setResult(result);
    return responseBody;
}
Also used : PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) ResultList(org.craftercms.studio.model.rest.ResultList) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) RemoteRepositoryNotBareException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 100 with ServiceLayerException

use of org.craftercms.studio.api.v1.exception.ServiceLayerException in project studio by craftercms.

the class FormDmContentProcessor method writeContent.

protected void writeContent(PipelineContent content, ResultTO result) throws ServiceLayerException {
    String user = content.getProperty(DmConstants.KEY_USER);
    String site = content.getProperty(DmConstants.KEY_SITE);
    String path = content.getProperty(DmConstants.KEY_PATH);
    String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
    String contentType = content.getProperty(DmConstants.KEY_CONTENT_TYPE);
    InputStream input = content.getContentStream();
    boolean isPreview = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_IS_PREVIEW));
    boolean createFolders = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_CREATE_FOLDERS));
    String unlockValue = content.getProperty(DmConstants.KEY_UNLOCK);
    boolean unlock = (!StringUtils.isEmpty(unlockValue) && unlockValue.equalsIgnoreCase("false")) ? false : true;
    String parentContentPath = path;
    if (parentContentPath.endsWith(FILE_SEPARATOR + fileName)) {
        parentContentPath = parentContentPath.replace(FILE_SEPARATOR + fileName, "");
    } else {
        path = path + FILE_SEPARATOR + fileName;
    }
    try {
        // look up the path content first
        ContentItemTO parentItem = contentService.getContentItem(site, parentContentPath, 0);
        boolean parentContentExists = contentService.contentExists(site, parentContentPath);
        if (!parentContentExists && createFolders) {
            parentItem = createMissingFoldersInPath(site, path, isPreview);
        }
        if (parentItem != null) {
            // look up the path content first
            if (parentItem.getName().equals(fileName)) {
                ContentItemTO item = contentService.getContentItem(site, path, 0);
                InputStream existingContent = contentService.getContent(site, path);
                updateFile(site, item, path, input, user, isPreview, unlock, result);
                content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_UPDATE);
                if (unlock) {
                    // TODO: We need ability to lock/unlock content in repo
                    contentService.unLockContent(site, path);
                    logger.debug("Unlocked the content " + parentContentPath);
                }
                return;
            } else {
                // otherwise, create new one
                if (path.endsWith(DmConstants.XML_PATTERN) && !path.endsWith(DmConstants.INDEX_FILE)) {
                    parentContentPath = path.substring(0, path.lastIndexOf(FILE_SEPARATOR));
                    parentItem = contentService.getContentItem(site, parentContentPath, 0);
                }
                boolean fileExists = contentService.contentExists(site, path);
                if (fileExists) {
                    ContentItemTO contentItem = contentService.getContentItem(site, path, 0);
                    InputStream existingContent = contentService.getContent(site, path);
                    updateFile(site, contentItem, path, input, user, isPreview, unlock, result);
                    content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_UPDATE);
                    if (unlock) {
                        // TODO: We need ability to lock/unlock content in repo
                        contentService.unLockContent(site, path);
                        logger.debug("Unlocked the content site: " + site + " path: " + path);
                    }
                    return;
                } else {
                    ContentItemTO newFileItem = createNewFile(site, parentItem, fileName, contentType, input, user, unlock, result);
                    content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_CREATE);
                    return;
                }
            }
        } else {
            throw new ContentNotFoundException(path + " does not exist in site: " + site);
        }
    } catch (ContentNotFoundException | RepositoryLockedException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Error: ", e);
        throw new ContentNotFoundException("Unexpected exception ", e);
    } finally {
        ContentUtils.release(input);
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) InputStream(java.io.InputStream) ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException)

Aggregations

ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)124 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)62 HashMap (java.util.HashMap)55 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)39 User (org.craftercms.studio.api.v2.dal.User)36 Repository (org.eclipse.jgit.lib.Repository)35 Git (org.eclipse.jgit.api.Git)33 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)33 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)32 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)30 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)29 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)29 CryptoException (org.craftercms.commons.crypto.CryptoException)28 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)27 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)24 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)23 Path (java.nio.file.Path)21 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)21 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)20