use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class ProjectManager method createBatchProjects.
/**
* Create batch of projects according to their configurations.
* <p/>
* Notes: - a project will be created by importing when project configuration contains {@link SourceStorage} object,
* otherwise this one will be created corresponding its {@link NewProjectConfig}:
* <li> - {@link NewProjectConfig} object contains only one mandatory {@link NewProjectConfig#setPath(String)} field.
* In this case Project will be created as project of {@link BaseProjectType} type </li>
* <li> - a project will be created as project of {@link BaseProjectType} type with {@link Problem#code} = 12
* when declared primary project type is not registered, </li>
* <li> - a project will be created with {@link Problem#code} = 12 and without mixin project type
* when declared mixin project type is not registered</li>
* <li> - for creating a project by generator {@link NewProjectConfig#getOptions()} should be specified.</li>
*
* @param projectConfigList
* the list of configurations to create projects
* @param rewrite
* whether rewrite or not (throw exception otherwise) if such a project exists
* @return the list of new projects
* @throws BadRequestException
* when {@link NewProjectConfig} object not contains mandatory {@link NewProjectConfig#setPath(String)} field.
* @throws ConflictException
* when the same path project exists and {@code rewrite} is {@code false}
* @throws ForbiddenException
* when trying to overwrite the project and this one contains at least one locked file
* @throws NotFoundException
* when parent folder does not exist
* @throws UnauthorizedException
* if user isn't authorized to access to location at importing source code
* @throws ServerException
* if other error occurs
*/
public List<RegisteredProject> createBatchProjects(List<? extends NewProjectConfig> projectConfigList, boolean rewrite, ProjectOutputLineConsumerFactory lineConsumerFactory) throws BadRequestException, ConflictException, ForbiddenException, NotFoundException, ServerException, UnauthorizedException, IOException {
fileWatcherManager.suspend();
try {
final List<RegisteredProject> projects = new ArrayList<>(projectConfigList.size());
validateProjectConfigurations(projectConfigList, rewrite);
final List<NewProjectConfig> sortedConfigList = projectConfigList.stream().sorted((config1, config2) -> config1.getPath().compareTo(config2.getPath())).collect(Collectors.toList());
for (NewProjectConfig projectConfig : sortedConfigList) {
RegisteredProject registeredProject;
final String pathToProject = projectConfig.getPath();
//creating project(by config or by importing source code)
try {
final SourceStorage sourceStorage = projectConfig.getSource();
if (sourceStorage != null && !isNullOrEmpty(sourceStorage.getLocation())) {
doImportProject(pathToProject, sourceStorage, rewrite, lineConsumerFactory.setProjectName(projectConfig.getPath()));
} else if (!isVirtualFileExist(pathToProject)) {
registeredProject = doCreateProject(projectConfig, projectConfig.getOptions());
projects.add(registeredProject);
continue;
}
} catch (Exception e) {
if (!isVirtualFileExist(pathToProject)) {
//project folder is absent
rollbackCreatingBatchProjects(projects);
throw e;
}
}
//update project
if (isVirtualFileExist(pathToProject)) {
try {
registeredProject = updateProject(projectConfig);
} catch (Exception e) {
registeredProject = projectRegistry.putProject(projectConfig, asFolder(pathToProject), true, false);
final Problem problem = new Problem(NOT_UPDATED_PROJECT, "The project is not updated, caused by " + e.getLocalizedMessage());
registeredProject.getProblems().add(problem);
}
} else {
registeredProject = projectRegistry.putProject(projectConfig, null, true, false);
}
projects.add(registeredProject);
}
return projects;
} finally {
fileWatcherManager.resume();
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class ProjectService method uploadFile.
/* --------------------------------------------------------------------------- */
/* TODO check "upload" methods below, they were copied from old VFS as is */
/* --------------------------------------------------------------------------- */
private static Response uploadFile(VirtualFile parent, Iterator<FileItem> formData) throws ForbiddenException, ConflictException, ServerException {
try {
FileItem contentItem = null;
String name = null;
boolean overwrite = false;
while (formData.hasNext()) {
FileItem item = formData.next();
if (!item.isFormField()) {
if (contentItem == null) {
contentItem = item;
} else {
throw new ServerException("More then one upload file is found but only one should be. ");
}
} else if ("name".equals(item.getFieldName())) {
name = item.getString().trim();
} else if ("overwrite".equals(item.getFieldName())) {
overwrite = Boolean.parseBoolean(item.getString().trim());
}
}
if (contentItem == null) {
throw new ServerException("Cannot find file for upload. ");
}
if (name == null || name.isEmpty()) {
name = contentItem.getName();
}
try {
try {
parent.createFile(name, contentItem.getInputStream());
} catch (ConflictException e) {
if (!overwrite) {
throw new ConflictException("Unable upload file. Item with the same name exists. ");
}
parent.getChild(org.eclipse.che.api.vfs.Path.of(name)).updateContent(contentItem.getInputStream(), null);
}
} catch (IOException ioe) {
throw new ServerException(ioe.getMessage(), ioe);
}
return Response.ok("", MediaType.TEXT_HTML).build();
} catch (ForbiddenException | ConflictException | ServerException e) {
HtmlErrorFormatter.sendErrorAsHTML(e);
// never thrown
throw e;
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class ComposerProjectGenerator method onCreateProject.
@Override
public void onCreateProject(Path projectPath, Map<String, AttributeValue> attributes, Map<String, String> options) throws ForbiddenException, ConflictException, ServerException {
AttributeValue packageName = attributes.get(Constants.PACKAGE);
if (packageName == null) {
throw new ServerException("Missed some required options (package)");
}
VirtualFileSystem vfs = virtualFileSystemProvider.getVirtualFileSystem();
String projectAbsolutePath = new File(vfs.getRoot().toIoFile(), projectPath.toString()).toString();
String[] commandLine = { "composer", "create-project", packageName.getString(), projectAbsolutePath, "--no-install" };
try {
ComposerCommandExecutor.execute(commandLine, null);
} catch (TimeoutException | IOException | InterruptedException e) {
throw new ServerException(e);
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class LocalVirtualFileTest method getFileTreeAsList.
private List<VirtualFile> getFileTreeAsList(VirtualFile rootOfTree) throws Exception {
List<VirtualFile> list = newArrayList();
rootOfTree.accept(new VirtualFileVisitor() {
@Override
public void visit(VirtualFile virtualFile) throws ServerException {
list.add(virtualFile);
if (virtualFile.isFolder()) {
for (VirtualFile child : virtualFile.getChildren()) {
child.accept(this);
}
}
}
});
return list;
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class MemoryVirtualFileTest method getFileTreeAsList.
private List<VirtualFile> getFileTreeAsList(VirtualFile rootOfTree) throws Exception {
List<VirtualFile> list = newArrayList();
rootOfTree.accept(new VirtualFileVisitor() {
@Override
public void visit(VirtualFile virtualFile) throws ServerException {
list.add(virtualFile);
if (virtualFile.isFolder()) {
for (VirtualFile child : virtualFile.getChildren()) {
child.accept(this);
}
}
}
});
return list;
}
Aggregations