use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class IndexedFileUpdateConsumer method accept.
@Override
public void accept(Path path) {
try {
VirtualFileSystem virtualFileSystem = vfsProvider.getVirtualFileSystem();
SearcherProvider searcherProvider = virtualFileSystem.getSearcherProvider();
Searcher searcher = searcherProvider.getSearcher(virtualFileSystem);
Path innerPath = root.toPath().relativize(path);
org.eclipse.che.api.vfs.Path vfsPath = org.eclipse.che.api.vfs.Path.of(innerPath.toString());
VirtualFile child = virtualFileSystem.getRoot().getChild(vfsPath);
if (child != null) {
searcher.update(child);
}
} catch (ServerException e) {
LOG.error("Issue happened during updating modified file in index", e);
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class ProjectServiceTest method testMoveFileWithRenameAndOverwrite.
@Test
public void testMoveFileWithRenameAndOverwrite() throws Exception {
RegisteredProject myProject = pm.getProject("my_project");
myProject.getBaseFolder().createFolder("a/b/c");
// File names
String originFileName = "test.txt";
String destinationFileName = "overwriteMe.txt";
// File contents
String originContent = "to be or not no be";
String overwrittenContent = "that is the question";
((FolderEntry) myProject.getBaseFolder().getChild("a/b")).createFile(originFileName, originContent.getBytes(Charset.defaultCharset()));
((FolderEntry) myProject.getBaseFolder().getChild("a/b/c")).createFile(destinationFileName, overwrittenContent.getBytes(Charset.defaultCharset()));
Map<String, List<String>> headers = new HashMap<>();
headers.put(CONTENT_TYPE, singletonList(APPLICATION_JSON));
MoveOptions descriptor = DtoFactory.getInstance().createDto(MoveOptions.class);
descriptor.setName(destinationFileName);
descriptor.setOverWrite(true);
ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/move/my_project/a/b/" + originFileName + "?to=/my_project/a/b/c", "http://localhost:8080/api", headers, DtoFactory.getInstance().toJson(descriptor).getBytes(Charset.defaultCharset()), null);
assertEquals(response.getStatus(), 201, "Error: " + response.getEntity());
assertEquals(response.getHttpHeaders().getFirst("Location"), URI.create("http://localhost:8080/api/project/file/my_project/a/b/c/" + destinationFileName));
// new
assertNotNull(myProject.getBaseFolder().getChild("a/b/c/" + destinationFileName));
Scanner inputStreamScanner = null;
String theFirstLineFromDestinationFile;
try {
inputStreamScanner = new Scanner(myProject.getBaseFolder().getChild("a/b/c/" + destinationFileName).getVirtualFile().getContent());
theFirstLineFromDestinationFile = inputStreamScanner.nextLine();
// destination should contain original file's content
assertEquals(theFirstLineFromDestinationFile, originContent);
} catch (ForbiddenException | ServerException e) {
Assert.fail(e.getMessage());
} finally {
if (inputStreamScanner != null) {
inputStreamScanner.close();
}
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class LocalVirtualFileSystem method doGetChildren.
private List<VirtualFile> doGetChildren(LocalVirtualFile parent, FilenameFilter ioFileFilter, VirtualFileFilter vfsFilter) throws ServerException {
if (ioFileFilter == null) {
ioFileFilter = IoUtil.ANY_FILTER;
}
final String[] names = parent.toIoFile().list(ioFileFilter);
if (names == null) {
throw new ServerException(String.format("Unable get children of '%s'", parent.getPath()));
}
if (vfsFilter == null) {
vfsFilter = VirtualFileFilter.ACCEPT_ALL;
}
final List<VirtualFile> children = newArrayListWithCapacity(names.length);
for (String name : names) {
final Path childPath = parent.getPath().newPath(name);
final LocalVirtualFile child = new LocalVirtualFile(new File(ioRoot, toIoPath(childPath)), childPath, this);
if (vfsFilter.accept(child)) {
children.add(child);
}
}
return children;
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class LocalVirtualFileSystem method doDelete.
private void doDelete(LocalVirtualFile virtualFile, String lockToken) throws ForbiddenException, ServerException {
if (virtualFile.isFolder()) {
final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
if (!lockedFiles.isEmpty()) {
throw new ForbiddenException(String.format("Unable delete folder '%s'. Child items '%s' are locked", virtualFile.getPath(), lockedFiles));
}
} else if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
throw new ForbiddenException(String.format("Unable delete file '%s'. File is locked", virtualFile.getPath()));
}
cleanUpCaches();
final File fileLockIoFile = getFileLockIoFile(virtualFile.getPath());
if (fileLockIoFile.delete()) {
if (fileLockIoFile.exists()) {
LOG.error("Unable delete lock file {}", fileLockIoFile);
throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
}
}
final File metadataIoFile = getMetadataIoFile(virtualFile.getPath());
if (metadataIoFile.delete()) {
if (metadataIoFile.exists()) {
LOG.error("Unable delete metadata file {}", metadataIoFile);
throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
}
}
if (!deleteRecursive(virtualFile.toIoFile())) {
LOG.error("Unable delete file {}", virtualFile.toIoFile());
throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class LocalVirtualFileSystem method getContent.
InputStream getContent(LocalVirtualFile virtualFile) throws ForbiddenException, ServerException {
if (virtualFile.isFile()) {
final PathLockFactory.PathLock lock = pathLockFactory.getLock(virtualFile.getPath(), false).acquire(WAIT_FOR_FILE_LOCK_TIMEOUT);
File spoolFile = null;
try {
final File ioFile = virtualFile.toIoFile();
final long fileLength = ioFile.length();
if (fileLength <= MAX_BUFFER_SIZE) {
return new ByteArrayInputStream(Files.toByteArray(ioFile));
}
// Copy this file to be able release the file lock before leave this method.
spoolFile = File.createTempFile("spool_file", null);
Files.copy(ioFile, spoolFile);
return new DeleteOnCloseFileInputStream(spoolFile);
} catch (IOException e) {
if (spoolFile != null) {
FileCleaner.addFile(spoolFile);
}
String errorMessage = String.format("Unable get content of '%s'", virtualFile.getPath());
LOG.error(errorMessage + "\n" + e.getMessage(), e);
throw new ServerException(errorMessage);
} finally {
lock.release();
}
} else {
throw new ForbiddenException(String.format("Unable get content. Item '%s' is not a file", virtualFile.getPath()));
}
}
Aggregations