use of org.eclipse.che.api.core.ConflictException in project che by eclipse.
the class LocalVirtualFileSystem method doLock.
private String doLock(LocalVirtualFile virtualFile, long timeout) throws ConflictException, ServerException {
try {
if (NO_LOCK == lockTokensCache.get(virtualFile.getPath())) {
final FileLock lock = createLock(timeout);
final File fileLockIoFile = getFileLockIoFile(virtualFile.getPath());
fileLockIoFile.getParentFile().mkdirs();
try (DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileLockIoFile)))) {
locksSerializer.write(dos, lock);
}
lockTokensCache.put(virtualFile.getPath(), lock);
return lock.getLockToken();
}
throw new ConflictException(String.format("Unable lock file '%s'. File already locked", virtualFile.getPath()));
} catch (IOException | ExecutionException e) {
String errorMessage = String.format("Unable lock file '%s'", virtualFile.getPath());
if (e instanceof ExecutionException) {
LOG.error(errorMessage + "\n" + e.getCause().getMessage(), e.getCause());
} else {
LOG.error(errorMessage + "\n" + e.getMessage(), e);
}
throw new ServerException(errorMessage);
}
}
use of org.eclipse.che.api.core.ConflictException in project che by eclipse.
the class LocalVirtualFileSystem method move.
LocalVirtualFile move(LocalVirtualFile virtualFile, LocalVirtualFile parent, String name, boolean overwrite, String lockToken) throws ForbiddenException, ConflictException, ServerException {
if (virtualFile.isRoot()) {
throw new ForbiddenException("Unable move root folder");
}
if (virtualFile.getPath().equals(parent.getPath())) {
throw new ForbiddenException("Item cannot be moved to itself");
}
if (!parent.isFolder()) {
throw new ForbiddenException("Unable move. Item specified as parent is not a folder");
}
final Path sourcePath = virtualFile.getPath();
final Path parentPath = parent.getPath();
if (virtualFile.isFolder() && parent.getPath().isChild(virtualFile.getPath())) {
throw new ForbiddenException(String.format("Unable move item '%s' to '%s'. Item may not have itself as parent", sourcePath, parentPath));
}
if (virtualFile.isFile()) {
if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
throw new ForbiddenException(String.format("Unable move file '%s'. File is locked", sourcePath));
}
} else {
final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
if (!lockedFiles.isEmpty()) {
throw new ForbiddenException(String.format("Unable move folder '%s'. Child items '%s' are locked", virtualFile, lockedFiles));
}
}
String newName = isNullOrEmpty(name) ? virtualFile.getName() : name;
final Path newPath = parent.getPath().newPath(newName);
LocalVirtualFile newVirtualFile = new LocalVirtualFile(new File(ioRoot, toIoPath(newPath)), newPath, this);
if (newVirtualFile.exists()) {
if (overwrite) {
delete(newVirtualFile, null);
} else {
throw new ConflictException(String.format("Item '%s' already exists", newPath));
}
}
doCopy(virtualFile, newVirtualFile);
addInSearcher(newVirtualFile);
final Path path = virtualFile.getPath();
final boolean isFile = virtualFile.isFile();
doDelete(virtualFile, lockToken);
deleteInSearcher(path, isFile);
return newVirtualFile;
}
use of org.eclipse.che.api.core.ConflictException in project che by eclipse.
the class ProjectManager method validateProjectConfigurations.
private void validateProjectConfigurations(List<? extends NewProjectConfig> projectConfigList, boolean rewrite) throws NotFoundException, ServerException, ConflictException, ForbiddenException, BadRequestException {
for (NewProjectConfig projectConfig : projectConfigList) {
final String pathToProject = projectConfig.getPath();
if (isNullOrEmpty(pathToProject)) {
throw new BadRequestException("Path for new project should be defined");
}
final String path = ProjectRegistry.absolutizePath(pathToProject);
final RegisteredProject registeredProject = projectRegistry.getProject(path);
if (registeredProject != null && rewrite) {
delete(path);
} else if (registeredProject != null) {
throw new ConflictException(format("Project config already exists for %s", path));
}
final String projectTypeId = projectConfig.getType();
if (isNullOrEmpty(projectTypeId)) {
projectConfig.setType(BaseProjectType.ID);
}
}
}
use of org.eclipse.che.api.core.ConflictException in project che by eclipse.
the class ProjectManager method doImportProject.
/** Note: Use {@link FileWatcherManager#suspend()} and {@link FileWatcherManager#resume()} while importing source code */
private RegisteredProject doImportProject(String path, SourceStorage sourceStorage, boolean rewrite, LineConsumerFactory lineConsumerFactory) throws ServerException, IOException, ForbiddenException, UnauthorizedException, ConflictException, NotFoundException {
final ProjectImporter importer = importers.getImporter(sourceStorage.getType());
if (importer == null) {
throw new NotFoundException(format("Unable import sources project from '%s'. Sources type '%s' is not supported.", sourceStorage.getLocation(), sourceStorage.getType()));
}
String normalizePath = (path.startsWith("/")) ? path : "/".concat(path);
FolderEntry folder = asFolder(normalizePath);
if (folder != null && !rewrite) {
throw new ConflictException(format("Project %s already exists ", path));
}
if (folder == null) {
folder = getProjectsRoot().createFolder(normalizePath);
}
try {
importer.importSources(folder, sourceStorage, lineConsumerFactory);
} catch (final Exception e) {
folder.remove();
throw e;
}
final String name = folder.getPath().getName();
for (ProjectConfig project : workspaceProjectsHolder.getProjects()) {
if (normalizePath.equals(project.getPath())) {
// TODO Needed for factory project importing with keepDir. It needs to find more appropriate solution
List<String> innerProjects = projectRegistry.getProjects(normalizePath);
for (String innerProject : innerProjects) {
RegisteredProject registeredProject = projectRegistry.getProject(innerProject);
projectRegistry.putProject(registeredProject, asFolder(registeredProject.getPath()), true, false);
}
RegisteredProject rp = projectRegistry.putProject(project, folder, true, false);
workspaceProjectsHolder.sync(projectRegistry);
return rp;
}
}
RegisteredProject rp = projectRegistry.putProject(new NewProjectConfigImpl(normalizePath, name, BaseProjectType.ID, sourceStorage), folder, true, false);
workspaceProjectsHolder.sync(projectRegistry);
return rp;
}
use of org.eclipse.che.api.core.ConflictException in project che by eclipse.
the class TarArchiver method extract.
@Override
public void extract(InputStream tarInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(tarInput)) {
InputStream notClosableInputStream = new NotClosableInputStream(tarInputStream);
TarArchiveEntry tarEntry;
while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {
VirtualFile extractFolder = folder;
Path relativePath = Path.of(tarEntry.getName());
if (stripNumber > 0) {
if (relativePath.length() <= stripNumber) {
continue;
}
relativePath = relativePath.subPath(stripNumber);
}
if (tarEntry.isDirectory()) {
if (!extractFolder.hasChild(relativePath)) {
extractFolder.createFolder(relativePath.toString());
}
continue;
}
if (relativePath.length() > 1) {
Path neededParentPath = relativePath.getParent();
VirtualFile neededParent = extractFolder.getChild(neededParentPath);
if (neededParent == null) {
neededParent = extractFolder.createFolder(neededParentPath.toString());
}
extractFolder = neededParent;
}
String fileName = relativePath.getName();
VirtualFile file = extractFolder.getChild(Path.of(fileName));
if (file == null) {
extractFolder.createFile(fileName, notClosableInputStream);
} else {
if (overwrite) {
file.updateContent(notClosableInputStream);
} else {
throw new ConflictException(String.format("File '%s' already exists", file.getPath()));
}
}
}
}
}
Aggregations