Search in sources :

Example 1 with RepositoryLockedException

use of org.craftercms.studio.api.v2.exception.RepositoryLockedException 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)

Example 2 with RepositoryLockedException

use of org.craftercms.studio.api.v2.exception.RepositoryLockedException in project studio by craftercms.

the class BlobAwareContentRepository method writeContent.

@Override
public String writeContent(String site, String path, InputStream content) throws ServiceLayerException {
    logger.debug("Writing {0} in site {1}", path, site);
    try {
        StudioBlobStore store = getBlobStore(site, path);
        if (store != null) {
            store.writeContent(site, normalize(path), content);
            Blob reference = store.getReference(normalize(path));
            return localRepositoryV1.writeContent(site, getPointerPath(site, path), new ByteArrayInputStream(objectMapper.writeValueAsBytes(reference)));
        }
        return localRepositoryV1.writeContent(site, path, content);
    } catch (RepositoryLockedException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Error writing content {0} in site {1}", e, path, site);
        throw new ServiceLayerException(e);
    }
}
Also used : Blob(org.craftercms.commons.file.blob.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) StudioBlobStore(org.craftercms.studio.api.v2.repository.blob.StudioBlobStore) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ConfigurationException(org.craftercms.commons.config.ConfigurationException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) IOException(java.io.IOException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException)

Example 3 with RepositoryLockedException

use of org.craftercms.studio.api.v2.exception.RepositoryLockedException in project studio by craftercms.

the class GitRepositoryHelper method writeFile.

public boolean writeFile(Repository repo, String site, String path, InputStream content) {
    boolean result = true;
    try {
        // Create basic file
        File file = new File(repo.getDirectory().getParent(), path);
        // Create parent folders
        File folder = file.getParentFile();
        if (folder != null) {
            if (!folder.exists()) {
                folder.mkdirs();
            }
        }
        // Create the file if it doesn't exist already
        if (!file.exists()) {
            try {
                if (!file.createNewFile()) {
                    logger.error("error creating file: site: " + site + " path: " + path);
                    result = false;
                }
            } catch (IOException e) {
                logger.error("error creating file: site: " + site + " path: " + path, e);
                result = false;
            }
        }
        if (result) {
            // Write the bits
            try (FileChannel outChannel = new FileOutputStream(file.getPath()).getChannel()) {
                logger.debug("created the file output channel");
                ReadableByteChannel inChannel = Channels.newChannel(content);
                logger.debug("created the file input channel");
                // 1MB at a time
                long amount = 1024 * 1024;
                long count;
                long offset = 0;
                while ((count = outChannel.transferFrom(inChannel, offset, amount)) > 0) {
                    logger.debug("writing the bits: offset = " + offset + " count: " + count);
                    offset += count;
                }
            }
            // Add the file to git
            try (Git git = new Git(repo)) {
                AddCommand addCommand = git.add().addFilepattern(getGitPath(path));
                retryingRepositoryOperationFacade.call(addCommand);
                git.close();
                result = true;
            } catch (JGitInternalException internalException) {
                if (internalException.getCause() instanceof LockFailedException) {
                    throw new RepositoryLockedException("Writing file " + path + " for site " + site + " failed because " + "repository was locked.");
                }
            } catch (GitAPIException e) {
                logger.error("error adding file to git: site: " + site + " path: " + path, e);
                result = false;
            }
        }
    } catch (IOException e) {
        logger.error("error writing file: site: " + site + " path: " + path, e);
        result = false;
    }
    return result;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) LockFailedException(org.eclipse.jgit.errors.LockFailedException) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) FileOutputStream(java.io.FileOutputStream) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) File(java.io.File) AddCommand(org.eclipse.jgit.api.AddCommand)

Aggregations

RepositoryLockedException (org.craftercms.studio.api.v2.exception.RepositoryLockedException)3 IOException (java.io.IOException)2 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)2 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)2 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 FileChannel (java.nio.channels.FileChannel)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 ConfigurationException (org.craftercms.commons.config.ConfigurationException)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1 Blob (org.craftercms.commons.file.blob.Blob)1 ContentProcessException (org.craftercms.studio.api.v1.exception.ContentProcessException)1 DeploymentException (org.craftercms.studio.api.v1.service.deployment.DeploymentException)1 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)1 StudioBlobStore (org.craftercms.studio.api.v2.repository.blob.StudioBlobStore)1 AddCommand (org.eclipse.jgit.api.AddCommand)1 Git (org.eclipse.jgit.api.Git)1