Search in sources :

Example 6 with SiteNotFoundException

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

the class StudioClusterSandboxRepoSyncTask method executeInternal.

@Override
protected void executeInternal(String siteId) {
    // Log start time
    long startTime = System.currentTimeMillis();
    logger.debug("Worker starts syncing cluster node sandbox for site " + siteId);
    try {
        HierarchicalConfiguration<ImmutableNode> registrationData = studioClusterUtils.getClusterConfiguration();
        if (registrationData != null && !registrationData.isEmpty()) {
            String localAddress = studioClusterUtils.getClusterNodeLocalAddress();
            ClusterMember localNode = clusterDao.getMemberByLocalAddress(localAddress);
            List<ClusterMember> clusterNodes = studioClusterUtils.getClusterNodes(localAddress);
            SiteFeed siteFeed = siteService.getSite(siteId);
            List<ClusterSiteRecord> clusterSiteRecords = clusterDao.getSiteStateAcrossCluster(siteId);
            long nodesCreated = clusterSiteRecords.stream().filter(x -> StringUtils.equals(x.getState(), STATE_CREATED)).count();
            if (nodesCreated < 1 && !StringUtils.equals(siteFeed.getState(), STATE_CREATED)) {
                return;
            }
            // Check if site exists
            logger.debug("Check if site " + siteId + " exists in local repository");
            boolean success = true;
            boolean siteCheck = checkIfSiteRepoExists(siteId);
            if (!siteCheck) {
                // Site doesn't exist locally, create it
                success = createSite(localNode.getId(), siteFeed.getId(), siteId, siteFeed.getSiteUuid(), siteFeed.getSearchEngine(), clusterNodes, clusterSiteRecords);
            }
            if (success && clusterDao.existsClusterSiteSyncRepo(localNode.getId(), siteFeed.getId()) < 1) {
                String commitId = contentRepository.getRepoLastCommitId(siteId);
                clusterDao.insertClusterSiteSyncRepo(localNode.getId(), siteFeed.getId(), commitId, commitId, siteFeed.getLastSyncedGitlogCommitId());
                clusterDao.setSiteState(localNode.getId(), siteFeed.getId(), STATE_CREATED);
                addSiteUuidFile(siteId, siteFeed.getSiteUuid());
            }
            if (success) {
                syncRemoteRepositories(siteId, localAddress);
                // Check if the site needs to be synced
                boolean syncRequired = isSyncRequired(siteId, siteFeed.getLastCommitId());
                if (syncRequired) {
                    try {
                        // Add the remote repositories to the local repository to sync from if not added already
                        logger.debug("Add remotes for site " + siteId);
                        addRemotes(siteId, clusterNodes);
                    } catch (InvalidRemoteUrlException | ServiceLayerException e) {
                        logger.error("Error while adding remotes on cluster node for site " + siteId);
                    }
                    try {
                        // Sync with remote and update the local cache with the last commit ID to speed things up
                        logger.debug("Update content for site " + siteId);
                        updateContent(localNode.getId(), siteFeed.getId(), siteId, siteFeed.getSandboxBranch(), clusterNodes);
                    } catch (IOException | CryptoException | ServiceLayerException e) {
                        logger.error("Error while updating content for site " + siteId + " on cluster node.", e);
                    }
                }
            }
        }
    } catch (SiteNotFoundException | IOException e) {
        logger.error("Error while executing Cluster Node Sync Sandbox for site " + siteId, e);
    }
    // Compute execution duration and log it
    long duration = System.currentTimeMillis() - startTime;
    logger.debug("Worker finished syncing cluster node for site " + siteId);
    logger.debug("Worker performed cluster node sync for site " + siteId + " in " + duration + "ms");
    logger.debug("Finished Cluster Node Sync task for site " + siteId);
}
Also used : PullCommand(org.eclipse.jgit.api.PullCommand) URISyntaxException(java.net.URISyntaxException) SITES_REPOS_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.SITES_REPOS_PATH) StringUtils(org.apache.commons.lang3.StringUtils) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) Config(org.eclipse.jgit.lib.Config) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) Map(java.util.Map) URIish(org.eclipse.jgit.transport.URIish) ClusterDAO(org.craftercms.studio.api.v2.dal.ClusterDAO) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) Path(java.nio.file.Path) STATE_CREATED(org.craftercms.studio.api.v1.dal.SiteFeed.STATE_CREATED) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) Set(java.util.Set) SiteService(org.craftercms.studio.api.v1.service.site.SiteService) UUID(java.util.UUID) SITE_SANDBOX_REPOSITORY_GIT_LOCK(org.craftercms.studio.api.v1.constant.StudioConstants.SITE_SANDBOX_REPOSITORY_GIT_LOCK) CONFIG_PARAMETER_URL(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CONFIG_PARAMETER_URL) Objects(java.util.Objects) CONFIG_SECTION_REMOTE(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CONFIG_SECTION_REMOTE) List(java.util.List) StudioConfiguration(org.craftercms.studio.api.v2.utils.StudioConfiguration) PATTERN_SITE(org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) Optional(java.util.Optional) CLUSTER_NODE_REMOTE_NAME_PREFIX(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.CLUSTER_NODE_REMOTE_NAME_PREFIX) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) SANDBOX_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.SANDBOX_PATH) CloneCommand(org.eclipse.jgit.api.CloneCommand) Logger(org.craftercms.studio.api.v1.log.Logger) HashMap(java.util.HashMap) DeploymentService(org.craftercms.studio.api.v1.service.deployment.DeploymentService) CollectionUtils(org.apache.commons.collections4.CollectionUtils) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand) EventService(org.craftercms.studio.api.v1.service.event.EventService) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) EVENT_PREVIEW_SYNC(org.craftercms.studio.api.v1.ebus.EBusConstants.EVENT_PREVIEW_SYNC) LoggerFactory(org.craftercms.studio.api.v1.log.LoggerFactory) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) StudioConstants(org.craftercms.studio.api.v1.constant.StudioConstants) Files(java.nio.file.Files) StudioClusterUtils(org.craftercms.studio.impl.v2.service.cluster.StudioClusterUtils) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) CryptoException(org.craftercms.commons.crypto.CryptoException) File(java.io.File) Deployer(org.craftercms.studio.api.v2.deployment.Deployer) TransportException(org.eclipse.jgit.api.errors.TransportException) GIT_ROOT(org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryConstants.GIT_ROOT) SANDBOX(org.craftercms.studio.api.v1.constant.GitRepositories.SANDBOX) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) Paths(java.nio.file.Paths) GeneralLockService(org.craftercms.studio.api.v1.service.GeneralLockService) REPO_BASE_PATH(org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_BASE_PATH) Git(org.eclipse.jgit.api.Git) Repository(org.eclipse.jgit.lib.Repository) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) CryptoException(org.craftercms.commons.crypto.CryptoException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException)

Example 7 with SiteNotFoundException

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

the class StudioAuditLogProcessingTask method processAuditLog.

private void processAuditLog(String site) throws SiteNotFoundException {
    logger.debug("Getting last verified commit for site: " + site);
    SiteFeed siteFeed = siteService.getSite(site);
    if (checkSiteUuid(site, siteFeed.getSiteUuid())) {
        String lastSyncedCommit = siteService.getLastSyncedGitlogCommitId(site);
        if (StringUtils.isNotEmpty(lastSyncedCommit)) {
            logger.debug("Update gitlog for site " + site + " from last synced commit " + lastSyncedCommit);
            contentRepository.updateGitlog(site, lastSyncedCommit, batchSizeGitLog);
            processAuditLogFromRepo(site, batchSizeAudited);
        }
    }
}
Also used : SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed)

Example 8 with SiteNotFoundException

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

the class StudioAuditLogProcessingTask method processAuditLogFromRepo.

private void processAuditLogFromRepo(String siteId, int batchSize) throws SiteNotFoundException {
    List<GitLog> unauditedGitlogs = contentRepository.getUnauditedCommits(siteId, batchSize);
    if (unauditedGitlogs != null) {
        SiteFeed siteFeed = siteService.getSite(siteId);
        for (GitLog gl : unauditedGitlogs) {
            if (contentRepository.commitIdExists(siteId, gl.getCommitId())) {
                String prevCommitId = gl.getCommitId() + PREVIOUS_COMMIT_SUFFIX;
                List<RepoOperation> operations = contentRepository.getOperationsFromDelta(siteId, prevCommitId, gl.getCommitId());
                for (RepoOperation repoOperation : operations) {
                    Map<String, String> activityInfo = new HashMap<String, String>();
                    String contentClass;
                    AuditLog auditLog;
                    switch(repoOperation.getAction()) {
                        case CREATE:
                        case COPY:
                            contentClass = contentService.getContentTypeClass(siteId, repoOperation.getPath());
                            if (repoOperation.getPath().endsWith(DmConstants.XML_PATTERN)) {
                                activityInfo.put(DmConstants.KEY_CONTENT_TYPE, contentClass);
                            }
                            logger.debug("Insert audit log for site: " + siteId + " path: " + repoOperation.getPath());
                            auditLog = auditServiceInternal.createAuditLogEntry();
                            auditLog.setOperation(OPERATION_CREATE);
                            auditLog.setOperationTimestamp(repoOperation.getDateTime());
                            auditLog.setSiteId(siteFeed.getId());
                            auditLog.setActorId(repoOperation.getAuthor());
                            auditLog.setActorDetails(repoOperation.getAuthor());
                            auditLog.setPrimaryTargetId(siteId + ":" + repoOperation.getPath());
                            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
                            auditLog.setPrimaryTargetValue(repoOperation.getPath());
                            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(siteId, repoOperation.getPath()));
                            auditLog.setOrigin(ORIGIN_GIT);
                            auditServiceInternal.insertAuditLog(auditLog);
                            break;
                        case UPDATE:
                            contentClass = contentService.getContentTypeClass(siteId, repoOperation.getPath());
                            if (repoOperation.getPath().endsWith(DmConstants.XML_PATTERN)) {
                                activityInfo.put(DmConstants.KEY_CONTENT_TYPE, contentClass);
                            }
                            logger.debug("Insert audit log for site: " + siteId + " path: " + repoOperation.getPath());
                            auditLog = auditServiceInternal.createAuditLogEntry();
                            auditLog.setOperation(OPERATION_UPDATE);
                            auditLog.setOperationTimestamp(repoOperation.getDateTime());
                            auditLog.setSiteId(siteFeed.getId());
                            auditLog.setActorId(repoOperation.getAuthor());
                            auditLog.setActorDetails(repoOperation.getAuthor());
                            auditLog.setOrigin(ORIGIN_GIT);
                            auditLog.setPrimaryTargetId(siteId + ":" + repoOperation.getPath());
                            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
                            auditLog.setPrimaryTargetValue(repoOperation.getPath());
                            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(siteId, repoOperation.getPath()));
                            auditServiceInternal.insertAuditLog(auditLog);
                            break;
                        case DELETE:
                            contentClass = contentService.getContentTypeClass(siteId, repoOperation.getPath());
                            if (repoOperation.getPath().endsWith(DmConstants.XML_PATTERN)) {
                                activityInfo.put(DmConstants.KEY_CONTENT_TYPE, contentClass);
                            }
                            logger.debug("Insert audit log for site: " + siteId + " path: " + repoOperation.getPath());
                            auditLog = auditServiceInternal.createAuditLogEntry();
                            auditLog.setOperation(OPERATION_DELETE);
                            auditLog.setOperationTimestamp(repoOperation.getDateTime());
                            auditLog.setSiteId(siteFeed.getId());
                            auditLog.setOrigin(ORIGIN_GIT);
                            auditLog.setActorId(repoOperation.getAuthor());
                            auditLog.setActorDetails(repoOperation.getAuthor());
                            auditLog.setPrimaryTargetId(siteId + ":" + repoOperation.getPath());
                            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
                            auditLog.setPrimaryTargetValue(repoOperation.getPath());
                            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(siteId, repoOperation.getPath()));
                            auditServiceInternal.insertAuditLog(auditLog);
                            break;
                        case MOVE:
                            contentClass = contentService.getContentTypeClass(siteId, repoOperation.getMoveToPath());
                            if (repoOperation.getMoveToPath().endsWith(DmConstants.XML_PATTERN)) {
                                activityInfo.put(DmConstants.KEY_CONTENT_TYPE, contentClass);
                            }
                            logger.debug("Insert audit log for site: " + siteId + " path: " + repoOperation.getMoveToPath());
                            auditLog = auditServiceInternal.createAuditLogEntry();
                            auditLog.setOperation(OPERATION_MOVE);
                            auditLog.setOperationTimestamp(repoOperation.getDateTime());
                            auditLog.setSiteId(siteFeed.getId());
                            auditLog.setActorId(repoOperation.getAuthor());
                            auditLog.setActorDetails(repoOperation.getAuthor());
                            auditLog.setOrigin(ORIGIN_GIT);
                            auditLog.setPrimaryTargetId(siteId + ":" + repoOperation.getMoveToPath());
                            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
                            auditLog.setPrimaryTargetValue(repoOperation.getMoveToPath());
                            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(siteId, repoOperation.getMoveToPath()));
                            auditServiceInternal.insertAuditLog(auditLog);
                            break;
                        default:
                            logger.error("Error: Unknown repo operation for site " + siteId + " operation: " + repoOperation.getAction());
                            break;
                    }
                }
            }
            contentRepository.markGitLogAudited(siteId, gl.getCommitId());
        }
    }
}
Also used : RepoOperation(org.craftercms.studio.api.v2.dal.RepoOperation) HashMap(java.util.HashMap) GitLog(org.craftercms.studio.api.v2.dal.GitLog) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Example 9 with SiteNotFoundException

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

the class ContentServiceImpl method createFolder.

@Override
@ValidateParams
public boolean createFolder(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, @ValidateStringParam(name = "name") String name) throws SiteNotFoundException {
    boolean toRet = false;
    String commitId = _contentRepository.createFolder(site, path, name);
    if (commitId != null) {
        SiteFeed siteFeed = siteService.getSite(site);
        AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
        auditLog.setOperation(OPERATION_CREATE);
        auditLog.setSiteId(siteFeed.getId());
        auditLog.setActorId(securityService.getCurrentUser());
        auditLog.setPrimaryTargetId(site + ":" + path + FILE_SEPARATOR + name);
        auditLog.setPrimaryTargetType(TARGET_TYPE_FOLDER);
        auditLog.setPrimaryTargetValue(path + FILE_SEPARATOR + name);
        auditServiceInternal.insertAuditLog(auditLog);
        contentRepository.insertGitLog(site, commitId, 1, 1);
        siteService.updateLastCommitId(site, commitId);
        toRet = true;
    }
    return toRet;
}
Also used : SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 10 with SiteNotFoundException

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

the class ContentServiceImpl method updateDatabaseOnMove.

protected void updateDatabaseOnMove(String site, String fromPath, String movePath) throws SiteNotFoundException {
    logger.debug("updateDatabaseOnMove FROM {0} TO {1}  ", fromPath, movePath);
    String user = securityService.getCurrentUser();
    Map<String, String> params = new HashMap<>();
    params.put(DmConstants.KEY_SOURCE_PATH, fromPath);
    params.put(DmConstants.KEY_TARGET_PATH, movePath);
    // These do not exist in 3.0, note some extensions may be using it
    // params.put(DmConstants.KEY_SOURCE_FULL_PATH, expandRelativeSitePath(site, fromPath));
    // params.put(DmConstants.KEY_TARGET_FULL_PATH, expandRelativeSitePath(site, movePath));
    ContentItemTO renamedItem = getContentItem(site, movePath, 0);
    String contentType = renamedItem.getContentType();
    if (!renamedItem.isFolder()) {
        dmContentLifeCycleService.process(site, user, movePath, contentType, DmContentLifeCycleService.ContentLifeCycleOperation.RENAME, params);
        // change the path of this object in the object state database
        objectStateService.updateObjectPath(site, fromPath, movePath);
        objectStateService.transition(site, renamedItem, SAVE);
        renamedItem = getContentItem(site, movePath, 0);
    }
    // update metadata
    if (!objectMetadataManager.isRenamed(site, fromPath)) {
        // if an item was previously moved, we do not track intermediate moves because it will
        // ultimately orphan deployed content.  Old Path is always the OLDEST DEPLOYED PATH
        ItemMetadata metadata = objectMetadataManager.getProperties(site, fromPath);
        if (metadata == null && !renamedItem.isFolder()) {
            if (!objectMetadataManager.metadataExist(site, fromPath)) {
                objectMetadataManager.insertNewObjectMetadata(site, fromPath);
            }
            metadata = objectMetadataManager.getProperties(site, fromPath);
        }
        if (!renamedItem.isNew() && !renamedItem.isFolder()) {
            // if the item is not new, we need to track the old URL for deployment
            logger.debug("item is not new, and has not previously been moved. Track the old URL {0}", fromPath);
            Map<String, Object> objMetadataProps = new HashMap<String, Object>();
            objMetadataProps.put(ItemMetadata.PROP_RENAMED, 1);
            objMetadataProps.put(ItemMetadata.PROP_OLD_URL, fromPath);
            objectMetadataManager.setObjectMetadata(site, fromPath, objMetadataProps);
        }
    }
    if (!renamedItem.isFolder()) {
        if (objectMetadataManager.metadataExist(site, movePath)) {
            if (!StringUtils.equalsIgnoreCase(fromPath, movePath)) {
                objectMetadataManager.deleteObjectMetadata(site, movePath);
            }
        }
        objectMetadataManager.updateObjectPath(site, fromPath, movePath);
    }
    // write activity stream
    SiteFeed siteFeed = siteService.getSite(site);
    AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
    auditLog.setOperation(OPERATION_MOVE);
    auditLog.setSiteId(siteFeed.getId());
    auditLog.setActorId(user);
    auditLog.setPrimaryTargetId(site + ":" + movePath);
    if (renamedItem.isFolder()) {
        auditLog.setPrimaryTargetType(TARGET_TYPE_FOLDER);
    } else {
        auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
    }
    auditLog.setPrimaryTargetValue(movePath);
    auditLog.setPrimaryTargetSubtype(getContentTypeClass(site, movePath));
    auditServiceInternal.insertAuditLog(auditLog);
    updateDependenciesOnMove(site, fromPath, movePath);
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) HashMap(java.util.HashMap) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) ItemMetadata(org.craftercms.studio.api.v1.dal.ItemMetadata)

Aggregations

SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)41 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)34 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)23 ResponseBody (org.craftercms.studio.model.rest.ResponseBody)20 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)12 PostMapping (org.springframework.web.bind.annotation.PostMapping)11 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)10 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)9 GetMapping (org.springframework.web.bind.annotation.GetMapping)9 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)8 Result (org.craftercms.studio.model.rest.Result)8 CryptoException (org.craftercms.commons.crypto.CryptoException)6 ResultOne (org.craftercms.studio.model.rest.ResultOne)6 IOException (java.io.IOException)5 Set (java.util.Set)5 ItemMetadata (org.craftercms.studio.api.v1.dal.ItemMetadata)5 AuditLogParameter (org.craftercms.studio.api.v2.dal.AuditLogParameter)5 HashSet (java.util.HashSet)4