use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.
the class GitContentRepository method getContent.
@Override
public InputStream getContent(String site, String path) throws ContentNotFoundException, CryptoException {
InputStream toReturn = null;
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repo == null) {
throw new ContentNotFoundException("Repository not found for site " + site);
}
try {
RevTree tree = helper.getTreeForLastCommit(repo);
try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
// pick the first item in the list
if (tw != null && tw.getObjectId(0) != null) {
ObjectId id = tw.getObjectId(0);
ObjectLoader objectLoader = repo.open(id);
toReturn = objectLoader.openStream();
tw.close();
}
} catch (IOException e) {
logger.error("Error while getting content for file at site: " + site + " path: " + path, e);
}
} catch (IOException e) {
logger.error("Failed to create RevTree for site: " + site + " path: " + path, e);
}
return toReturn;
}
use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.
the class GitContentRepository method getContentVersion.
@Override
public InputStream getContentVersion(String site, String path, String version) throws ContentNotFoundException {
InputStream toReturn = null;
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
RevTree tree = helper.getTreeForCommit(repo, version);
if (tree != null) {
try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
if (tw != null) {
ObjectId id = tw.getObjectId(0);
ObjectLoader objectLoader = repo.open(id);
toReturn = objectLoader.openStream();
tw.close();
}
} catch (IOException e) {
logger.error("Error while getting content for file at site: " + site + " path: " + path + " version: " + version, e);
}
}
} catch (IOException | CryptoException e) {
logger.error("Failed to create RevTree for site: " + site + " path: " + path + " version: " + version, e);
}
return toReturn;
}
use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.
the class GitContentRepository method revertContent.
@Override
public String revertContent(String site, String path, String version, boolean major, String comment) {
// TODO: SJ: refactor to remove the notion of a major/minor for 3.1+
String commitId = null;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
InputStream versionContent = getContentVersion(site, path, version);
commitId = writeContent(site, path, versionContent);
createVersion(site, path, major);
} catch (ContentNotFoundException err) {
logger.error("error reverting content for site: " + site + " path: " + path, err);
} finally {
generalLockService.unlock(gitLockKey);
}
return commitId;
}
use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.
the class FormDmContentProcessor method createNewFile.
/**
* create new file to the given path. If the path is a file name, it will
* create a new folder with the same name as the file name (without the
* prefix) and move the existing file to the folder created. Then it creates
* new file to the folder
*
* @param site
* Site name
* @param fileName
* new file name
* @param contentType
* content type
* @param input
* file content
* @param user
* current user
* @throws ContentNotFoundException
*/
protected ContentItemTO createNewFile(String site, ContentItemTO parentItem, String fileName, String contentType, InputStream input, String user, boolean unlock, ResultTO result) throws ContentNotFoundException, SiteNotFoundException {
ContentItemTO fileItem = null;
if (parentItem != null) {
// convert file to folder if target path is a file
String folderPath = fileToFolder(site, parentItem.getUri());
try {
contentService.writeContent(site, parentItem.getUri() + FILE_SEPARATOR + fileName, input);
if (!objectMetadataManager.metadataExist(site, parentItem.getUri() + FILE_SEPARATOR + fileName)) {
objectMetadataManager.insertNewObjectMetadata(site, parentItem.getUri() + FILE_SEPARATOR + fileName);
}
Map<String, Object> properties = new HashMap<>();
properties.put(ItemMetadata.PROP_NAME, fileName);
properties.put(ItemMetadata.PROP_MODIFIED, ZonedDateTime.now(ZoneOffset.UTC));
properties.put(ItemMetadata.PROP_CREATOR, user);
properties.put(ItemMetadata.PROP_MODIFIER, user);
properties.put(ItemMetadata.PROP_OWNER, user);
if (unlock) {
properties.put(ItemMetadata.PROP_LOCK_OWNER, StringUtils.EMPTY);
} else {
properties.put(ItemMetadata.PROP_LOCK_OWNER, user);
}
objectMetadataManager.setObjectMetadata(site, parentItem.getUri() + FILE_SEPARATOR + fileName, properties);
result.setCommitId(objectMetadataManager.getProperties(site, parentItem.getUri() + FILE_SEPARATOR + fileName).getCommitId());
} catch (Exception e) {
logger.error("Error writing new file: " + fileName, e);
} finally {
IOUtils.closeQuietly(input);
}
// unlock the content upon save
if (unlock) {
contentRepository.unLockItem(site, parentItem.getUri() + FILE_SEPARATOR + fileName);
} else {
}
fileItem = contentService.getContentItem(site, parentItem.getUri() + FILE_SEPARATOR + fileName, 0);
return fileItem;
} else {
throw new ContentNotFoundException(parentItem.getUri() + " does not exist in site: " + site);
}
}
use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.
the class DependencyServiceImpl method upsertDependencies.
@Override
public Set<String> upsertDependencies(String site, List<String> paths) throws SiteNotFoundException, ContentNotFoundException, ServiceLayerException {
Set<String> toRet = new HashSet<String>();
List<DependencyEntity> dependencyEntities = new ArrayList<>();
StringBuilder sbPaths = new StringBuilder();
logger.debug("Resolving dependencies for list of paths.");
for (String path : paths) {
sbPaths.append("\n").append(path);
logger.debug("Resolving dependencies for content site: " + site + " path: " + path);
Map<String, Set<String>> dependencies = dependencyResolver.resolve(site, path);
if (dependencies != null) {
logger.debug("Found " + dependencies.size() + " dependencies. " + "Create entities to insert into database.");
for (String type : dependencies.keySet()) {
dependencyEntities.addAll(createDependencyEntities(site, path, dependencies.get(type), type, toRet));
}
}
}
logger.debug("Preparing transaction for database updates.");
DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
defaultTransactionDefinition.setName("upsertDependencies");
String lock = site + ":upsertDependencies";
generalLockService.lock(lock);
logger.debug("Starting transaction.");
TransactionStatus txStatus = transactionManager.getTransaction(defaultTransactionDefinition);
try {
logger.debug("Delete all source dependencies for list of paths site: " + site);
for (String path : paths) {
deleteAllSourceDependencies(site, path);
}
logger.debug("Insert all extracted dependencies entries lof list of paths for site: " + site);
insertDependenciesIntoDatabase(dependencyEntities);
logger.debug("Committing transaction.");
transactionManager.commit(txStatus);
} catch (Exception e) {
logger.debug("Rolling back transaction.", e);
transactionManager.rollback(txStatus);
throw new ServiceLayerException("Failed to upsert dependencies for site: " + site + " paths: " + sbPaths.toString(), e);
} finally {
generalLockService.unlock(lock);
}
return toRet;
}
Aggregations