use of org.eclipse.che.api.project.shared.dto.ItemReference 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;
}
use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.
the class ProjectServiceTest method testSearchByNameAndText.
@SuppressWarnings("unchecked")
@Test
public void testSearchByNameAndText() throws Exception {
RegisteredProject myProject = pm.getProject("my_project");
myProject.getBaseFolder().createFolder("a/b").createFile("test.txt", "test".getBytes(Charset.defaultCharset()));
myProject.getBaseFolder().createFolder("x/y").createFile("test.txt", "test".getBytes(Charset.defaultCharset()));
myProject.getBaseFolder().createFolder("c").createFile("test", "test".getBytes(Charset.defaultCharset()));
ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/search/my_project?text=test&name=test.txt", "http://localhost:8080/api", null, null, null);
assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
List<ItemReference> result = (List<ItemReference>) response.getEntity();
assertEquals(result.size(), 2);
assertEqualsNoOrder(new Object[] { result.get(0).getPath(), result.get(1).getPath() }, new Object[] { "/my_project/a/b/test.txt", "/my_project/x/y/test.txt" });
}
use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.
the class ProjectServiceTest method testSearchWordWithAnyEnding.
@SuppressWarnings("unchecked")
@Test
public void testSearchWordWithAnyEnding() throws Exception {
String queryToSearch = "?text=" + "question" + URL_ENCODED_ASTERISK;
RegisteredProject myProject = pm.getProject("my_project");
myProject.getBaseFolder().createFolder("x/y").createFile("containsSearchText.txt", "To be or not to be that is the question".getBytes(Charset.defaultCharset()));
myProject.getBaseFolder().createFolder("a/b").createFile("containsSearchTextAlso.txt", "Pay attention! To be or not to be that is the questionS".getBytes(Charset.defaultCharset()));
myProject.getBaseFolder().createFolder("c").createFile("notContainsSearchText", "Pay attention! To be or to not be that is the questEon".getBytes(Charset.defaultCharset()));
ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/search/my_project" + queryToSearch, "http://localhost:8080/api", null, null, null);
assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
List<ItemReference> result = (List<ItemReference>) response.getEntity();
assertEquals(result.size(), 2);
Set<String> paths = new LinkedHashSet<>(2);
paths.addAll(result.stream().map(ItemReference::getPath).collect(Collectors.toList()));
Assert.assertTrue(paths.contains("/my_project/x/y/containsSearchText.txt"));
Assert.assertTrue(paths.contains("/my_project/a/b/containsSearchTextAlso.txt"));
Assert.assertFalse(paths.contains("/my_project/c/notContainsSearchText.txt"));
}
use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.
the class ProjectServiceTest method testGetTreeWithDepthAndIncludeFilesNoFiles.
@Test
public void testGetTreeWithDepthAndIncludeFilesNoFiles() throws Exception {
RegisteredProject myProject = pm.getProject("my_project");
FolderEntry a = myProject.getBaseFolder().createFolder("a");
a.createFolder("b/c");
a.createFolder("x");
ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/tree/my_project/a?depth=100&includeFiles=true", "http://localhost:8080/api", null, null, null);
assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
TreeElement tree = (TreeElement) response.getEntity();
ItemReference a_node = tree.getNode();
assertEquals(a_node.getName(), "a");
List<TreeElement> children = tree.getChildren();
assertNotNull(children);
Set<String> names = new LinkedHashSet<>(4);
for (TreeElement subTree : children) {
ItemReference _node = subTree.getNode();
validateFolderLinks(_node);
String name = _node.getName();
names.add(name);
for (TreeElement subSubTree : subTree.getChildren()) {
ItemReference __node = subSubTree.getNode();
validateFolderLinks(__node);
names.add(name + "/" + __node.getName());
}
}
Assert.assertTrue(names.contains("b"));
Assert.assertTrue(names.contains("x"));
Assert.assertTrue(names.contains("b/c"));
Assert.assertFalse(names.contains("x/test.txt"));
}
use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.
the class ProjectServiceTest method testCreateFolder.
@Test
public void testCreateFolder() throws Exception {
ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/folder/my_project/test", "http://localhost:8080/api", null, null, null);
assertEquals(response.getStatus(), 201, "Error: " + response.getEntity());
ItemReference fileItem = (ItemReference) response.getEntity();
assertEquals(fileItem.getName(), "test");
assertEquals(fileItem.getPath(), "/my_project/test");
validateFolderLinks(fileItem);
assertEquals(response.getHttpHeaders().getFirst("Location"), URI.create("http://localhost:8080/api/project/children/my_project/test"));
VirtualFileEntry folder = pm.getProject("my_project").getBaseFolder().getChild("test");
Assert.assertTrue(folder.isFolder());
}
Aggregations