use of org.eclipse.che.api.core.ForbiddenException 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.ForbiddenException 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()));
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class LocalVirtualFileSystem method rename.
LocalVirtualFile rename(LocalVirtualFile virtualFile, String newName, String lockToken) throws ForbiddenException, ConflictException, ServerException {
checkName(newName);
if (virtualFile.isRoot()) {
throw new ForbiddenException("Unable rename root folder");
}
if (virtualFile.isFile()) {
if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
throw new ForbiddenException(String.format("Unable rename file '%s'. File is locked", virtualFile.getPath()));
}
} else {
final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
if (!lockedFiles.isEmpty()) {
throw new ForbiddenException(String.format("Unable rename folder '%s'. Child items '%s' are locked", virtualFile.getPath(), lockedFiles));
}
}
if (newName.equals(virtualFile.getName())) {
return virtualFile;
} else {
final Path newPath = virtualFile.getPath().getParent().newPath(newName);
final LocalVirtualFile newVirtualFile = new LocalVirtualFile(new File(ioRoot, toIoPath(newPath)), newPath, this);
if (newVirtualFile.exists()) {
throw new ConflictException(String.format("Item '%s' already exists", newVirtualFile.getName()));
}
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.ForbiddenException in project che by eclipse.
the class RemoteOAuthTokenProvider method getToken.
/** {@inheritDoc} */
@Override
public OAuthToken getToken(String oauthProviderName, String userId) throws IOException {
if (userId.isEmpty()) {
return null;
}
try {
UriBuilder ub = UriBuilder.fromUri(apiEndpoint).path(OAuthAuthenticationService.class).path(OAuthAuthenticationService.class, "token").queryParam("oauth_provider", oauthProviderName);
Link getTokenLink = DtoFactory.newDto(Link.class).withHref(ub.build().toString()).withMethod("GET");
return httpJsonRequestFactory.fromLink(getTokenLink).request().asDto(OAuthToken.class);
} catch (NotFoundException ne) {
LOG.warn("Token not found for user {}", userId);
return null;
} catch (ServerException | UnauthorizedException | ForbiddenException | ConflictException | BadRequestException e) {
LOG.warn("Exception on token retrieval, message : {}", e.getLocalizedMessage());
return null;
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class ProjectServiceTest method testCopyFileWithRenameAndOverwrite.
@Test
public void testCopyFileWithRenameAndOverwrite() 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));
CopyOptions descriptor = DtoFactory.getInstance().createDto(CopyOptions.class);
descriptor.setName(destinationFileName);
descriptor.setOverWrite(true);
ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/copy/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));
// old
assertNotNull(myProject.getBaseFolder().getChild("a/b/" + originFileName));
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();
}
}
}
Aggregations