use of org.eclipse.che.api.core.ConflictException in project che by eclipse.
the class UserDaoTest method shouldNotCreateUserWhenSubscriberThrowsExceptionOnUserStoring.
@Test(dependsOnMethods = "shouldThrowNotFoundExceptionWhenGettingNonExistingUserById", expectedExceptions = NotFoundException.class)
public void shouldNotCreateUserWhenSubscriberThrowsExceptionOnUserStoring() throws Exception {
final UserImpl newUser = new UserImpl("user123", "user123@eclipse.org", "user_name", "password", asList("google:user123", "github:user123"));
CascadeEventSubscriber<PostUserPersistedEvent> subscriber = mockCascadeEventSubscriber();
doThrow(new ConflictException("error")).when(subscriber).onCascadeEvent(any());
eventService.subscribe(subscriber, PostUserPersistedEvent.class);
try {
userDao.create(newUser);
fail("UserDao#create had to throw conflict exception");
} catch (ConflictException ignored) {
}
eventService.unsubscribe(subscriber, PostUserPersistedEvent.class);
userDao.getById(newUser.getId());
}
use of org.eclipse.che.api.core.ConflictException 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.ConflictException in project che by eclipse.
the class ProjectRegistry method setProjectType.
/* ------------------------------------------ */
/* to use from extension */
/* ------------------------------------------ */
/**
* Extension writer should call this method to apply changes which (supposedly) change
* Attributes defined with particular Project Type
* If incoming Project Type is primary and:
* - If the folder located on projectPath is a Project, its Primary PT will be converted to incoming PT
* - If the folder located on projectPath is NOT a Project the folder will be converted to "detected" Project with incoming Primary PT
* If incoming Project Type is mixin and:
* - If the folder located on projectPath is a Project, this PT will be added (if not already there) to its Mixin PTs
* - If the folder located on projectPath is NOT a Project - ConflictException will be thrown
* For example:
* - extension code knows that particular file content is used by Value Provider
* so this method should be called when content of this file changed to check
* and update attributes.
* OR
* If Extension writer wants to force initializing folder to be Project
* For example:
* - extension code knows that particular folder inside should (or may) be treated
* as sub-project of same as "parent" project type
*
* @param projectPath
* absolute project path
* @param type
* type to be updated or added
* @param asMixin
* whether the type supposed to be mixin (true) or primary (false)
* @return refreshed project
* @throws ConflictException
* @throws NotFoundException
* @throws ServerException
*/
public RegisteredProject setProjectType(String projectPath, String type, boolean asMixin) throws ConflictException, NotFoundException, ServerException {
final RegisteredProject project = getProject(projectPath);
final NewProjectConfig conf;
List<String> newMixins = new ArrayList<>();
if (project == null) {
if (asMixin) {
throw new ConflictException("Can not assign as mixin type '" + type + "' since the " + projectPath + " is not a project.");
} else {
final String path = absolutizePath(projectPath);
final String name = Path.of(projectPath).getName();
conf = new NewProjectConfigImpl(path, type, newMixins, name, name, null, null, null);
return putProject(conf, root.getChildFolder(path), true, true);
}
} else {
newMixins = project.getMixins();
String newType = project.getType();
if (asMixin) {
if (!newMixins.contains(type)) {
newMixins.add(type);
}
} else {
newType = type;
}
conf = new NewProjectConfigImpl(project.getPath(), newType, newMixins, project.getName(), project.getDescription(), project.getAttributes(), null, project.getSource());
return putProject(conf, project.getBaseFolder(), true, project.isDetected());
}
}
use of org.eclipse.che.api.core.ConflictException 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.ConflictException in project che by eclipse.
the class ProjectService method search.
@GET
@Path("/search/{path:.*}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Search for resources", notes = "Search for resources applying a number of search filters as query parameters", response = ItemReference.class, responseContainer = "List")
@ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "User not authorized to call this operation"), @ApiResponse(code = 404, message = "Not found"), @ApiResponse(code = 409, message = "Conflict error"), @ApiResponse(code = 500, message = "Internal Server Error") })
public List<ItemReference> search(@ApiParam(value = "Path to resource, i.e. where to search?", required = true) @PathParam("path") String path, @ApiParam(value = "Resource name") @QueryParam("name") String name, @ApiParam(value = "Search keywords") @QueryParam("text") String text, @ApiParam(value = "Maximum items to display. If this parameter is dropped, there are no limits") @QueryParam("maxItems") @DefaultValue("-1") int maxItems, @ApiParam(value = "Skip count") @QueryParam("skipCount") int skipCount) throws NotFoundException, ForbiddenException, ConflictException, ServerException {
final Searcher searcher;
try {
searcher = projectManager.getSearcher();
} catch (NotFoundException e) {
LOG.warn(e.getLocalizedMessage());
return Collections.emptyList();
}
if (skipCount < 0) {
throw new ConflictException(String.format("Invalid 'skipCount' parameter: %d.", skipCount));
}
final QueryExpression expr = new QueryExpression().setPath(path.startsWith("/") ? path : ('/' + path)).setName(name).setText(text).setMaxItems(maxItems).setSkipCount(skipCount);
final SearchResult result = searcher.search(expr);
final List<SearchResultEntry> searchResultEntries = result.getResults();
final List<ItemReference> items = new ArrayList<>(searchResultEntries.size());
final FolderEntry root = projectManager.getProjectsRoot();
for (SearchResultEntry searchResultEntry : searchResultEntries) {
final VirtualFileEntry child = root.getChild(searchResultEntry.getFilePath());
if (child != null && child.isFile()) {
items.add(injectFileLinks(asDto((FileEntry) child)));
}
}
return items;
}
Aggregations