use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class ZipArchiver method addZipEntry.
private void addZipEntry(VirtualFile virtualFile, ZipOutputStream zipOutputStream) throws ServerException {
try {
ZipEntry zipEntry = new ZipEntry(getZipEntryName(virtualFile));
zipOutputStream.putNextEntry(zipEntry);
if (virtualFile.isFolder()) {
zipEntry.setTime(0);
} else {
try (InputStream content = virtualFile.getContent()) {
ByteStreams.copy(content, zipOutputStream);
}
zipEntry.setTime(virtualFile.getLastModificationDate());
}
zipOutputStream.closeEntry();
} catch (ForbiddenException e) {
throw new ServerException(e.getServiceError());
} catch (IOException e) {
throw new ServerException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class LocalVirtualFileSystem method copy.
LocalVirtualFile copy(LocalVirtualFile source, LocalVirtualFile parent, String name, boolean overwrite) throws ForbiddenException, ConflictException, ServerException {
if (source.getPath().equals(parent.getPath())) {
throw new ForbiddenException("Item cannot be copied to itself");
}
if (parent.isFolder()) {
final String newName = isNullOrEmpty(name) ? source.getName() : name;
LocalVirtualFile destination = (LocalVirtualFile) parent.getChild(Path.of(newName));
if (destination != null) {
if (overwrite) {
delete(destination, null);
} else {
throw new ConflictException(String.format("Item '%s' already exists", destination.getPath()));
}
} else {
final Path newPath = parent.getPath().newPath(newName);
final File newIoFile = new File(ioRoot, toIoPath(newPath));
destination = new LocalVirtualFile(newIoFile, newPath, this);
}
doCopy(source, destination);
addInSearcher(destination);
return destination;
} else {
throw new ForbiddenException("Unable copy item. Item specified as parent is not a folder");
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class MemoryVirtualFile method createFolder.
@Override
public VirtualFile createFolder(String name) throws ForbiddenException, ConflictException, ServerException {
checkExistence();
checkName(name);
if (name.charAt(0) == '/') {
name = name.substring(1);
}
checkName(name);
if (isFolder()) {
MemoryVirtualFile newFolder = null;
MemoryVirtualFile current = this;
if (name.indexOf('/') > 0) {
final Path internPath = Path.of(name);
for (String element : internPath.elements()) {
MemoryVirtualFile folder = newFolder(current, element);
if (current.addChild(folder)) {
newFolder = folder;
current = folder;
} else {
current = current.children.get(element);
}
}
if (newFolder == null) {
throw new ConflictException(String.format("Item with the name '%s' already exists", name));
}
} else {
newFolder = newFolder(this, name);
if (!addChild(newFolder)) {
throw new ConflictException(String.format("Item with the name '%s' already exists", name));
}
}
return newFolder;
} else {
throw new ForbiddenException("Unable create new folder. Item specified as parent is not a folder");
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class MemoryVirtualFile method createFile.
@Override
public VirtualFile createFile(String name, InputStream content) throws ForbiddenException, ConflictException, ServerException {
checkExistence();
checkName(name);
if (Path.of(name).length() > 1) {
throw new ServerException(String.format("Invalid name '%s'", name));
}
if (isFolder()) {
final MemoryVirtualFile newFile;
try {
newFile = newFile(this, name, content);
} catch (IOException e) {
throw new ServerException(String.format("Unable set content of '%s'. Error: %s", getPath(), e.getMessage()));
}
if (!addChild(newFile)) {
throw new ConflictException(String.format("Item with the name '%s' already exists", name));
}
addInSearcher(newFile);
return newFile;
} else {
throw new ForbiddenException("Unable create new file. Item specified as parent is not a folder");
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class MemoryVirtualFile method rename.
@Override
public VirtualFile rename(String newName, String lockToken) throws ForbiddenException, ConflictException, ServerException {
checkExistence();
checkName(newName);
boolean isFile = isFile();
if (isRoot()) {
throw new ForbiddenException("We were unable to rename a root folder.");
}
final Path myPath = getPath();
final boolean isFolder = isFolder();
if (isFolder) {
final List<VirtualFile> lockedFiles = new LockedFileFinder(this).findLockedFiles();
if (!lockedFiles.isEmpty()) {
throw new ForbiddenException(String.format("Unable rename item '%s'. Child items '%s' are locked", getName(), lockedFiles));
}
} else {
if (fileIsLockedAndLockTokenIsInvalid(lockToken)) {
throw new ForbiddenException(String.format("We were unable to rename an item '%s'." + " The item is currently locked by the system", getPath()));
}
}
if (parent.children.get(newName) != null) {
throw new ConflictException(String.format("Item '%s' already exists", newName));
}
parent.children.remove(name);
parent.children.put(newName, this);
name = newName;
lock = null;
lastModificationDate = System.currentTimeMillis();
deleteFromSearcher(myPath, isFile);
addInSearcher(this);
return this;
}
Aggregations