use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class MediaTypeFilter method accept.
@Override
public boolean accept(VirtualFile file) {
try (InputStream content = file.getContent()) {
TikaConfig tikaConfig = new TikaConfig();
MediaType mimeType = tikaConfig.getDetector().detect(content, new Metadata());
if (excludedMediaTypes.contains(mimeType) || excludedTypes.contains(mimeType.getType())) {
return true;
}
return false;
} catch (TikaException | ForbiddenException | ServerException | IOException e) {
return true;
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class HashSumsCounter method visit.
@Override
public void visit(VirtualFile virtualFile) throws ServerException {
if (virtualFile.isFile()) {
try (InputStream in = virtualFile.getContent()) {
final Hasher hasher = hashFunction.newHasher();
ByteStreams.copy(in, asOutputStream(hasher));
final String hexHash = hasher.hash().toString();
hashSums.add(Pair.of(hexHash, virtualFile.getPath().subPath(folder.getPath()).toString()));
} catch (IOException e) {
throw new ServerException(e);
} catch (ForbiddenException e) {
throw new ServerException(e.getServiceError());
}
} else {
for (VirtualFile child : virtualFile.getChildren()) {
child.accept(this);
}
}
}
use of org.eclipse.che.api.core.ForbiddenException in project che by eclipse.
the class TarArchiver method addTarEntry.
private void addTarEntry(VirtualFile virtualFile, TarArchiveOutputStream tarOutputStream) throws ServerException {
try {
TarArchiveEntry tarEntry = new TarArchiveEntry(getTarEntryName(virtualFile));
if (virtualFile.isFolder()) {
tarEntry.setModTime(0);
tarOutputStream.putArchiveEntry(tarEntry);
} else {
tarEntry.setSize(virtualFile.getLength());
tarEntry.setModTime(virtualFile.getLastModificationDate());
tarOutputStream.putArchiveEntry(tarEntry);
try (InputStream content = virtualFile.getContent()) {
ByteStreams.copy(content, tarOutputStream);
}
}
tarOutputStream.closeArchiveEntry();
} 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 ProjectManagerWriteTest method registerImporter.
private void registerImporter(String importType, InputStream zip) throws Exception {
final ValueHolder<FolderEntry> folderHolder = new ValueHolder<>();
importerRegistry.register(new ProjectImporter() {
@Override
public String getId() {
return importType;
}
@Override
public boolean isInternal() {
return false;
}
@Override
public String getDescription() {
return "importer";
}
@Override
public void importSources(FolderEntry baseFolder, SourceStorage storage) throws ConflictException, ServerException, ForbiddenException {
importSources(baseFolder, storage, LineConsumerFactory.NULL);
}
@Override
public void importSources(FolderEntry baseFolder, SourceStorage storage, LineConsumerFactory importOutputConsumerFactory) throws ConflictException, ServerException, ForbiddenException {
// Don't really use location in this test.
baseFolder.getVirtualFile().unzip(zip, true, 0);
folderHolder.set(baseFolder);
}
@Override
public ImporterCategory getCategory() {
return ProjectImporter.ImporterCategory.ARCHIVE;
}
});
}
use of org.eclipse.che.api.core.ForbiddenException 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();
}
}
}
Aggregations